Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destination only works when Lambda is invoked through AWS CLI

I have a hello-world test Lambda configured with:

  • trigger: API Gateway
  • destination: Amazon SQS. one queue for success, and another for failure.
import json

def lambda_handler(event, context):
    print("Received event: " + json.dumps(event))

    return {
        "statusCode": 200,
        "body": 'success'
    }

When I invoke the Lambda via the CLI, the message gets enqueued to the success queue as expected:

aws lambda invoke --function-name event-destinations --invocation-type Event --payload '{}' response.json

However, when I invoke the Lambda via the API Gateway, no messages are enqueued to either destination queue. I have Lambda Proxy Integration enabled. Cloudwatch metrics confirm that the invocation is successful (Invocations count goes up, Errors count does not). The following returns a 200 and and the expected response body from my Lambda code:

curl 'https://REDACTED.execute-api.us-east-1.amazonaws.com/api_trigger' \
--header 'Content-Type: application/json' \
--data-raw '{}'

Similarly, no messages are enqueued to either destination queue when I use the Test button in the Lambda console. edit: this is expected behavior per https://www.trek10.com/blog/lambda-destinations-what-we-learned-the-hard-way

Why would the destination behavior differ between these 3 invocations? I have set retry attempts to 0 for this test.

like image 903
user3281392 Avatar asked Jan 29 '20 22:01

user3281392


People also ask

What happens when Lambda is invoked?

When you invoke a function, you can choose to invoke it synchronously or asynchronously. With synchronous invocation, you wait for the function to process the event and return a response. With asynchronous invocation, Lambda queues the event for processing and returns a response immediately.

How does Lambda destination work?

AWS Lambda Destinations gives you more visibility and control of function execution results. This helps you build better event-driven applications, reducing code, and using Lambda's native failure handling controls. There are no additional costs for enabling Lambda Destinations.

Which AWS CLI command invokes a Lambda function?

Description. Invokes a Lambda function. You can invoke a function synchronously (and wait for the response), or asynchronously.

Can Lambda run without Trigger?

Pay for the services you use. You don't have to manage the infrastructure. You can scale the service automatically up and down.


1 Answers

It seems there is a set of valid {trigger, destination} pairs, and {API Gateway, SQS} is not one of them. Being able to invoke the lambda from a given trigger is not sufficient to get the event passed along to the destination. AWS console doesn't enforce these pairing or raise warnings.

I referenced the chart from: https://www.trek10.com/blog/lambda-destinations-what-we-learned-the-hard-way/

I added an S3 trigger to my lambda, and the S3 events are published to the destination SQS queues without issue.

like image 83
user3281392 Avatar answered Dec 04 '22 23:12

user3281392