简体   繁体   English

使用“事件”调用类型时未调用 AWS Lambda

[英]AWS Lambda not invoked when using 'Event' invocation type

I have 2 lambdas, test1 which invokes test2 .我有 2 个 lambda, test1调用test2 I want to invoke test2 asynchronously with the "fire and forget" mentality.我想以“即发即忘”的心态异步调用test2

Below is the code for both lambdas.下面是两个 lambda 表达式的代码。 When I set the InvocationType to RequestResponse , the function gets invoked and there is a log in CloudWatch.当我将InvocationType设置为RequestResponse ,该函数被调用并且在 CloudWatch 中有一个日志。 When I change the InvocationType to Event , there is no record in CloudWatch of the function being invoked.当我将InvocationType更改为Event ,CloudWatch 中没有被调用函数的记录。

test1:测试1:

const AWS = require('aws-sdk');
const lambda = new AWS.Lambda();

exports.handler = async (event) => {
    const params = {
        'FunctionName': 'test2',
        'InvocationType': 'Event',
        'Payload': JSON.stringify('Hello')
    };

    const response = await lambda.invoke(params).promise();
    return response;
};

test2:测试2:

exports.handler = async (event) => {
    console.log(event);
    return 'Success';
};

The IAM policy includes invoke permissions (otherwise the RequestResponse type wouldn't work either). IAM 策略包括调用权限(否则RequestResponse类型也不起作用)。 I've also read about limits on the size of the args you can pass , but in this case I'm only passing "Hello"...what gives?我还阅读了有关您可以传递的参数大小的限制,但在这种情况下,我只传递了“你好”......什么给出了?

EDIT: I should also add that in every case I get a response code of 202, which indicates a successful invoke, I just see no evidence of the function being executed in the logs.编辑:我还应该补充一点,在每种情况下,我都会收到 202 的响应代码,这表明调用成功,我只是在日志中没有看到正在执行的函数的证据。

EDIT 2: I've added await to the end of test1 , but still do not see any indication that test2 was invoked in CloudWatch.编辑 2:我已将await添加到test1的末尾,但仍然没有看到任何迹象表明在 CloudWatch 中调用了 test2。

I think there is just await missing:我认为只是await丢失:

exports.handler = async (event) => {
    const params = {
        'FunctionName': 'test2',
        'InvocationType': 'Event',
        'Payload': JSON.stringify('Hello')
    };

    await lambda.invoke(params).promise();
};

Without await a promise from lambda.invoke(params).promise() exites the lambda execution without resolving that promise.await来自lambda.invoke(params).promise()的承诺lambda.invoke(params).promise()退出 lambda 执行而不解决该承诺。

Because the lambda is called asynchronously there is no result returned.因为 lambda 是异步调用的,所以没有返回结果。

You can check the CloudWatch log of the test2 lambda to see if has been invoked.您可以查看test2 lambda 的 CloudWatch 日志以查看是否已被调用。

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

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