Step-by-Step Guide to Using Lambda Proxy Integrations in Amazon API Gateway

Step-by-Step Guide to Using Lambda Proxy Integrations in Amazon API Gateway

ยท

4 min read

In this post, we will create a REST API endpoint with GET and POST methods for the previous backend using Amazon API Gateway's HTTP API.

Prerequisites

All you need is just an AWS account: no worries, if you are new to AWS, AWS offers you a 12-month free tier account where you can play with lots of services as a developer (isn't it cool? ๐Ÿ˜œ)

Step-by-step guide to creating a Lambda integration HTTP API

Amazon API Gateway is an AWS service that helps you create and secure REST and HTTP APIs at any scale.

The following steps will show you how to create an HTTP API and set up a POST method for an API resource.

Let's use the AWS console to create the API Gateway.

  • On the API Gateway dashboard menu, select APIs (1) and then click on the Create API (2) button.

  • On the Create API page, click on the Build button (1) under HTTP API section.

  • In the Create an API step, select Lambda (1) in the Create and configure integrations section, then choose us-east-1 as the AWS Region (2). Select the ***Lambda function:***arn:aws:lambda:us-east-1:xxx:function:sample-crud-func (3) we created in this post. Then, give the API name: sample-crud-api (4), and click Next (5).

  • In the Configure routes step, select POST as the Method (1), type /api/items as the Resource path (2), select sample-crud-func as the Integration target (3) and then click Next (4).

  • Provide the Stage name*: dev* (1) and then click Next (2) in the Configure stages step.

  • The last step is to review what we provided in the previous steps and click Create (1).

Setting up the HTTP GET method for an existing API Gateway Lambda integration

  • Select the API we created early (sample-crud-func), on the API Gateway menu, choose Routes menu (1), then click Create (2).

  • On the Create a route page, under the Route and method section, choose the GET method (1) and provide the route path: /api/items (2). Then, click on the Create button (3).

  • On the Routes page, under the /api/items tree, choose the GET method (1). In the Route details section, click on Attach integration (2).

  • Select the Integrations menu (1). In the Attach integrations to routes tab, choose GET (2) under the /api/items tree. In the Integration details for route section, choose the Lambda function: sample-crud-funcxxx (3), then click on the Attach integration button (4).

Update the Lambda function to handle the API HTTP methods

Let's update this lambda function to handle the GET and POST requests we created above.

As of the time we are writing this article, the version of the event payload format that API Gateway sends to Lambda is 2.0.

We will use the routeKey attribute value from the event payload to get the request route path. This will help direct traffic to the corresponding Lambda CRUD methods. Additionally, we will use the queryStringParameters attribute value to get request parameters.

The index.mjs file's handler function becomes this:

  • An environment variable called TABLE is created to handle the DynamoDB table's name. We retrieve its value into the TABLE_NAME variable in line 4: const TABLE_NAME = process.env.TABLE;. More information about Lambda environment variables can be found here.

  • In line 5, we get the routeKey value, which is the API request's path from the API Gateway. This value will be used in line 14 to call the suitable action implemented in the Lambda function.

  • This pattern is called Lambda monolith: using a single backend Lambda function to dispatch incoming traffic to multiple implemented actions. It's used for simplicity and is more suitable for small applications or monolith applications migrated to AWS Lambda. Take a look at this documentation for the recommendation.

Recap and test

  • Let's go back to the API Gateway and choose the sample-crud-apixxx, then choose the Stages menu (1) and dev stage (2) on the Stages page. In the Stages details section, copy the Invoke URL value (3).

  • Open Postman, choose the POST method, paste the URL, and append /api/items.

  • Select the Body tab, and past the payload data (3) then click Send (4) to get the result (5) if everything is ok.

In this post, we provided a step-by-step guide to creating an API Gateway HTTP API with Lambda proxy integrations, highlighting the Lambda monolith pattern. You can find the source code for the Lambda function here.

Thanks for reading ๐Ÿ˜Š. Your suggestions and comments are welcome. Akpรฉ kaka ๐Ÿ™!

Did you find this article valuable?

Support Wilson KOMLAN by becoming a sponsor. Any amount is appreciated!

ย