Lesson content
Read, practise, then check your understanding
Serverless Computing: Lambda and API Gateway
AWS Lambda executes code in response to events, while Amazon API Gateway exposes HTTP, REST, or WebSocket interfaces with routing, authorization, throttling, and monitoring. Serverless removes server management but not architecture, security, testing, or cost responsibility.
Core ideas
- An API route maps a method and path to an integration such as Lambda.
- Lambda execution roles grant the function access to AWS resources; resource policies control who may invoke it.
- Asynchronous invocations and stream or queue event sources have different retry and failure behaviors.
- Idempotency prevents duplicate event delivery from creating duplicate business outcomes.
Design and operating model
Validate input at the edge and in the function, authenticate callers, set throttles and quotas, keep functions stateless, and store durable state in managed services. Configure reserved concurrency to protect dependencies. Route failed asynchronous events to a destination or dead-letter queue and trace requests end to end.
Example
import json
def handler(event, context):
request_id = context.aws_request_id
name = event.get("queryStringParameters", {}).get("name", "learner")
return {
"statusCode": 200,
"headers": {"content-type": "application/json"},
"body": json.dumps({"message": f"Hello, {name}", "requestId": request_id}),
}
Run examples in a disposable training account and replace Regions, identifiers, policies, resource sizes, and names with reviewed values. Verify commands with the current AWS CLI and service documentation before production use.
Production guidance
- Reuse SDK clients outside the handler, but never rely on memory as durable state.
- Set realistic timeout and memory, cap concurrency, and load-test downstream limits.
- Use structured logs, correlation IDs, traces, metrics, and alarms for every critical function path.
The chapter quiz follows the lesson and checks both service vocabulary and architecture decisions.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.