CDK Destinations Constructs for AWS Lambda
Project description
Amazon Lambda Destinations Library
---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
- SNS topic
- EventBridge event bus
Example with a SNS topic for sucessful invocations:
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import aws_cdk.aws_lambda as lambda
import aws_cdk.aws_lambda_destinations as destinations
import aws_cdk.aws_sns as sns
my_topic = sns.Topic(self, "Topic")
my_fn = lambda.Function(self, "Fn",
# other props
on_success=destinations.SnsDestination(my_topic)
)
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 theMessageto 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 thedetailin the PutEvents call. The value for the event fieldsourceislambda, and the value for the event fielddetail-typeis either 'Lambda Function Invocation Result - Success' or 'Lambda Function Invocation Result – Failure', depending on whether the lambda function invocation succeeded or failed. The event fieldresourcecontains 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:
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import aws_cdk.aws_lambda as lambda
import aws_cdk.aws_lambda_destinations as destinations
destination_fn = lambda.Function(self, "Destination")
source_fn = lambda.Function(self, "Source",
# other props
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aws-cdk.aws-lambda-destinations-1.52.0.tar.gz.
File metadata
- Download URL: aws-cdk.aws-lambda-destinations-1.52.0.tar.gz
- Upload date:
- Size: 38.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3787db5126577d0b89e514c7d326fddeb2d6bbdbf80797a0a81988d36ab0505
|
|
| MD5 |
78635cfa6440b0645b1e881b92c0eed0
|
|
| BLAKE2b-256 |
2c67ab981c5089e7d8a2afd6079d5d360b0165ee59064b70e45426e5d0a29eb4
|
File details
Details for the file aws_cdk.aws_lambda_destinations-1.52.0-py3-none-any.whl.
File metadata
- Download URL: aws_cdk.aws_lambda_destinations-1.52.0-py3-none-any.whl
- Upload date:
- Size: 36.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e189fc209da2ed3c4c7f814c2adb48b32fd2a401efdd3baa7a59659ae98f573
|
|
| MD5 |
dcca5e4153b63d72ed9a83c7be251140
|
|
| BLAKE2b-256 |
ed811fb4598f867e35c6c26d36a72ebaba60d63025bbaf19859a59e75410cfbd
|