API Gateway Deployment using Amazon CloudFormation

SARVESH VIRKUD
3 min readJul 8, 2021

In my previous article, I walked you guys through creating an API Gateway and configuring the resources, methods of the API Gateway and then deploying that API Gateway to a stage. In this article, we are going to achieve the same thing by making use of Amazon CloudFormation.

I will be giving you guys a CloudFormation template to start with your API Gateway deployment. Follow these steps to deploy the template -

  1. From the AWS Management Console landing page, search for CloudFormation in the search bar and navigate your way to the CloudFormation console.
  2. Click on Create Stack.
  3. Copy the template provided at the bottom of the article and save it on your machine with the name apigw.yml. Here .yml extension in important.
  4. On the create stack screen, click on the upload a template file and then choose the file you saved above. Keep all other settings as default. Then click on next.
  5. Enter a name for the stack and then update the parameters to your liking if you want or you can keep them the way they are. In this stack, we will be creating an Edge Optimized API Gateway restricted by a resource policy and an API Key. If you want to secure your API Gateway, you can update the WhitelistIPrange parameter to your IP address e.g. a.b.c.d/32. If you don’t update this, the API gateway created will be open to the public but they will need the API key to invoke the APIs.
  6. Click on Next. You will be taken to the Advanced Stack options screen, You can leave everything as default and Click on Next.
  7. Now, review your configuration before launching the template. After review, scroll down to the bottom of the page and check the box stating “I acknowledge that AWS CloudFormation might create IAM resources with custom names”. Click on Create Stack after selecting that check box.

Note: This stack will create IAM roles and policies, so make sure that your User or Role has the required permissions to create IAM roles in your account.

5. The stack will take anywhere between 2-5 mins to launch. You can view the status of the stack in the Events tab of the stack. After the stack is deployed, you can go to the resources tab and observe all the resources that were created as a part of this stack. The stack is complete when you see the status “CREATE_COMPLETE”.

9. Now go the API Gateway Console and look for the API gateway created. Under the Resources tab, Click on the “POST” method under the path “/invokefunction”.

10. Click on Test. Keeping everything blank, scroll down to the bottom of the page and click on the Test button. You should see a response body having the message “Hello from Lambda!”.

Note: You can also test it using Postman by using the API key that you can get under the API Keys section. After going to that tab, select the api key name, and then click on show, which will provide you with the secret api key which you can use to invoke it from Postman. All you have to do is pass the secret api key as one of the headers (x-api-key) from Postman when you are calling the API endpoint as POST.

That’s it for this article. Thanks for reading! :)

--

--