API Gateway REST API with Lambda Integration
Tags: awslambda, amazonapigateway, aws
Published: 2024-02-22 02:51:42

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

- Sign in to the [Lambda console](https://console.aws.amazon.com/lambda)
- 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.
- Choose Functions in the navigation pane.
- Choose Create function.
- Choose Author from scratch.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7wkwhoi47ckp3dz31nz6.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.
- Sign in to the [API Gateway console](https://console.aws.amazon.com/apigateway)
- Choose Create API. Under REST API, choose Build.

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxs459pdw4esr974puqm.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/ogj88dsv90k5wds0ecxw.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/yffsl8cc7n5pevoihrqb.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dalrv1gphc243facah6o.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/08txgsocft7v26eph7ro.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/ay88meh4tuquouvcrgge.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/7iggg79xitfiyifxoo7h.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6tmnd020jyck3rqdw564.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**.