Lesson 2 of 6 AWS Lambda

Handling Failures Across Lambda Invocation Models

This lesson explains how the way a function is invoked decides who waits for the result, who retries failures and where failed events end up.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish synchronous, asynchronous and event source mapping invocations.
  • Explain how destinations and dead-letter queues capture failed events.
  • Identify common Lambda triggers.

Invocation types

Lambda functions are invoked in three ways, and the invocation type decides who handles failures. In a synchronous invocation, used by API Gateway, ALBs, function URLs and direct SDK calls, the caller waits for the result. If the function fails, the error goes back to the caller, which decides whether to retry.

In an asynchronous invocation, used by S3, SNS and EventBridge, Lambda places the event on an internal queue and returns at once. If the function fails, Lambda retries it twice, and events that still fail can be sent to an on-failure destination or dead-letter queue. With an event source mapping, used for SQS, Kinesis, DynamoDB Streams and Amazon MSK, Lambda polls the source and invokes the function with batches of records; a failed batch is retried, and failed SQS messages return to the queue. The following table summarises the three types.

TypeSourcesBehaviour on failure
SynchronousAPI Gateway, ALB, function URLs, SDKThe caller receives the error and decides whether to retry
AsynchronousS3, SNS, EventBridgeQueued and retried twice, then sent to an on-failure destination or dead-letter queue
Event source mappingSQS, Kinesis, DynamoDB Streams, Amazon MSKLambda polls and retries the batch; failed SQS messages return to the queue

The following diagram shows each invocation type and its failure path.

The three Lambda invocation types and what happens on failure. In a synchronous invocation, the caller waits and receives the error. In an asynchronous invocation, Lambda queues the event, retries twice and then sends it to an on-failure destination or dead-letter queue. With an event source mapping, Lambda polls the source in batches and retries failed batches; failed SQS messages return to the queue.
The invocation type, not the function, decides who retries a failure and where the event ends up.
Sort into groups

How is it invoked?

Exam skills 2.1.5 · 2.1.1

Decide which invocation type each trigger uses.

Log in to try this activity Interactive activities are free with an account.

Capturing failures

An event that fails repeatedly should be captured rather than lost or retried for ever, and the mechanism depends on the trigger. For asynchronous triggers, an on-failure destination, an SQS queue, SNS topic, Lambda function, EventBridge bus or S3 bucket, receives events that still fail after Lambda's retries, together with the error details, so they can be inspected and reprocessed.

For SQS, failures are captured by a dead-letter queue on the source queue, which receives messages after a set number of failed receives. Partial batch responses let the function report only the messages that failed, so one bad message does not cause the whole batch to be retried. For Kinesis and DynamoDB Streams, a failing batch blocks its shard, so bisecting the batch on error, limiting retries and sending details of skipped records to an on-failure destination keep one bad record from stopping processing. The following table summarises the mechanisms.

Trigger typeCapture failures with
Asynchronous (S3, SNS, EventBridge)On-failure destinations (SQS, SNS, Lambda, EventBridge, S3) after retries, including error details
SQSA DLQ on the source queue, plus partial batch responses so one bad message does not retry the batch
Kinesis and DynamoDB StreamsBisect on error, a retry limit and an on-failure destination, so one bad record does not block the shard
Scenario questions

Capturing failed events

Exam skills 2.1.3 · 2.2.4

Choose how to capture failures for each trigger.

Log in to try this activity Interactive activities are free with an account.

Common triggers

API Gateway, function URLs, ALBs, S3 notifications, DynamoDB Streams, Kinesis, SQS, SNS, EventBridge rules and schedules, Cognito, CloudFront (Lambda@Edge) and Step Functions. Because every model can deliver an event more than once, handlers should be idempotent.

Worked example

Where did the failed events go?

The destination of a failed Lambda event depends on the invocation model of its trigger. The following table identifies where failed events end up for several triggers.

TriggerInvocation modelFailed events end up
API GatewaySynchronousReturned to the client as an error
S3 upload notificationAsynchronousOn-failure destination after two retries, or lost if none is set
SQS queueEvent source mappingThe queue's dead-letter queue after maxReceiveCount attempts
Kinesis streamEvent source mappingAn on-failure destination once retries or record age are exhausted

A team that lost failed image-processing events adds an on-failure destination to the S3-triggered function, so every failure is kept with its error details for reprocessing.

Check yourself

What is an event source mapping?

Show answer

A Lambda resource that polls a queue or stream, such as SQS, Kinesis or DynamoDB Streams, and invokes the function with batches of records.

Exam Tips

Remember for the exam

  • To capture events that failed after asynchronous retries, use a Lambda on-failure destination.
  • To run a function on a schedule, use EventBridge Scheduler or a scheduled rule.
  • To retry only the failed messages in an SQS batch, use partial batch responses.
  • Lambda handlers should be idempotent, because events can be delivered more than once.