Amazon API Gateway is a service you can use to create application programming interfaces (APIs) that are gateways to your business logic or application on AWS. It creates, publishes, maintains, monitors, and secures REST, HTTP, and WebSocket APIs at any scale. API developers can create APIs that access AWS or other web services, as well as data stored in the AWS Cloud. It currently supports 1) REST API, 2) HTTP API, 3) REST API (private), and 4) WebSocket API. In this blog, we will explore how to set up an API Gateway and expose an API that invokes a Lambda function.
Architecture of API Gateway with Lambda Integration
Let’s begin by navigating to the AWS Management Console at aws.amazon.com and searching for the API Gateway, as shown below.
Different API types are shown.
REST API Design with Amazon API Gateway
REST API is suitable where you need to gain complete control over the request and response. Methods are GET, POST, PUT, PATCH, and DELETE.
REST APIs connect to published backend APIs. REST APIs have method requests. This defines which methods we’re going to allow the API to use. Methods are DELETE, GET, POST, and PUT.
Each method can then be mapped to various endpoints—for example, Lambda functions. The backend could be a Lambda function, an HTTP endpoint, an EC2 instance, or another AWS service.
Method requests and integration requests provide flexibility in the way that we configure our API. It can be just a pass-through method, or we can transform the payload before it reaches the backend. When the response is returned from the endpoint, we have some options. We can convert or pass through the information. We can also modify the HTTP status codes or response bodies. Example: If you already have an API Specification in Swagger or OAS format, then select ‘Import’ from Swagger or Open API 3.
For example, we can create a customer API. When you invoke the customer-API, the API Gateway routes the request to the Hello-world Lambda function. Lambda runs the Lambda function and returns a response to API Gateway, which then returns a response to you.
Or create a new API:
Endpoint Type: Regional APIs are deployed in the current region. Edge Optimized APIs are deployed to the CloudFront network. Private APIs are accessible only from VPCs.
Click on Actions -> Create Methods.
Create a Lambda Function
Go to the main page at https://console.aws.amazon.com/console/home and search for ‘Lambda’
Click on Lambda.
Navigate to ‘Function’ in the left-hand menu and click ‘Create Function.’
How to create a basic ‘Hello World’ function is shown below.
Connect to the API Gateway with the Lambda function
Click on ‘Add trigger’ and select the existing ‘Customer API’ from the drop-down as shown below.
You should notice a line connecting API Gateway to the Lambda function.
The API endpoint will not work since we have yet to deploy the API. Let’s switch back to the API Gateway console:
The Lambda Function is now showing as ‘hello.’
Deploy as shown below.
Click on the Invoke URL similar to the one highlighted below.
Append /hello to the URL: https://5mu01ufx63.execute-api.us-east-1.amazonaws.com/DevEnv
By integrating AWS Lambda with Amazon API Gateway, you create a serverless API that requires no server management, automatically scales, and can handle thousands of requests per second. This setup can be deployed within minutes and is covered by the AWS Free Tier: http://aws.amazon.com/free
For more information about Amazon API Gateway or AWS Lambda, please contact us!