What Are Lambda Destinations And How To Use Them?
How to dynamically route results for successful or failed outputs.
Did you know Lambda provides a powerful way to route the results of your function invocations based on their execution?
Lambda destinations allow you to configure your functions output to various AWS services based on whether the function code will succeed or fail.
Destinations enhance error handling and post-processing for you by letting you specify different outputs of function executions.
How Destinations Work
When you invoke a Lambda function asynchronously — such as through an S3 bucket object upload or directly with an Event — you can configure a destination for both the successful and failed cases.
A destination is allowed to consist of the following AWS services:
An Amazon SQS message queue for further processing
An Amazon SNS topic to notify a subscriber (email or in-app notification)
Route events to other AWS services based on an AWS EventBridge rule.
Another AWS Lambda function for further processing (e.g. resizing an image)
Demo Implementation
Let’s create a Lambda function with a destination to an SNS topic if the execution succeeds, and an SQS message queue if it fails.
Create a new Lambda function with the Node JS 20.x runtime.
We’ll write some very simple code to try this.
exports.handler = async (event) => {
try {
console.log("Event:", JSON.stringify(event, null, 2));
const response = {
statusCode: 200,
body: JSON.stringify('Successful file upload'),
};
return response;
}
catch(error) {
throw new Error("file upload failure.");
}
};
We add a try and catch block — the try will lead to a success destination (for brevity’s sake, let’s assume this will always be the case) and the catch will lead to a failed destination.
Now we can add Destinations to this Lambda function. Click on the + Add Destination button as shown below.
Let’s add a Destination to an SNS topic on success:
Then, do the same with an SQS message queue on function failure.
Testing
You can now test the Destinations to see if a function success will send a message to your SNS topic and a failure to your SQS message queue.
In the Code section of your Lambda function, run a test by clicking the Test button at the top. Use some dummy JSON values — one test with correct values and one with faulty values to trigger both SQS and SNS destinations.
Practical Use Cases
Some useful situations for Lambda destinations include:
Error notifications: With Destinations you can create enhanced error-handling pipelines and alert an incident management system when an error occurs.
Retry logic: sending failures to an SQS queue can be used to trigger the Lambda function to retry the operation.
Chaining Lambda functions: Another Lambda function can be triggered from a destination as part of a multipart processing workflow.
I’m creating a free DynamoDB email course, if you would like to learn the basics of DynamoDB and efficient data modelling, you can sign up free here:
https://www.buildawaitlist.com/waitlist/dynamodb-course
Conclusion
Lambda Destinations provide a flexible and powerful way to handle different outcomes of your Lambda function invocations based on success or failure states.
This makes your serverless applications more resilient and offers a more robust method of handling function execution results.
👋 My name is Uriel Bitton and I’m committed to helping you master Serverless, Cloud Computing, and AWS.
🚀 If you want to learn how to build serverless, scalable, and resilient applications, you can also follow me on Linkedin for valuable daily posts.
Thanks for reading and see you in the next one!