简体   繁体   English

目标仅在通过 AWS CLI 调用 Lambda 时有效

[英]Destination only works when Lambda is invoked through AWS CLI

I have a hello-world test Lambda configured with:我有一个 hello-world 测试 Lambda 配置:

  • trigger: API Gateway触发器:API 网关
  • destination: Amazon SQS.目的地: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:当我通过 CLI 调用 Lambda 时,消息按预期排入成功队列:

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.但是,当我通过 API 网关调用 Lambda 时,没有消息排入任一目标队列。 I have Lambda Proxy Integration enabled.我启用了 Lambda 代理集成。 Cloudwatch metrics confirm that the invocation is successful (Invocations count goes up, Errors count does not). Cloudwatch 指标确认调用成功(调用计数增加,错误计数不增加)。 The following returns a 200 and and the expected response body from my Lambda code:以下返回 200 和来自我的 Lambda 代码的预期响应主体:

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.同样,当我使用 Lambda 控制台中的“测试”按钮时,没有消息排队到任一目标队列。 edit: this is expected behavior per https://www.trek10.com/blog/lambda-destinations-what-we-learned-the-hard-way编辑:这是每个https://www.trek10.com/blog/lambda-destinations-what-we-learned-the-hard-way的预期行为

Why would the destination behavior differ between these 3 invocations?为什么这 3 次调用的目标行为会有所不同? I have set retry attempts to 0 for this test.对于此测试,我已将重试次数设置为 0。

It seems there is a set of valid {trigger, destination} pairs, and {API Gateway, SQS} is not one of them.似乎有一组有效的 {trigger, destination} 对,而 {API Gateway, SQS} 不是其中之一。 Being able to invoke the lambda from a given trigger is not sufficient to get the event passed along to the destination.能够从给定的触发器调用 lambda 不足以将事件传递到目的地。 AWS console doesn't enforce these pairing or raise warnings. AWS 控制台不会强制执行这些配对或发出警告。

I referenced the chart from: https://www.trek10.com/blog/lambda-destinations-what-we-learned-the-hard-way/我参考了以下图表: 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.我向我的 lambda 添加了一个 S3 触发器,并且 S3 事件发布到目标 SQS 队列没有问题。

Lambda Destinations are only triggered for asynchronous invocations . Lambda 目标仅针对异步调用触发 In Lambda non-proxy (custom) integration, the backend Lambda function is invoked synchronously by default. Lambda非代理(自定义)集成,默认同步调用后端Lambda function。

You can configure the Lambda function for a Lambda non-proxy integration to be invoked asynchronously by specifying 'Event' as the Lambda invocation type.您可以将 Lambda function 配置为异步调用 Lambda 非代理集成,方法是将“事件”指定为 Lambda 调用类型。 This is done as follows:这是按如下方式完成的:

In Integration Request, add an X-Amz-Invocation-Type header with a static value of 'Event'.在集成请求中,添加 X-Amz-Invocation-Type header,其 static 值为“事件”。

Quoting from here .这里引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM