|
| 1 | +# Amazon EventBridge Scheduler to AWS Lambda with Dual Dead Letter Queues |
| 2 | + |
| 3 | +This pattern demonstrates how to use Amazon EventBridge Scheduler to invoke AWS Lambda functions with comprehensive failure handling through dual Dead Letter Queues (DLQs). The pattern is deployed using the AWS Serverless Application Model (SAM). |
| 4 | + |
| 5 | +Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns](https://serverlessland.com/patterns) |
| 6 | + |
| 7 | +Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 12 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 13 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 14 | +* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed |
| 15 | + |
| 16 | +## Deployment Instructions |
| 17 | + |
| 18 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 19 | + ``` |
| 20 | + git clone https://github.com/aws-samples/serverless-patterns |
| 21 | + ``` |
| 22 | +1. Change directory to the pattern directory: |
| 23 | + ``` |
| 24 | + cd eventbridge-schedule-to-lambda-dlq-sam-python |
| 25 | + ``` |
| 26 | +1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yaml file: |
| 27 | + ``` |
| 28 | + sam deploy --guided |
| 29 | + ``` |
| 30 | +1. During the prompts: |
| 31 | + * Enter a stack name |
| 32 | + * Enter the desired AWS Region |
| 33 | + * Allow SAM CLI to create IAM roles with the required permissions. |
| 34 | +
|
| 35 | + Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults. |
| 36 | +
|
| 37 | +1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. |
| 38 | +
|
| 39 | +## How it works |
| 40 | +
|
| 41 | +This pattern showcases EventBridge Scheduler's robust failure handling capabilities through two distinct failure paths: |
| 42 | +
|
| 43 | +### Dual Dead Letter Queue Architecture |
| 44 | +
|
| 45 | +**Lambda Execution DLQ**: Captures failures that occur during Lambda function execution (code errors, timeouts, out-of-memory errors). After Lambda's built-in async retry mechanism exhausts its attempts (default: 2 retries), failed events are sent to this queue. |
| 46 | +
|
| 47 | +**EventBridge Scheduler DLQ**: Captures failures that occur at the invocation level before the Lambda function executes. This includes: |
| 48 | +- IAM permission errors (scheduler role lacks lambda:InvokeFunction permission) |
| 49 | +- Lambda service throttling (concurrent execution limits reached) |
| 50 | +- Lambda function state issues (function being deleted, doesn't exist, invalid ARN) |
| 51 | +- Resource not found errors (function deleted after schedule creation) |
| 52 | +- Maximum event age exceeded (event couldn't be delivered within configured time window) |
| 53 | +- Maximum retry attempts exhausted (all scheduler retries failed) |
| 54 | +
|
| 55 | +### Workflow |
| 56 | +
|
| 57 | +1. EventBridge Scheduler invokes the Lambda function asynchronously every 5 minutes |
| 58 | +2. If invocation fails (permissions, throttling, etc.), EventBridge Scheduler retries up to 3 times |
| 59 | +3. After scheduler retries are exhausted, the event is sent to the EventBridge Scheduler DLQ |
| 60 | +4. If invocation succeeds but execution fails (code error), Lambda retries automatically |
| 61 | +5. After Lambda retries are exhausted, the event is sent to the Lambda Execution DLQ |
| 62 | +
|
| 63 | +This dual DLQ architecture provides complete visibility into both configuration-level and code-level failures, enabling appropriate remediation strategies for each failure type. |
| 64 | +
|
| 65 | +## Testing |
| 66 | +
|
| 67 | +### Test Normal Execution |
| 68 | +
|
| 69 | +The function runs automatically every 5 minutes. View the logs: |
| 70 | +
|
| 71 | +```bash |
| 72 | +# Get function name from stack outputs |
| 73 | +FUNCTION_NAME=$(aws cloudformation describe-stacks \ |
| 74 | + --stack-name <your-stack-name> \ |
| 75 | + --query 'Stacks[0].Outputs[?OutputKey==`ScheduledFunctionName`].OutputValue' \ |
| 76 | + --output text) |
| 77 | +
|
| 78 | +# Tail logs |
| 79 | +aws logs tail /aws/lambda/${FUNCTION_NAME} --follow |
| 80 | +``` |
| 81 | + |
| 82 | +### Test Lambda Execution Failure |
| 83 | + |
| 84 | +Enable failure simulation to test the Lambda Execution DLQ: |
| 85 | + |
| 86 | +```bash |
| 87 | +# Update function to simulate failures |
| 88 | +aws lambda update-function-configuration \ |
| 89 | + --function-name ${FUNCTION_NAME} \ |
| 90 | + --environment 'Variables={LOG_LEVEL=INFO,SIMULATE_FAILURE=true}' |
| 91 | +``` |
| 92 | + |
| 93 | +Wait up to 5 minutes for the next scheduled execution. After Lambda retries are exhausted, check the Lambda Execution DLQ: |
| 94 | + |
| 95 | +```bash |
| 96 | +LAMBDA_DLQ_URL=$(aws cloudformation describe-stacks \ |
| 97 | + --stack-name <your-stack-name> \ |
| 98 | + --query 'Stacks[0].Outputs[?OutputKey==`LambdaExecutionDLQUrl`].OutputValue' \ |
| 99 | + --output text) |
| 100 | + |
| 101 | +aws sqs receive-message --queue-url ${LAMBDA_DLQ_URL} |
| 102 | +``` |
| 103 | + |
| 104 | +Disable failure simulation: |
| 105 | + |
| 106 | +```bash |
| 107 | +aws lambda update-function-configuration \ |
| 108 | + --function-name ${FUNCTION_NAME} \ |
| 109 | + --environment 'Variables={LOG_LEVEL=INFO,SIMULATE_FAILURE=false}' |
| 110 | +``` |
| 111 | + |
| 112 | +### Test EventBridge Scheduler Invocation Failure |
| 113 | + |
| 114 | +Remove Lambda invoke permission to test the EventBridge Scheduler DLQ: |
| 115 | + |
| 116 | +```bash |
| 117 | +# Get schedule name |
| 118 | +SCHEDULE_NAME=$(aws cloudformation describe-stacks \ |
| 119 | + --stack-name <your-stack-name> \ |
| 120 | + --query 'Stacks[0].Outputs[?OutputKey==`ScheduleName`].OutputValue' \ |
| 121 | + --output text) |
| 122 | + |
| 123 | +# Get scheduler role name |
| 124 | +SCHEDULER_ROLE=$(aws cloudformation describe-stack-resources \ |
| 125 | + --stack-name <your-stack-name> \ |
| 126 | + --logical-resource-id SchedulerRole \ |
| 127 | + --query 'StackResources[0].PhysicalResourceId' \ |
| 128 | + --output text) |
| 129 | + |
| 130 | +# Remove Lambda invoke permission |
| 131 | +aws iam delete-role-policy \ |
| 132 | + --role-name ${SCHEDULER_ROLE} \ |
| 133 | + --policy-name InvokeLambda |
| 134 | +``` |
| 135 | + |
| 136 | +Wait up to 5 minutes for the next scheduled execution. After scheduler retries are exhausted, check the Scheduler DLQ: |
| 137 | + |
| 138 | +```bash |
| 139 | +SCHEDULER_DLQ_URL=$(aws cloudformation describe-stacks \ |
| 140 | + --stack-name <your-stack-name> \ |
| 141 | + --query 'Stacks[0].Outputs[?OutputKey==`SchedulerDLQUrl`].OutputValue' \ |
| 142 | + --output text) |
| 143 | + |
| 144 | +aws sqs receive-message --queue-url ${SCHEDULER_DLQ_URL} |
| 145 | +``` |
| 146 | + |
| 147 | +Restore permissions by redeploying: |
| 148 | + |
| 149 | +```bash |
| 150 | +sam deploy |
| 151 | +``` |
| 152 | + |
| 153 | +## Cleanup |
| 154 | + |
| 155 | +1. Delete the stack |
| 156 | + ```bash |
| 157 | + sam delete --stack-name <your-stack-name> |
| 158 | + ``` |
| 159 | +1. Confirm the stack has been deleted |
| 160 | + ```bash |
| 161 | + aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'<your-stack-name>')].StackStatus" |
| 162 | + ``` |
| 163 | +---- |
| 164 | +Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 165 | + |
| 166 | +SPDX-License-Identifier: MIT-0 |
0 commit comments