ken*_*ken 2 amazon-web-services aws-lambda aws-step-functions
我有 Lambda 函数tranportKickoff,它接收输入,然后将输入发送/代理到Step Function中。下面的代码确实运行,我没有收到任何错误,但同时步骤函数没有执行。
对于设计也很重要,我不希望函数transportKickoff等待步骤函数完成,因为它可能运行很长时间。然而,我希望调用Step Function 时出现的任何错误都会同步报告回来。也许这个想法是错误的,我不知何故错过了某个地方抛出的错误。但是,如果是这种情况,我想找到一种方法,能够实现在 Step Function 开始执行后立即退出 lambda 函数的目标。
注意:我可以独立执行步骤函数,并且我知道它工作正常
const stepFn = new StepFunctions({ apiVersion: "2016-11-23" });
const stage = process.env.AWS_STAGE;
const name = `transport-steps ${message.command} for "${stage}" environment at ${Date.now()}`;
const params: StepFunctions.StartExecutionInput = {
stateMachineArn: `arn:aws:states:us-east-1:999999999:stateMachine:transportion-${stage}-steps`,
input: JSON.stringify(message),
name
};
const request = stepFn.startExecution(params);
request.send();
console.info(
`startExecution request for step function was sent, context sent was:\n`,
JSON.stringify(params, null, 2)
);
callback(null, {
statusCode: 200
});
Run Code Online (Sandbox Code Playgroud)
我还从控制台检查我是否拥有我认为开始执行步骤函数的正确权限:
我现在添加了更多权限(见下文),但仍然遇到同样的问题:
好吧,我自己已经解决了这个问题,希望这个答案对其他人有帮助:
首先,该send()方法不是同步调用,但它也不返回 Promise。相反,您必须在发送之前在对象上设置侦听器Request,以便可以适当地响应成功/失败状态。
我已经使用以下代码完成了此操作:
const stepFn = new StepFunctions({ apiVersion: "2016-11-23" });
const stage = process.env.AWS_STAGE;
const name = `${message.command}-${message.upc}-${message.accountName}-${stage}-${Date.now()}`;
const params: StepFunctions.StartExecutionInput = {
stateMachineArn: `arn:aws:states:us-east-1:837955377040:stateMachine:transportation-${stage}-steps`,
input: JSON.stringify(message),
name
};
const request = stepFn.startExecution(params);
// listen for success
request.on("extractData", req => {
console.info(
`startExecution request for step function was sent and validated, context sent was:\n`,
JSON.stringify(params, null, 2)
);
callback(null, {
statusCode: 200
});
});
// listen for error
request.on("error", (err, response) => {
console.warn(
`There was an error -- ${err.message} [${err.code}, ${
err.statusCode
}] -- that blocked the kickoff of the ${message.command} ITMS command for ${
message.upc
} UPC, ${message.accountName} account.`
);
callback(err.statusCode, {
message: err.message,
errors: [err]
});
});
// send request
request.send();
Run Code Online (Sandbox Code Playgroud)
现在请记住有一个“成功”事件,但我使用“extractData”来捕获成功,因为我想尽快得到响应。成功也可能同样有效,但查看 Typescript 类型中的语言并不完全清楚,在我的测试中,我确信“extractData”方法确实按预期工作。
至于为什么我没有在我的步骤函数上得到任何执行......它必须按照我命名函数的方式......你仅限于名称中的字符子集,并且我已经超越了该限制,但是直到我能够使用上面的代码捕获错误时才意识到。