Sub topic 1
How an HTTP API works: routes, integrations and stages
Prerequisites
This chapter assumes you know basic HTTP (methods, status codes, headers, JSON bodies). It also assumes you have an AWS account and a basic grasp of IAM roles. You don't need to deploy anything yet. Later chapters build the API step by step.
The four building blocks
An Amazon API Gateway HTTP API is a managed front door for your backend. It accepts HTTPS requests, decides which backend should handle each one, and returns the backend's response. Four objects do this work.
- API: the top-level container. It has an ID and a base URL such as
https://abc123.execute-api.us-east-1.amazonaws.com. - Route: a combination of an HTTP method and a path, for example
GET /items/{id}. The route decides which requests match. - Integration: the backend a route calls. In this tutorial the backend is an AWS Lambda function.
- Stage: a named, deployed snapshot of the API, such as
prodor$default. Clients reach the API through a stage.
A route is linked to exactly one integration. Several routes can share the same integration.
How a request flows
The steps are:
- The client sends a request to the API's URL.
- The stage receives it and applies its settings, such as logging and throttling.
- API Gateway looks for the route that matches the method and path. If none matches, it uses the
$defaultroute when you have defined one. Otherwise it returns404 Not Found. - The route's integration invokes the Lambda function with an event that describes the request.
- The function returns a result. API Gateway turns it into an HTTP response for the client.
Routes and stages in practice
A route key has the form METHOD /path. Curly braces mark a path parameter, so GET /items/{id} matches /items/42 and passes 42 to your function as id. The special key $default catches any request that no other route matches.
Stages let you separate environments or versions under one API. A stage name becomes part of the URL unless it is the $default stage. A prod stage is called at https://abc123.execute-api.us-east-1.amazonaws.com/prod/items. The $default stage has no prefix. HTTP APIs can also deploy automatically, so a change to a route takes effect without a separate deployment step.
| Concept | Answers the question | Example |
|---|---|---|
| Route | Which requests do I accept? | `POST /items` |
| Integration | Who handles them? | A Lambda function |
| Stage | Where and how is the API published? | `$default` or `prod` |
Common mistakes
- Forgetting the stage name in the URL when the stage is not
$default. The result is a{"message":"Not Found"}response. - Defining a route but no integration. A route without an integration cannot serve requests.
- Assuming the path alone selects a route. The method is part of the route key, so
GET /itemsandPOST /itemsare different routes.
Exercise
A client calls https://abc123.execute-api.us-east-1.amazonaws.com/prod/items/7 with GET. The API has the routes GET /items/{id} and POST /items, plus a prod stage. Which route matches, what value does id have, and what happens if the client calls DELETE /items/7 instead?
Show answer
GET /items/{id} matches, and id is 7. The prod segment is the stage name and is not part of the route path. DELETE /items/7 matches no route, and no $default route exists, so API Gateway returns 404 Not Found.