AWS Certified Developer – Associate
Lambda async invocation: retries, destinations, DLQ (DVA-C03)
The Lambda Developer Guide says that for an asynchronous invocation Lambda queues the event and returns a success response at once. If the function returns an error, Lambda retries twice by default. An event that fails every attempt or expires is discarded unless an on-failure destination or a dead-letter queue is configured.
What happens when Lambda is invoked asynchronously?
Services such as Amazon S3 and Amazon SNS invoke functions asynchronously. Your own code does the same by calling Invoke with InvocationType set to Event. Lambda places the event in a queue and returns status code 202 with no other information. A separate process reads the queue and sends events to the function.
When the function returns an error, Lambda by default runs it two more times. It waits one minute before the second attempt and two minutes before the third. Function errors include errors from the code and from the runtime, such as timeouts.
Throttling errors (429) and system errors (500-series) are handled differently. Lambda returns the event to the queue and keeps trying for up to 6 hours by default, with a retry interval that grows from 1 second to a maximum of 5 minutes.
The queue is eventually consistent, so a function can receive the same event more than once even without an error. Write the handler so that duplicate events are safe.
Which settings control retries and event age?
MaximumRetryAttempts is the number of times Lambda retries when the function returns an error. MaximumEventAgeInSeconds is how long Lambda keeps an event in the asynchronous queue before discarding it. Both can be set on a function, a version or an alias.
Setting reserved concurrency to zero stops the function from running. For an asynchronously invoked function, Lambda then sends new events to the configured dead-letter queue or on-failure destination without any retries.
Destinations vs dead-letter queues
A destination receives an invocation record, a JSON document about the request and the response. Supported targets are a standard SQS queue, a standard SNS topic, a Lambda function, an EventBridge event bus and, for failures only, an S3 bucket. You can set one destination for success and another for failure. The record includes requestContext with a condition such as RetriesExhausted and an approximateInvokeCount, the requestPayload, the responseContext and the responsePayload.
A dead-letter queue is the older alternative for discarded events. The target must be a standard SQS queue or a standard SNS topic. Lambda sends only the content of the event, with RequestID, ErrorCode and ErrorMessage as message attributes, and no details of the response. A DLQ can be set only at the function level, and versions use the setting of $LATEST.
For both options the function's execution role needs permission on the target, for example sqs:SendMessage for an SQS queue. If delivery fails, Lambda emits the DestinationDeliveryFailures metric for destinations and the DeadLetterErrors metric for a DLQ.
Where the exam catches people
- Believing a dead-letter queue delivers the function's response with the event: a DLQ gets only the event content. The on-failure destination record carries the response and the attempt count.
- Believing a FIFO queue or FIFO topic can be the target: destinations and dead-letter queues accept standard SQS queues and standard SNS topics only.
- Believing reserved concurrency of zero pauses asynchronous events until it is raised: Lambda sends new events straight to the DLQ or on-failure destination with no retries.
- Believing throttled events get only two retries: throttling and system errors are retried for up to 6 hours by default, limited by the maximum event age.
- Believing the caller's role needs the permission to write to the destination: the function's execution role does.