Sub topic 3
Stages and deploying an HTTP API
Assume basic HTTP, Python, AWS CLI, IAM, and terminal skills. An API Gateway stage is a named deployment environment, such as dev or prod. A stage has a URL and points to a deployed API configuration. Keep development and production stages separate.
Deploying with AWS SAM
AWS SAM defines the API and Lambda function as code. Build and deploy the stack with:
sam build
sam deploy --guidedThe deployment output includes the API URL. Call that URL with curl. Use separate AWS stacks or stage names when you need isolated environments.
flowchart LR
Client --> Stage["API Gateway stage"]
Stage --> Route["Route and integration"]
Route --> Lambda["Lambda function"]
Lambda --> Logs["CloudWatch Logs"]caption: A request travels through a stage, route, Lambda integration, and logs. alt: A flow from a client through an API Gateway stage and route to Lambda, with Lambda writing CloudWatch Logs.
Browser access with CORS
Cross-origin resource sharing (CORS) controls whether browser JavaScript may call an API hosted on another origin. Configure allowed origins, methods, and headers in the HTTP API. Allow only the origins your application uses. Avoid * when requests include credentials.
Protecting routes with authorizers
An authorizer checks identity before API Gateway invokes Lambda.
- A JWT authorizer validates tokens from an identity provider.
- A Lambda authorizer runs your validation code and returns an authorization decision.
- An IAM authorizer requires callers to sign requests with AWS credentials.
Attach an authorizer only to routes that need it. Keep token validation outside the business handler.
> callout note > Authentication identifies a caller. Authorization decides whether that caller may use a route or resource.
Testing and debugging
Run the API locally before deploying:
sam local start-api
curl -i http://127.0.0.1:3000/items/42A 500 usually means the function raised an exception. A 502 often means the handler returned an invalid proxy response, such as a non-string body or missing statusCode. Inspect the event and test error paths with representative JSON files.
Logs and metrics
Enable access logs for request-level details. Lambda writes function output and stack traces to CloudWatch Logs. API Gateway metrics help reveal latency, request counts, and error rates. Never log tokens or sensitive request bodies.
Throttling
Throttling limits request rates and protects Lambda from bursts. Set suitable route or stage limits, then handle 429 Too Many Requests with client backoff. Throttling is protection, not authentication.
Cold starts and performance
A cold start occurs when Lambda creates a new execution environment. Reduce startup work, package only needed dependencies, reuse clients outside the handler, and choose memory deliberately. More memory can also provide more CPU and reduce latency.
Custom domains
A custom domain gives clients a stable URL such as api.example.com. Configure an API Gateway certificate, map the domain to the stage, and create the required DNS record. Test the certificate, DNS, and route mapping before changing clients.
Exercise
A deployed request returns 502, but the Lambda code ran. What should you inspect first?
Show answer
Inspect the Lambda proxy response. Verify that it includes statusCode and that body is a string, commonly JSON encoded.