Opscraft
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 diagramSynchronousAPI Gateway, ALB, URLs · callerwaits; caller retriesAsynchronousS3, SNS, EventBridge · Lambdaqueues, retries 2×, then DLQPollingSQS, Kinesis, DynamoDB · Lambdapulls batches; source redelivers
Figure 1. Who waits and who retries depends on how the function is invoked.
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 set BisectBatchOnFunctionError, 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 diagramSQS queueordersLambdabatch of 10deleted9 succeededDLQafter maxReceiveCountpollReportBatchItemFailures returns only the 1 failed record to the queue.
Figure 2. SQS with Lambda: failed records return to the queue after the visibility timeout, and land in the DLQ after too many tries.
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 diagramclientPOST /reportsAPI + Lambda202 + job ID in200 msSQSjob queuedworkerLambda, Fargateor Step Functions
Figure 3. Long work behind an API: accept fast, queue, process, let the client check status.
Swipe sideways to see all of it, or tap to open it full screen.