Sub topic 1
How an HTTP API works: routes, integrations and stages
An Amazon API Gateway HTTP API is a managed front door for your backend. It accepts HTTPS requests, matches each one to a route, forwards it to an integration, and returns the integration's response to the caller. This chapter explains the four objects involved (API, route, integration, stage) and how a request moves through them. Later chapters build on this model.
Prerequisites
This chapter assumes you already have:
- Basic HTTP knowledge: methods, status codes, headers and JSON bodies.
- Working knowledge of Python or another language that Lambda supports.
- An AWS account and a basic understanding of IAM roles.
- A terminal with the AWS CLI installed and configured.
You do not need to have written a Lambda function yet. The next chapters cover Lambda in detail.
The four building blocks
An HTTP API is made of a small set of objects. Each has one job.
| Object | What it is | Example |
|---|---|---|
| API | The container for everything else. It has an ID and a base URL. | `ItemsApi`, ID `abc123` |
| Route | A match rule made of an HTTP method and a path. | `GET /items/{id}` |
| Integration | The backend a route sends requests to. | A Lambda function |
| Stage | A named, deployable snapshot of the API that clients call. | `$default`, `prod` |
The relationships matter more than the definitions:
- One API has many routes.
- Each route points to exactly one integration.
- Several routes can share the same integration. A single Lambda function can serve
GET /items/{id}andPOST /items. - One API has one or more stages. A stage is part of the URL that clients call (except the
$defaultstage, which adds nothing to the URL).
The life of a request
Suppose a client sends GET https://abc123.execute-api.us-east-1.amazonaws.com/items/42. The request goes through these steps:
- DNS resolves the API's endpoint hostname to API Gateway.
- API Gateway identifies the API from the hostname and the stage from the first path segment. With the
$defaultstage there is no stage segment. - It matches the method and path against the API's routes and picks the most specific one.
- If the route has an authorizer, API Gateway runs it. A failed check returns 401 or 403 without touching your backend.
- API Gateway applies throttling limits. A request over the limit returns 429.
- It calls the route's integration. For Lambda, it builds an event and invokes the function.
- It converts the integration's output into an HTTP response and sends it back.
Steps 4 and 5 happen before your code runs. Rejected requests never cost you a Lambda invocation, which is one reason to enforce authorization and limits at the gateway.
Routes
A route is identified by a route key. The key is the method and path joined by a space, for example POST /items. Three forms exist:
- Specific routes such as
GET /items/{id}. Curly braces define a path parameter. The value of{id}reaches your handler in the event. - Greedy routes such as
GET /files/{proxy+}. The+makes the parameter match the rest of the path, including slashes. - The
$defaultroute, a catch-all for requests that match no other route.
When several routes could match, API Gateway picks the most specific one. GET /items/{id} wins over GET /items/{proxy+}, and both win over $default. A route with method ANY matches any method, but a route with an explicit method beats it.
If no route matches and you have no $default route, API Gateway returns 404 Not Found itself. Your function is never called. This is a common source of confusion: a 404 with a body of {"message":"Not Found"} normally comes from the gateway, not from your code.
A route with a method that exists on the path but is not defined for that method also gives a 404 unless you add a matching route or a $default route. Routing is exact, so do not expect a 405 response.
Integrations
An integration tells API Gateway where to send a matched request. HTTP APIs support these types:
- AWS Lambda proxy: invokes a function and passes the request as an event. This is the type used throughout this tutorial.
- HTTP proxy: forwards the request to a public HTTP endpoint.
- Private integration: forwards to resources inside a VPC through a load balancer or Cloud Map service.
- AWS service integration: calls certain AWS APIs directly, such as sending a message to SQS.
"Proxy" means API Gateway passes the whole request through and expects the backend to produce the whole response. You do not write mapping templates as you would in a REST API. The next chapters cover the Lambda proxy integration and its payload format in detail.
When Lambda is the integration, API Gateway needs permission to call your function. That permission is a resource-based policy on the function that allows apigateway.amazonaws.com to invoke it, scoped to your API. AWS SAM creates this policy for you. If you create the pieces by hand and forget it, requests fail with a 500 error and the function shows no invocations.
Stages
A stage is a named deployment target for an API, such as dev, prod or $default. Stages let you run one set of routes under separate URLs, and they carry their own settings:
- Access logging
- Default and per-route throttling
- Stage variables
- Automatic deployment
The URL for a named stage includes the stage name:
https://abc123.execute-api.us-east-1.amazonaws.com/prod/items/42The $default stage serves the root path, so the same request becomes https://abc123.execute-api.us-east-1.amazonaws.com/items/42. This is the URL form used in later chapters.
HTTP APIs can deploy automatically. With auto-deploy enabled on a stage, any change you make to routes or integrations goes live without a separate deployment step. Quick-create APIs and SAM-defined APIs enable this by default. Without it, you must create a deployment and attach it to the stage before a change is visible.
Inspecting an API with the AWS CLI
You can look at these objects with the apigatewayv2 commands. The following script lists each object type for one API. Replace the API ID with your own.
API_ID="abc123"
aws apigatewayv2 get-api --api-id "$API_ID"
aws apigatewayv2 get-routes --api-id "$API_ID"
aws apigatewayv2 get-integrations --api-id "$API_ID"
aws apigatewayv2 get-stages --api-id "$API_ID"In the get-routes output, each route has a RouteKey and a Target such as integrations/xyz789. The ID after the slash matches an IntegrationId in the get-integrations output. This is the link from route to integration. In the get-stages output, look at AutoDeploy and DefaultRouteSettings.
Common mistakes
- Calling the wrong URL. Using the base URL with a named stage but leaving out the stage name returns
{"message":"Not Found"}. - Treating a gateway 404 as an application error. Check the route list before you look at function logs.
- Missing invoke permission. A 500 response with no Lambda invocation points to the resource policy, not your code.
- Forgetting that routes are exact.
GET /itemsandGET /items/are different keys. Pick one form and keep to it. - Assuming a deployment happened. Confirm stage auto-deploy or create a deployment.
Exercise
A client calls GET /items/42 on your HTTP API and receives {"message":"Not Found"}. The Lambda function's CloudWatch log group shows no new log entries. Which object should you check first, and why?
Show answer
Check the API's routes (and the stage in the URL). A Not Found response with no function invocation means API Gateway failed to match the request to any route, so the function was never called. Verify that a route key such as GET /items/{id} exists (or that a $default route exists), that the stage name in the URL is correct, and that the change has been deployed to the stage.
Exercise
An API has the routes GET /items/{id}, GET /items/{proxy+} and $default, all pointing to the same function. Which route handles GET /items/42, and which handles GET /items/42/history?
Show answer
GET /items/42 is handled by GET /items/{id}, because it is the most specific match. GET /items/42/history does not fit {id}, which matches a single segment, so GET /items/{proxy+} handles it. Requests that match neither, such as GET /orders, go to $default.