Certopolis

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.

Written from these sources

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. An AWS Lambda function is invoked asynchronously by Amazon S3 event notifications. Some invocations fail all retry attempts, and the events are lost. A developer must capture the failed events, together with details about the function's error response, for later analysis. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Development with AWS Services building.

    Not that one: A is right. In the game your Development with AWS Services building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    The record is a JSON document containing the request payload plus response context, including the function error, so the failed events are preserved with diagnostic detail for later analysis or redrive.

    Written from docs.aws.amazon.com/lambda/latest/dg/invocation-async-retain-records.html

  2. An AWS Lambda function is invoked asynchronously and currently writes discarded events to a dead-letter queue. During incident reviews the team can see the original event but cannot tell which error the function returned or how many attempts were made without correlating CloudWatch Logs by hand. The team wants that context delivered with each failed event. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Development with AWS Services building.

    Not that one: D is right. In the game your Development with AWS Services building would lose a little Renown, and this question would come back later.

  3. An AWS Lambda function is invoked asynchronously by Amazon S3 event notifications and has an on-failure destination that points to an Amazon SQS queue. To stop a runaway loop, an on-call engineer sets the function's reserved concurrency to 0. Events continue to arrive from Amazon S3, and the SQS queue immediately fills with invocation records. What could be causing this?

    Right. In the game that’s Renown for your Development with AWS Services building.

    Not that one: B is right. In the game your Development with AWS Services building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Throttling the function to zero therefore routes the event stream straight into the destination rather than pausing it.

    Written from docs.aws.amazon.com/lambda/latest/dg/invocation-async-retain-records.html

Ask for something