Introduction

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

How to Setup Amazon API Gateway and Invoke a Lambda Function Architecture Diagram
API Gateway architecture diagram

Let’s begin by navigating to the AWS Management Console at aws.amazon.com and searching for the API Gateway, as shown below.

How to Setup Amazon API Gateway and Invoke a Lambda Function AWS Management Console

Different API types are shown.

How to Setup Amazon API Gateway and Invoke a Lambda Function API Types

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.

Methods

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.

Backend

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.

Transformation

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.

How to Setup Amazon API Gateway and Invoke a Lambda Function Customer API
How to Setup Amazon API Gateway and Invoke a Lambda FunctionChoose the Protocol

Or create a new API:

How to Setup Amazon API Gateway and Invoke a Lambda Function 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.

How to Setup Amazon API Gateway and Invoke a Lambda Function Actions Create Methods

Select GET.

How to Setup Amazon API Gateway and Invoke a Lambda Function Actions GET Setup

Create a Lambda Function

Go to the main page at https://console.aws.amazon.com/console/home and search for ‘Lambda’

Click on Lambda.

How to Setup Amazon API Gateway and Invoke a Lambda Function Search Lambda Function

Navigate to ‘Function’ in the left-hand menu and click ‘Create Function.’

How to Setup Amazon API Gateway and Invoke a Lambda Function Create Lambda Function

How to create a basic ‘Hello World’ function is shown below.

How to Setup Amazon API Gateway and Invoke a Lambda Function Hello World Function

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.

How to Setup Amazon API Gateway and Invoke a Lambda Function Add Trigger
How to Setup Amazon API Gateway and Invoke a Lambda Function Trigger Configuration
How to Setup Amazon API Gateway and Invoke a Lambda Function Add Trigger Existing API

You should notice a line connecting API Gateway to the Lambda function.

How to Setup Amazon API Gateway and Invoke a Lambda Function API Endpoint

The API endpoint will not work since we have yet to deploy the API. Let’s switch back to the API Gateway console:

https://us-east-1.console.aws.amazon.com/apigateway/home?region=us-east-1

The Lambda Function is now showing as ‘hello.’

Deploy as shown below.

How to Setup Amazon API Gateway and Invoke a Lambda Function Deploy
How to Setup Amazon API Gateway and Invoke a Lambda Function Deployment Stage

Click on the Invoke URL similar to the one highlighted below.

How to Setup Amazon API Gateway and Invoke a Lambda Function Invoke URL

Append /hello to the URL: https://5mu01ufx63.execute-api.us-east-1.amazonaws.com/DevEnv

How to Setup Amazon API Gateway and Invoke a Lambda Function Append /Hello

Conclusion

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!

Share This