Introduction

Amazon API Gateway is a fully managed service through which application programming interfaces are created, published, secured, and monitored. An application programming interface, known as an API, is a defined set of endpoints through which one program requests data or actions from another. This guide explains what API Gateway is, the problem it was created to solve, how it processes a request, and the situations in which it is appropriate.

What is it?

API Gateway acts as the front door for an application. Requests from clients are received by the service, which then routes each request to the correct backend, applies security and rate controls, and returns the response. A client is any program that makes a request, for example a web browser or a mobile application. A backend is the service that performs the work, for example an AWS Lambda function or an HTTP server.

Two principal API types are offered. A REST API provides the fuller feature set, and an HTTP API provides a smaller feature set at lower cost and latency. A third type supports WebSocket connections for two-way, real-time communication.

Why does it exist?

An API that is exposed to the public must perform several tasks that are unrelated to its core logic. Requests must be routed to the correct handler, callers must be authenticated, the rate of requests must be limited so that the backend is not overwhelmed, and traffic must be monitored. If these tasks were implemented separately for every service, a large amount of duplicated effort would be required. API Gateway was created so that these concerns are handled by a single managed service, which allows the backend to focus on business logic.

How it works

An API in API Gateway is composed of resources and methods. A resource is a path, for example /orders. A method is an HTTP verb on that path, for example GET or POST. Each method is connected to an integration, which defines the backend that is called and how the request and response are mapped.

When a request arrives, the path and method are matched to a configured resource. An authorizer may then be invoked to verify the caller. An authorizer is a component that confirms identity and permissions, and it may be based on Amazon Cognito or on a custom Lambda function. Throttling, which is the deliberate limiting of the request rate, is applied so that the backend is protected. The request is then passed to the integration, and the response is returned to the client. Each deployed version of an API is published to a stage, for example a stage named prod, so that separate versions can be maintained.

Architecture diagram

Client Amazon API Gateway Backend (browser, --> route match -> authorizer -> (Lambda function, mobile, throttling (Cognito or HTTP endpoint, service) request mapping Lambda) AWS service) | v Access logs and metrics are sent to Amazon CloudWatch

Advantages

Disadvantages

Common use cases

Best practices

Common mistakes

Further reading in this library

Frequently Asked Questions

What is the difference between a REST API and an HTTP API in API Gateway?
A REST API provides the fuller feature set, including request validation, response caching, and usage plans. An HTTP API provides a smaller feature set at lower cost and latency. The HTTP API should be chosen when its features are sufficient.
How is Amazon API Gateway priced?
Charges are based on the number of API calls that are received and, for REST APIs, on any response caching that is enabled. HTTP APIs are generally less expensive per request.
Can API Gateway call backends other than AWS Lambda?
Yes. In addition to invoking Lambda functions, API Gateway can forward requests to any reachable HTTP endpoint and can integrate directly with several AWS services.
What is an authorizer?
An authorizer is a component that verifies the identity and permissions of a caller before a request reaches the backend. Authorizers can be based on Amazon Cognito or on a custom Lambda function.
Does API Gateway handle CORS?
Yes. Cross-Origin Resource Sharing, known as CORS, is a browser mechanism that controls whether a web page may call an API on a different domain. API Gateway can be configured to return the headers that CORS requires.
AWS Serverless Architecture Handbook cover
Go deeper ยท Book as a Service™
AWS Serverless Architecture Handbook

This article is the summary. The book is the full, continuously updated reference: production API designs, authentication, throttling, and complete serverless architectures.

View the book