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

In this post, we will create a REST API endpoint with GET and POST methods for [the previous backend](https://blog.wilsonkomlan.com/create-a-fast-nodejs-serverless-backend-using-aws-lambda-and-dynamodb) using Amazon API Gateway's HTTP API.

### Prerequisites

All you need is just an [AWS account](https://console.aws.amazon.com/console/home): 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](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html) 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](https://us-east-1.console.aws.amazon.com/apigateway/main/apis?api=unselected&region=us-east-1&routes=0vkpr6p) to create the API Gateway.

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720880445937/dab838d8-8b1d-45db-82ad-bb9c4766cf07.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720880845816/1d191464-e428-40ef-ae41-fb688f2f0a22.png align="center")

* 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](https://blog.wilsonkomlan.com/create-a-fast-nodejs-serverless-backend-using-aws-lambda-and-dynamodb). Then, give the *API name: sample-crud-api* (4), and click ***Next*** (5).
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720880395200/9be900ad-96ee-43b3-9e07-927e32b915f6.png align="center")

* 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).
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720882135107/780aafd9-798d-4a9c-8ade-058fde0d3abc.png align="center")

* Provide the ***Stage name***\*: dev\* (1) and then click ***Next*** (2) in the **Configure stages** step.
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720884013458/012a60e5-87cd-4277-ab05-dcc881fce71d.png align="center")
    
* The last step is to review what we provided in the previous steps and click ***Create*** (1).
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720884551543/319c0fde-4be8-44cd-b515-8f736cb581cf.png align="center")

### 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).
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720913039975/c9b411ea-6aea-48f9-8b8d-403d1ff1c114.png align="center")

* 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).
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720913611641/cba4361c-557e-4f08-a65c-9bd2ca59d65e.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720915444632/8c0ffbb5-c866-4dfc-a6f2-3de445534f41.png align="center")

* 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).
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720913205174/6866c02d-1afe-42bf-96e0-87bb1836b7c5.png align="center")

### Update the Lambda function to handle the API HTTP methods

Let's update [this lambda function](https://blog.wilsonkomlan.com/create-a-fast-nodejs-serverless-backend-using-aws-lambda-and-dynamodb) 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](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html#http-api-develop-integrations-lambda.proxy-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 i[ndex.mjs](https://github.com/wilkom2009/sample-crud-func/blob/main/index.mjs) file's handler function becomes this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720952737164/d78909d7-0ab9-482f-9acf-512da9a57fcd.png align="center")

* 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](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars-retrieve.html).
    
* 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](https://docs.aws.amazon.com/lambda/latest/operatorguide/monolith.html): 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](https://docs.aws.amazon.com/lambda/latest/operatorguide/monolith.html) 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).
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720957430745/bfdfeb4e-5e43-4d34-a8ab-02ee25a62ee9.png align="center")

* 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.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1720960053635/96c9a3c3-be0c-4a1e-b784-400a3247afb6.png align="center")

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](https://github.com/wilkom2009/sample-crud-func/tree/main).

Thanks for reading 😊. Your suggestions and comments are welcome. Akpé kaka 🙏!
