Use a new function AWS Lambda “Destination”

Koji Ishida
4 min readNov 28, 2019

New function “Destination” using for controlling Lambda flow according to function results “Success/Failure”

This is awesome update for AWS Lambda.

Introducing AWS Lambda Destinations
https://aws.amazon.com/jp/blogs/compute/introducing-aws-lambda-destinations/

According to the Lambda execution results, we can set next action (AWS services) as a flow.

So let’s try to use.

TL;DR;

  1. The next action can be separated by Success/Failure for Lambda execution results.
  2. Root execution parameters in event is transported to next AWS service as a “requestPayload” attribute.
  3. Transported parameters from a caller Lambda function is defined as a “responsePayload” attribute.

Create a caller Lambda function

Firstly, implement a caller Lambda function like below. (Using Python 3.7)

import json
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)


def destination_sample_handler(event, context):
logger.info("destination sample lambda started.")
if event['Success'] == True:
return {
'statusCode': 200,
'body': event
}
else:
raise Exception('Success is False', event)

When executing, send a parameter “Success” for True/False.

Set a Destination

The AWS blog tests with Amazon SNS and AWS Lambda for the next services of Success/Failure, now I will use Lambda for both type.

You can find “Add destination” button in “Designer”-”Configuration” tag.

New function of “Add destination”

Below is a Success pattern Destination configuration.

Success branch configuration

I just implemented simple code for Success pattern liKe below.

import json
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
logger.info("Success dest lambda started.")
logger.info(event)
return {
'statusCode': 200,
'message': 'Success in dest lambda',
'body': event
}

Below is a Failure pattern Destination configuration.

Failure branch configuration

The implementation is totally same as Success pattern.

import json
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
logger.info("Failure dest lambda started.")
logger.info(event)
return {
'statusCode': 200,
'message': "Failure in dest lambda",
'body': event
}

Execution

Start execution with CLI.

$ aws lambda invoke --function-name destination-sample --invocation-type Event --payload '{ "Success": false }' response.json
{
"StatusCode": 202
}

After success, you can get 202 as Status Code.

Success Pattern

After success, there is a log in CloudWatch Logs.

[INFO]  2019-11-26T22:26:49.792Z    xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx    {'version': '1.0', 'timestamp': '2019-11-26T22:26:49.425Z', 'requestContext': {'requestId': '26fab6f9-a2b7-45f2-bce2-47c612cdb8b7', 'functionArn': 'arn:aws:lambda:us-east-1:000000000000:function:destination-sample:$LATEST', 'condition': 'Success', 'approximateInvokeCount': 1}, 'requestPayload': {'Success': True}, 'responseContext': {'statusCode': 200, 'executedVersion': '$LATEST'}, 'responsePayload': {'statusCode': 200, 'body': {'Success': True}}}

Above log contains event parameter info, and it shows below things.
- Success/Fail information in “condition” attribute.
- Root event contents as a parameter of first Lambda in “requestPayload” attribute.
- Transported event contents as a parameter in “responsePayload” attribute.

After modifying coding to include an element “Message” in event, it shows in “responsePayload”

if event['Success'] == True:
event['Message'] = "Successful finished."
return {
'statusCode': 200,
'body': event
}

CloudWatch Logs

[INFO]  2019-11-26T23:41:21.229Z    xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx    {'version': '1.0', 'timestamp': '2019-11-26T23:41:20.730Z', 'requestContext': {'requestId': 'ba73f7a2-77f4-4166-a95d-d43020e5cc32', 'functionArn': 'arn:aws:lambda:us-east-1:000000000000:function:destination-sample:$LATEST', 'condition': 'Success', 'approximateInvokeCount': 1}, 'requestPayload': {'Success': True}, 'responseContext': {'statusCode': 200, 'executedVersion': '$LATEST'}, 'responsePayload': {'statusCode': 200, 'body': {'Success': True, 'Message': 'Successful finished.'}}}

As trial, when I added one more Destination as Success, previous “On success” configuration was updated. Only one service can be set for one condition.

New destination for one more On success

After adding destination, On success dest function was updated.

Update On success condition with new configuration

Failure pattern

As failure pattern logs, it is similar to success pattern.

[INFO]  2019-11-26T22:30:46.594Z    xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx    {'version': '1.0', 'timestamp': '2019-11-26T22:30:46.203Z', 'requestContext': {'requestId': '6fc2d258-fafb-40a2-8d18-886df8510fa1', 'functionArn': 'arn:aws:lambda:us-east-1:000000000000:function:destination-sample:$LATEST', 'condition': 'RetriesExhausted', 'approximateInvokeCount': 3}, 'requestPayload': {'Success': False}, 'responseContext': {'statusCode': 200, 'executedVersion': '$LATEST', 'functionError': 'Unhandled'}, 'responsePayload': {'errorMessage': "('Success is False', {'Success': False})", 'errorType': 'Exception', 'stackTrace': ['  File "/var/task/lambda_function.py", line 17, in destination_sample_handler\n    raise Exception(\'Success is False\', event)\n']}}

It can include contents of Exception.

X-Ray displaying

In X-ray, it can show every branches and routes.

X-Ray result showing

Conclusion

It was very easy to connect AWS Lambda and Other services (in this time, Lambda). For simple flow of processing can be implemented only this function.

--

--

Koji Ishida

Senior Solution Architect ofAcroquest Technology. CTO of Acroquest Myanmar Technology.