Serverless with AWS Lambda · Lesson 2 of 6 · 4 min read
Triggers and invocation models
The three invocation models
How a function is invoked decides who retries, and what happens on failure:
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
| Model | Examples | Who waits | On error |
|---|---|---|---|
| Synchronous | API Gateway, ALB, function URLs, aws lambda invoke |
The caller waits for the result | The error goes back to the caller; the caller decides whether to retry |
| Asynchronous | S3 events, SNS, EventBridge | Lambda queues the event and returns 202 at once | Lambda retries twice (configurable 0–2), with up to 6 hours max event age; then sends it to an on-failure destination or a dead-letter queue |
| Event source mapping (polling) | SQS, Kinesis, DynamoDB Streams, Kafka | Lambda polls the source and invokes with batches | SQS: messages reappear after the visibility timeout and retry until maxReceiveCount, then go to the queue's DLQ. Streams: the batch retries until it succeeds or expires, blocking that shard |
Two consequences come up constantly in interviews:
- At-least-once delivery. Async and polling sources can deliver the same event more than once, so handlers must be idempotent: processing an event twice must have the same effect as once. Use an idempotency key (the message ID, an order ID) with a conditional write to DynamoDB, or the Powertools idempotency utility.
- Poison messages. One bad record in a batch makes the whole batch fail and retry. Turn on partial batch responses (
ReportBatchItemFailures) so only failed records are retried, and for streams setBisectBatchOnFunctionError, a maximum retry count and an on-failure destination so one bad record can't block a shard forever.
For SQS, set the queue's visibility timeout to at least six times the function timeout, or messages reappear and get processed twice while the first invocation is still running.
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
API Gateway and function URLs
| Option | Use when |
|---|---|
| HTTP API (API Gateway v2) | Most new REST-style APIs: cheaper and faster; JWT authorisers, CORS, Lambda and HTTP integrations |
| REST API (v1) | You need API keys and usage plans, request validation, caching, WAF directly on the API, private APIs or edge-optimised endpoints |
| Function URL | A simple HTTPS endpoint for one function (webhooks, internal tools), with IAM auth or none |
| ALB target | The function sits behind an existing ALB alongside containers |
API Gateway's integration timeout is 29 seconds by default, so a synchronous API backed by Lambda can't use the full 15 minutes. Long work should be accepted quickly (return 202 with a job ID), queued, and processed asynchronously.
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.