AWS Lambda with API Gateway

Table of contents

To secure your Lambda function, you can use AS API Gateway to invoke the Lambda function. Please check the previous article on creating your first Lambda function if you don’t know how to create one. AWS API Gateway is a managed API Gateway service offered by AWS. It provides three kinds of products: HTTP, REST, and WebSocket. HTTP is the most basic API. You will use HTTP Api to connect API Gateway with Lambda.
Create an API Gateway API
Go to the API Gateway page. Click on the Create API button.
You will be redirected to a page where you must choose the API type you want to create. Select HTTP API.
Now you have to configure the API. Give the API a name. Keep the ipV4 selected. In the integration, click on Add integration. Select lambda. By default, your region should be us-east-1. Select the Lambda function you created in the previous blog. Keep version 2.0 selected. Click on next.
In the previous Blog, you created a Lambda function that accepts the Post and Get methods. So you will only allow these two methods to Lambda. Even though you created an exception for other methods, it is more secure and efficient to block all unwanted requests in the API Gateway. This reduces the Lambda invocations and secures our application from unauthorized access.
In the Configure route section, select GET. If you want, you can change the Resource path. Make sure the Integration target is the Lambda function. Click on Add route. Select the POST method and give the same resource path for a clearer URL. Also, make sure the Integration target is the same Lambda function. Click on next.
In the Define stage, rename the stage as dev. Enable auto-deploy if it is not pre-enabled. Whenever you make changes to the API, it will be auto-deployed to the dev stage. This is a simple API, enabling auto-deploy reduces steps. Click on next.
Review the entire API configuration before deployment. Once confident, click on Create.
Invoking Lambda through API Gateway
Open your new API gateway page. You can see both the GET and POST methods under the resource path.
You can also edit the routes to change the HTTP method and resource path.
But we are interested in invoking the API. The endpoint URL is available in the Stages section. Go to the Stages. Select the dev stage. You can see the invoke URL. Copy the URL.
Open Postman and create a new HTTP API. Paste the URL. Remember to add the resource path after the invoke URL text. Select the GET method and click on send.
GET
POST
PUT
In case of the PUT method, you will see a different response from the previous Blog. This is because Lambda function is not handling the PUT request anymore. This response is coming from the API Gateway. Because you did not allow the PUT method to call Lambda.
Subscribe to my newsletter
Read articles from Ritwik Math directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ritwik Math
Ritwik Math
As a seasoned senior web developer with a wealth of experience in Python, Javascript, web development, MySQL, MongoDB, and React, I am passionate about crafting exceptional digital experiences that delight users and drive business success.