API Gateway REST API with Lambda Integration
Tags: aws, apigateway, lambda
Published: 2023-11-28 05:56:06

I created an API using the API Gateway console that allows a client to call Lambda functions via the Lambda integration.

Before creating the API, set up the Lambda backend by creating a Lambda function in AWS Lambda.

### Create Lambda Function

1. Sign in to the [Lambda console](https://console.aws.amazon.com/lambda).
2. On the AWS navigation bar, choose a region Note: Note the region where you create the Lambda function. You'll need it when you create the API.
3. Choose Functions in the navigation pane.
4. Choose Create function.
5. Choose Author from scratch.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17b91d619dhbyyqd1v9r.png)

Under **Function code source**, write your code in the inline code editor

Choose **Deploy**

### Create API Gateway

Now create an API for your Lambda function by using the API Gateway console.

1. Sign in to the [API Gateway console](https://console.aws.amazon.com/apigateway)
2. Choose Create API. Under REST API, choose Build.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vtz8t26td6rf5d95t14.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/okuk87ziwxfk3qc7usgv.png)

Under Create new API, choose New API. Under Settings: For API name, enter "**API name**". Enter a description in the Description field or leave it empty. Leave Endpoint Type set to Regional. Choose Create API.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sohkmdlzku402s2we8qv.png)

Create the resource as follows:

Choose the root resource (/) in the **Resources** tree.

- Choose **Create Resource** from the **Actions** dropdown menu.
- Leave **Configure as proxy resource** unchecked.
- For **Resource Name**, enter "**resource_name**".
- Leave **Resource Path** set to **/resource_name**.
- Leave **Enable API Gateway CORS** unchecked.
- Choose **Create Resource**.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvxfas8h6gcglpguel7c.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/esq3wli7109lgebdyzvf.png)

The whole request is transmitted to the backend Lambda function through a proxy integration, using a catch-all ANY method that represents any HTTP method. The client specifies the actual HTTP method at run time. The ANY method enables you to utilize a single API method configuration for all available HTTP methods, including DELETE, GET, HEAD, OPTIONS, PATCH, POST, and PUT.

To set up the method, do the following:

- In the **Resources** list, choose **/"resource_name"**.
- In the **Actions** menu, choose **Create method**.
- Choose **ANY** from the dropdown menu, and choose the checkmark icon


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/geoefpcd3nsnfroao683.png)

- Leave the **Integration type** set to **Lambda Function**.
- Choose Use **Lambda Proxy integration**.
- From the **Lambda Region** dropdown menu, choose the region where you created the Lambda function.
- In the **Lambda Function** field, type any character and choose the Lambda function from the dropdown menu.
- Leave **Use Default Timeout** checked.
- Choose **Save**.
- Choose **OK** when prompted with **Add Permission to Lambda Function**.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xfds38c0oaticjvv9ue4.png)

### Deploy API

Deploy the API in the API Gateway console

- Choose **Deploy API** from the **Actions** dropdown menu.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y76i9onwbnbqa28qn55b.png)

For the **Deployment stage**, choose **[new stage]**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nnhkfodrbduublx69bwj.png)

- For the **Stage name**, enter "test_stage".
- If desired, enter a **Stage description**.
- If desired, enter a **Deployment description**.
- Choose **Deploy**.
- Note the API's **Invoke URL**.