Understanding Amazon Lambda Destinations
AWS Lambda Destination provides visibility into Lambda function asynchronous execution by routing success/failure invocation results to different AWS services such as SNS, SQS etc. With Destinations, asynchronous function results are routed as an execution record to a destination resource without writing additional code. An execution record contains details about the request and response in JSON format including version, timestamp, request context, request payload, response context, and response payload.
In this exercise, you create a Lambda function dojolambda which is invoked asynchronous from Amazon SNS by publishing a message to SNS topic dojotopic. The successful execution records are routed to Amazon SQS queue successqueue and failed execution records are routed to Amazon SQS queue failurequeue.
Step1: Pre-Requisite
You need to have an AWS account with administrative access to complete the exercise. If you don’t have an AWS account, kindly use the link to create free trial account for AWS.
Step2: Create IAM Role
You start with creation of the IAM role which AWS Lambda function uses for the authorization to call other AWS Services.
-
Login to the AWS Console. Select an AWS Region of your choice. You will see the exercise using the Paris region.
-
Goto the IAM Management console and click on the Roles menu in the left and then click on the Create role button.
-
On the next screen, select Lambda as the service and click on the Next: Permissions button.
-
On the next screen, select PowerUserAccess as the policy and click on the Next: Tags button. The exercise is using power user permission but in actual production use it is recommended to use minimum required permission only.
-
On the next screen, click on the Next: Review button.
-
On the next screen, type in dojolambdarole for the Role name and click on the Create role button.
-
The role is created in no time. The next step is to create two queues successqueue and failurequeue in Amazon SQS.
Step3: Create Queues
You will create two queues successqueue and failurequeue in Amazon SQS which are used to handle successful and failed Lambda executions.
-
Goto Amazon SQS Management console and click on the Create queue.
-
On the next screen, select Standard as the type. Type in successqueue as the name. Keep rest of the configuration as the default and then click on the Create queue button.
-
The queue is created in no time. Repeat step 1 and 2 to create another similar queue with the name failurequeue. There are now two queues created.
-
The next step is to create a Lambda function and configure the queues as the destination.
Step4: Create Lambda Function
You create a Lambda function which is configured with the destinations.
-
Goto Lambda Management console and click on the Create function button.
-
On the next screen, select Author from scratch as the option. Type in dojolambda as the Function name. Select Python 3.8 as the Runtime. Under Permissions, select Use an existing role as the option and then select dojolambdarole (you created in the earlier steps) as the role. Finally, click on the Create function button.
-
The function is created in no time. You now configure the destinations for the Lambda function. In the Designer setting area, click on the + Add destination button.
-
On the next screen, select Asynchronous invocation as the source. Select On failure as the condition. Select SQS Queue as the Destination type. Finally select failurequeue as the destination and click on the Save button.
-
The destination is created. Click on the + Add destination button again. On the next screen, select Asynchronous invocation as the source. Select On success as the condition. Select SQS Queue as the Destination type. Finally select successqueue as the destination and click on the Save button.
-
Both success and failure destination are now configured for the Lambda function.
-
With destination configuration is place, let’s update the code of the Lambda function. Goto the Function code setting for the lambda function and replace the code with the following code below.
import json
def lambda_handler(event, context):
result = 0;
msg = event['Records'][0]['Sns']['Message']
if (msg=="success"):
result = 1;
else:
result = 1/0;
return {
'statusCode': 200,
'body': json.dumps(result)
}
-
In the code above, when the function is called asynchronous; if the message passed is success string, it returns 1 and if the message passed is anything else; it tries to divide 1 by 0 to throw divide by zero error and the let Lambda function execution fail. Click on the Save button to upload the updated code. Note: please use the save button which saves the overall Lambda function.
-
The lambda function code and configuration is ready. Next step is to configure SNS topic and add Lambda as the subscriber. This way - you can publish a message to the SNS topic to invoke the lambda function asynchronous.
Step5: Create the SNS Topic
Create the Amazon SNS topic which will use Lambda as one of the subscription to call the Lambda function asynchronous.
-
Goto AWS SNS console, click on the Topics menu in the left and then click on Create topic button.
-
On the next screen, type in dojotopic as then name and keep the rest of the configuration as the default. Click on the Create topic button.
-
The topic is created. Click on the Create subscription button next.
-
On the next screen, select AWS Lambda as the protocol and select dojolambda as the endpoint. Click on the Create subscription button.
-
The SNS Topic and the Lambda subscription is ready.
-
It is time to test both success and failed executions.
Step6: Invoke Lambda Function Asynchronous
You publish message to dojotopic so that it invokes Lambda function asynchronously.
-
Goto SNS console, open dojotopic topic details and then click on the Publish message button.
-
On the next screen, type in success in the Message body to send to the endpoint field. Click on the Publish message button.
-
The message is published. The Lambda function is called asynchronously and the message is passed as the payload.
-
Repeat the steps 1 to 3 to publish another message error in the Message body to send to the endpoint field.
-
Both messages will result in calling Lambda function asynchronously. One will result in successful execution while the other will fail. You can check them in the destination queues. It might take some time for the execution results to be routed to the destination queues.
-
Goto AWS SQS console, open queue list and you can see 1 message (execution result) published to each of the queue. The message in failurequeue is for the failed Lambda execution and the message in the successqueue is for the successful Lambda execution.
-
You can poll messages in each queues to see the message details.
Successful Execution Message
Failed Execution Message
-
That finishes the exercise. You saw success and failed Lambda execution handling using destination. The next step is to clean-up the resources so that you don’t incur cost after the exercise.
Step7: Clean up
Delete failurequeue and successqueue queues in the AWS SQS Console.
Delete dojotopic topic and associated Lambda function subscription in the AWS SNS Console.
Delete dojolambda function in the AWS Lambda console.
Finally delete dojolambdarole IAM role from the IAM Management console.
Thanks and hope you enjoyed the exercise.