【问题标题】:Invoking AWS Lambda from Another lambda, child lambda stop immediately从另一个 lambda 调用 AWS Lambda,子 lambda 立即停止
【发布时间】:2021-11-04 03:11:30
【问题描述】:

是的,我看过很多关于如何从另一个 lambda 异步调用 lambda 的帖子和 stackoverflow 答案。我可以从父 lambda 调用子 lambda。但不知道为什么我的子 lambda 函数会在 100 毫秒内立即关闭,看起来我的子 lambda 正在与我的父 lambda 同步工作。

这是我的父 lambda 具有 HttpAPI 类型网关:

// PARENT LAMBDA having HttpAPI gateway.

const AWS = require("aws-sdk");
const auth = require("./services/auth");
var Lambda = new AWS.Lambda();

/**
* Note: Step Functions, which are called out in many answers online, do NOT actually work in this case. The reason
* being that if you use Sequential or even Parallel steps they both require everything to complete before a response
* is sent. That means that this one will execute quickly but Step Functions will still wait on the other one to
* complete, thus defeating the purpose.
*
* @param {Object} event The Event from Lambda
*/
// LAMBDA A
exports.handler = async (event, context, callback) => {
user = auth.authenticateUser(event);
const userId = auth.authenticateUser(event);
if (!user) {
return auth.UNAUTHORISED;
}
let params = {
FunctionName: "my-child-lambda-here",
InvocationType: "Event", // <--- This is KEY as it tells Lambda to start execution but immediately return / not wait.
Payload: JSON.stringify(event),
};

// we have to wait for it to at least be submitted. Otherwise Lambda runs too fast and will return before
// the Lambda can be submitted to the backend queue for execution
// LAMBDA B
await new Promise((resolve, reject) => {
Lambda.invoke(params, function (err, data) {
if (err) {
reject(err, err.stack);
} else {
console.log(data);
resolve("Lambda invoked: " + data);
}
});
});
// Always return 200 not matter what
return {
statusCode: 200,
body: "Your request is currently being processed",
};
};

这是我的孩子 lambda:

// CHILD LAMBDA!!
exports.handler = async (event) => {
console.log("inside child Lambda!!!!!!!!!!!")
console.log(event)
let i = 0;
// repeat with the interval of 1 seconds
let timerId = setInterval(() => console.log("count: ", i++), 1000);
console.log("inside child Lambda print 2!!!!!!!!!!!")
setTimeout(() => console.log("setTimoutTesting"), 10);

// after 15 seconds stop
setTimeout(() => { clearInterval(timerId); console.log("stop"); }, 15000);

// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};

我的 yaml 设置了以下 iam 以允许另一个 lambda 调用:

provider:
name: aws
memorySize: 128
runtime: nodejs14.x
lambdaHashingVersion: 20201221
region: us-east-2
iam:
role:
name: invoke-other-lambda-role
statements:
- Effect: 'Allow'
# Resource: '*'
Resource: 'arn:aws:lambda:*:*:function:*'
Action:
- 'lambda:InvokeAsync'
- 'lambda:InvokeFunction'

【问题讨论】:

  • 您为什么希望您的孩子 lambda 等待更长时间?因为超时的东西?

标签: amazon-web-services aws-lambda


【解决方案1】:

我不是 node.js 方面的专家,但据我所知,lambda 处理程序实际上并没有等到 setTimeout() 完成。如果您想真正等待超时发生,您应该在同一函数中将其转换为 Promise 对象和 await 。一个很好的例子可以在这里找到(代码 sn-p 应该是不言自明的):https://stackoverflow.com/a/42183439/12259756

我的问题是,为什么您首先要这样做,因为与其他技术相比,对于这种等待 15 秒的用例,lambda 提供的服务非常昂贵。例如,在您的逻辑中包含等待的一种廉价方法是 Step Function。

【讨论】:

    猜你喜欢
    • 2016-12-31
    • 2018-08-21
    • 2018-06-09
    • 1970-01-01
    • 2019-05-22
    • 2017-01-01
    • 2021-05-12
    相关资源
    最近更新 更多