Working with REST APIs in API Gateway

REST APIs (Representational State Transfer) are widely used for building scalable and interoperable web services. One popular solution to create and manage REST APIs is Amazon API Gateway. In this blog post, we’ll walk through the steps involved in creating an end-to-end REST API using Amazon API Gateway and Lambda functions.

Step 1: Create an HTTP API

To begin, navigate to the Amazon API Gateway console and choose the option to create a new REST API. Provide a name for your API and select the type of endpoint you want to use (Regional, Edge-optimized, or Private) based on your use case. For this demonstration, we’ll choose a regional endpoint.

A screenshot of a computer

Description automatically generated

A screenshot of a computer

Description automatically generated

Step 2: Create two Lambda Functions

Next, go to the AWS Lambda console and create two new Lambda functions. One function will handle the POST method, and the other will handle the GET method. You can use the default execution role for these Lambda functions.

  1. Creating this lambda function for the POST method.
A screenshot of a computer

Description automatically generated
  1. Creating this lambda function for the GET method.
A screenshot of a computer

Description automatically generated

Step 3: Create Integrations and Methods

Go back to the API Gateway console and create a new integration for each Lambda function. Add a resource to your API and give it a name.

A screenshot of a computer

Description automatically generated

Then, under that resource, create a method for each HTTP method you want to support (POST and GET). First, I am creating a POST method.

A screenshot of a computer

Description automatically generated

Select the appropriate Lambda function as the integration for each method and save the configuration. Ensure that the necessary permissions are granted to the Lambda functions.

A screenshot of a computer

Description automatically generated

Similarly, create a GET method with lambda integration.

A screenshot of a computer

Description automatically generated

A screenshot of a computer

Description automatically generated

Step 4: Test the API

You can now test your API using the built-in testing console in the API Gateway console. Use the available options to send requests and verify that you receive the expected responses. For the POST method, you can add a new record by providing a request body, and for the GET method, you can retrieve information from the database.

A screenshot of a computer

Description automatically generated

A screenshot of a computer

Description automatically generated

Step 5: Deploy the API

Once you have tested the API and are satisfied with the results, it’s time to deploy it. Click on the Actions button and choose the Deploy API option. Select a new stage and provide a name for it. After deployment, you will receive an Invoke URL that can be used to access your API.

A screenshot of a computer

Description automatically generated

A screenshot of a computer

Description automatically generated

Congratulations! You have successfully built a REST API using Amazon API Gateway and Lambda functions to handle GET and POST requests.

Addend Analytics is a Microsoft Gold Partner based in Mumbai, India, and a branch office in the U.S.

Addend has successfully implemented 100+ Microsoft Power BI and Business Central projects for 100+ clients across sectors like Financial Services, Banking, Insurance, Retail, Sales, Manufacturing, Real estate, Logistics, and Healthcare in countries like the US, Europe, Switzerland, and Australia.

Get a free consultation now by emailing us or contacting us.