AWS Lambda Destination Support
Tags: aws, serverless, serverlessframework, lambda
Published: 2022-04-18 15:59:48

Originally posted at [Serverless](https://www.serverless.com/blog/lambda-destinations/) on May 10th, 2020

![](https://cdn-images-1.medium.com/max/5200/0*x109WH64REFebEKK.png)

We first [wrote about](https://serverless.com/blog/november-2019-lambda-releases/) Lambda Destinations when AWS announced support for them right before re:Invent 2019.

Essentially, destinations are the ability for asynchronous Lambda invocations to have their execution results sent to other AWS services without needing to wait for the Lambda execution to finish. Previously, you would have to wait for a Lambda success or failure or need to leverage something like Step Functions. Now, you can invoke functions asynchronously, and send the results of the invocations to different places depending on success or failure.

Today, we’re excited to announce support for Lambda Destinations in the Serverless Framework! Make sure you’ve [upgraded to](https://serverless.com/framework/docs/getting-started/) v1.66.0 or higher and let's look at how to add them.

You can [watch the video](https://www.youtube.com/watch?v=8KBLzRi76Tc) for an abbreviated version or work through the entire guide!

## Adding Lambda Destination

The first step to adding Lambda destinations is to decide what resources you would like to use as a destination on a success or failure. Currently, event destinations can be configured as another Lambda function, an SNS topic, SQS queue, or Amazon EventBridge. You can either create these resources in your serverless.yml file or you can reference already-existing resource ARNs.

Inside of serverless.yml, you'll add a destinations section to a function that you want to configure Event destinations with:
{% gist https://gist.github.com/richardgrantserverless/96c562176ecff1eafb8c5c304872665e.js %}
Inside of the destinations configuration you can add either an onSuccess or onFailure or both. The value for those bits of configuration can either be another function defined in the same serverless.yml file or an ARN of the destination resource.

Now let’s look at a few configuration examples. You can find full-fledged example services of the below snippets on [GitHub here](https://github.com/fernando-mc/serverless-event-destinations).

## Function Event Destinations from the Same Service

One of the easiest ways to configure event destinations with your Lambda functions is to refer to other Lambda functions that you’re already creating in your service in the same serverless.yml file. For example, in the below snippet, the functions section creates a helloStarting function that then uses the helloSuccess and helloFailure functions as destinations:
{% gist https://gist.github.com/richardgrantserverless/2639a20c195021d68b68c9ac69dc474e.js %}
This configuration has the benefit of creating a per-stage destination automatically as you deploy your service across stages like dev and prod.

## ARN-based Destinations

If you already have existing resources for your event destinations, you can also reference them using the ARN value of those destinations. For example:
{% gist https://gist.github.com/richardgrantserverless/9994e9d186817f1c1a7c617ceda8f9e0.js %}
In the above example, we’ve already created an SQS queue called successQueue and a Lambda Function called failureFunction and we've configured our function to deliver success and failure event to each of them, respectively.

There is one potential issue with this sort of configuration though. You may not want to send success and failure notifications for every application stage to the same place. Let’s take a look at at least one way of fixing this.

## Stage-based Destinations

If you want separate destinations depending on the stage of your service you can use a few different methods to accomplish this.

### Framework Pro Parameters

One of the simplest ways to accomplish this would be to leverage [Framework Pro Parameters](https://serverless.com/framework/docs/dashboard/parameters/) and load them in at deployment time:
{% gist https://gist.github.com/richardgrantserverless/49f97573a04b37e1421fc3f62058823d.js %}
This way, the framework will load the relevant parameter at deployment time from [Framework Pro](https://app.serverless.com/).

### Stage Variables in Event Destinations

You can also forego Framework Pro and create destinations resources with the stage as part of their name. For example, a destination queue for success messages in the dev stage might become successQueue-dev and in prod successQueue-prod. Then, you can create the final ARN at deployment time based on the stage:

This sort of configuration assumes you’ve already created all the resources required to configure with the destinations.

## Testing the Destinations

So how would we test one of these examples?

First, clone the examples repo from GitHub [here](https://github.com/fernando-mc/serverless-event-destinations). There are several examples in that repo, but change directories into the arn-based-event-destinations and we'll go from there.

First, we can create a single SQS queue for failure and success events to end up in.

 1. Run aws sqs create-queue --queue-name destinationQueue to create a queue

 2. Then, copy the output queue url into the following command to get the QueueArn: aws sqs get-queue-attributes 
 — queue-url 
 — attribute-names QueueArn```

Now that we have the ARN value, replace the existing onSuccess and onFailure ARNs inside of the serverless.yml file. You should end up with something like this:
{% gist https://gist.github.com/richardgrantserverless/9682e07164f31f0b1c35ac4ac441c066.js %}
From here, we can run serverless deploy to set everything up. When the deployment is finished, we can invoke our function with events that will cause the function to succeed and fail and then review the results in the SQS queue.

Run this a few times to create a few successful events:
{% gist https://gist.github.com/richardgrantserverless/e8e9b705911fd62380325b9991ad75a3.js %}
Then, you can run this one a few times to cause some failures:
{% gist https://gist.github.com/richardgrantserverless/f69102e0afcda20b2f013d3aeca6a232.js %}
If you’ve changed the service or the function name before you deployed you’ll need to make sure to update the --function-name value in the commands.

When you’re done, you can review the results in the queue you created.You’ll need to replace <queueUrl> with your queue URL in the following command:
{% gist https://gist.github.com/richardgrantserverless/414907e493b598c4ec7411ba6b41b750.js %}
If you need the url, you can always run the aws sqs list-queues command. It might take a moment for the messages to make their way to the SQS queue, but when they are there you should see something like this:
{% gist https://gist.github.com/richardgrantserverless/fe347ba16633026cd9a11d5a6eaa222b.js %}
And that’s it! You’ve successfully configured and tested your Event Destinations.

## Now What?

Now that you’ve configured your first event destination you can use them to save yourself from architectures that have to wait for Lambda Functions to finish their invocation and then return a response. This means you can avoid situations where one function is doing nothing, and waiting for the service or function it called to respond. Instead, you can use Event Destinations to process any successes or failures appropriately once the Lambda Function has done it’s job.

If you didn’t have enough fun with the example above, or the [other examples in GitHub](https://github.com/fernando-mc/serverless-event-destinations), there are also plenty of other ways to configure Event Destination resources! You can:

 1. Store ARN values in SSM and pull them in with the SSM variable syntax: ${ssm:paramName}

 2. Reference ARN outputs from other services with [Framework Pro Outputs](https://serverless.com/framework/docs/dashboard/output-variables/) and the outputs variable syntax: ${output:my-service.myOutputKey}

We hope this helps you get started with Event Destinations in the Serverless Framework! Have questions about Event Destinations? [Get in touch](https://twitter.com/fmc_sea) or leave a comment below!

*Originally published at [https://www.serverless.com](https://www.serverless.com/blog/lambda-destinations/).*