CDK Destinations Constructs for AWS Lambda
Project description
Amazon Lambda Destinations Library
---AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2.
For more information on how to migrate, see the Migrating to AWS CDK v2 guide.
This library provides constructs for adding destinations to a Lambda function.
Destinations can be added by specifying the onFailure
or onSuccess
props when creating a function or alias.
Destinations
The following destinations are supported
- Lambda function
- SQS queue - Only standard SQS queues are supported for failure destinations, FIFO queues are not supported.
- SNS topic
- EventBridge event bus
Example with a SNS topic for successful invocations:
# An sns topic for successful invocations of a lambda function
import aws_cdk.aws_sns as sns
my_topic = sns.Topic(self, "Topic")
my_fn = lambda_.Function(self, "Fn",
runtime=lambda_.Runtime.NODEJS_14_X,
handler="index.handler",
code=lambda_.Code.from_asset(path.join(__dirname, "lambda-handler")),
# sns topic for successful invocations
on_success=destinations.SnsDestination(my_topic)
)
Example with a SQS queue for unsuccessful invocations:
# An sqs queue for unsuccessful invocations of a lambda function
import aws_cdk.aws_sqs as sqs
dead_letter_queue = sqs.Queue(self, "DeadLetterQueue")
my_fn = lambda_.Function(self, "Fn",
runtime=lambda_.Runtime.NODEJS_14_X,
handler="index.handler",
code=lambda_.Code.from_inline("// your code"),
# sqs queue for unsuccessful invocations
on_failure=destinations.SqsDestination(dead_letter_queue)
)
See also Configuring Destinations for Asynchronous Invocation.
Invocation record
When a lambda function is configured with a destination, an invocation record is created by the Lambda service when the lambda function completes. The invocation record contains the details of the function, its context, and the request and response payloads.
The following example shows the format of the invocation record for a successful invocation:
{
"version": "1.0",
"timestamp": "2019-11-24T23:08:25.651Z",
"requestContext": {
"requestId": "c2a6f2ae-7dbb-4d22-8782-d0485c9877e2",
"functionArn": "arn:aws:lambda:sa-east-1:123456789123:function:event-destinations:$LATEST",
"condition": "Success",
"approximateInvokeCount": 1
},
"requestPayload": {
"Success": true
},
"responseContext": {
"statusCode": 200,
"executedVersion": "$LATEST"
},
"responsePayload": "<data returned by the function here>"
}
In case of failure, the record contains the reason and error object:
{
"version": "1.0",
"timestamp": "2019-11-24T21:52:47.333Z",
"requestContext": {
"requestId": "8ea123e4-1db7-4aca-ad10-d9ca1234c1fd",
"functionArn": "arn:aws:lambda:sa-east-1:123456678912:function:event-destinations:$LATEST",
"condition": "RetriesExhausted",
"approximateInvokeCount": 3
},
"requestPayload": {
"Success": false
},
"responseContext": {
"statusCode": 200,
"executedVersion": "$LATEST",
"functionError": "Handled"
},
"responsePayload": {
"errorMessage": "Failure from event, Success = false, I am failing!",
"errorType": "Error",
"stackTrace": [ "exports.handler (/var/task/index.js:18:18)" ]
}
}
Destination-specific JSON format
- For SNS/SQS (
SnsDestionation
/SqsDestination
), the invocation record JSON is passed as theMessage
to the destination. - For Lambda (
LambdaDestination
), the invocation record JSON is passed as the payload to the function. - For EventBridge (
EventBridgeDestination
), the invocation record JSON is passed as thedetail
in the PutEvents call. The value for the event fieldsource
islambda
, and the value for the event fielddetail-type
is either 'Lambda Function Invocation Result - Success' or 'Lambda Function Invocation Result – Failure', depending on whether the lambda function invocation succeeded or failed. The event fieldresource
contains the function and destination ARNs. See AWS Events for the different event fields.
Auto-extract response payload with lambda destination
The responseOnly
option of LambdaDestination
allows to auto-extract the response payload from the
invocation record:
# Auto-extract response payload with a lambda destination
# destination_fn: lambda.Function
source_fn = lambda_.Function(self, "Source",
runtime=lambda_.Runtime.NODEJS_14_X,
handler="index.handler",
code=lambda_.Code.from_asset(path.join(__dirname, "lambda-handler")),
# auto-extract on success
on_success=destinations.LambdaDestination(destination_fn,
response_only=True
)
)
In the above example, destinationFn
will be invoked with the payload returned by sourceFn
(responsePayload
in the invocation record, not the full record).
When used with onFailure
, the destination function is invoked with the error object returned
by the source function.
Using the responseOnly
option allows to easily chain asynchronous Lambda functions without
having to deal with data extraction in the runtime code.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file aws-cdk.aws-lambda-destinations-1.204.0.tar.gz
.
File metadata
- Download URL: aws-cdk.aws-lambda-destinations-1.204.0.tar.gz
- Upload date:
- Size: 38.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1ffc2c8b2ff38f572e0352838974351a4ac92a513b199f6ee89148eb7a957b0 |
|
MD5 | bcca8997a236fe199a54e68e9da5559a |
|
BLAKE2b-256 | f9b10c2d8da411654d7d4d593c53e70e9e6bedf3cf47d3a970bf04ec2d9538e7 |
File details
Details for the file aws_cdk.aws_lambda_destinations-1.204.0-py3-none-any.whl
.
File metadata
- Download URL: aws_cdk.aws_lambda_destinations-1.204.0-py3-none-any.whl
- Upload date:
- Size: 37.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 893e22dc0af642d5f34924711e2c2a17a939147bfd29663fad6e15133b36f467 |
|
MD5 | 0ce48f2380c415a6a8cab65cdfc77419 |
|
BLAKE2b-256 | 203ab9da0e5fdae2c832bdc9675a796cad3b451d40831e3aaecbfbcfaeea115e |