A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda_destinations-readme.html below:

aws-cdk-lib.aws_lambda_destinations module · AWS CDK

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

Example with a SNS topic for successful invocations:


import * as sns from 'aws-cdk-lib/aws-sns';

const myTopic = new sns.Topic(this, 'Topic');

const myFn = new lambda.Function(this, 'Fn', {
  runtime: lambda.Runtime.NODEJS_LATEST,
  handler: 'index.handler',
  code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
  
  onSuccess: new destinations.SnsDestination(myTopic),
})

Example not in your language?

Example with a SQS queue for unsuccessful invocations:


import * as sqs from 'aws-cdk-lib/aws-sqs';

const deadLetterQueue = new sqs.Queue(this, 'DeadLetterQueue');

const myFn = new lambda.Function(this, 'Fn', {
  runtime: lambda.Runtime.NODEJS_LATEST,
  handler: 'index.handler',
  code: lambda.Code.fromInline('// your code'),
  
  onFailure: new destinations.SqsDestination(deadLetterQueue),
});

Example not in your language?

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>"
}

Example not in your language?

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)" ]
  }
}

Example not in your language?

Destination-specific JSON format Auto-extract response payload with lambda destination

The responseOnly option of LambdaDestination allows to auto-extract the response payload from the invocation record:


declare const destinationFn: lambda.Function;

const sourceFn = new lambda.Function(this, 'Source', {
  runtime: lambda.Runtime.NODEJS_LATEST,
  handler: 'index.handler',
  code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
  
  onSuccess: new destinations.LambdaDestination(destinationFn, {
    responseOnly: true,
  }),
})

Example not in your language?

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.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4