【发布时间】:2020-03-14 17:32:48
【问题描述】:
我的 aws 步进函数工作流程有问题。我已经定义了多个状态,我想为它们添加错误处理。问题是,对于来自函数的任何错误(引发异常、未处理的承诺拒绝或代码错误),状态机总是会获得 LambdaFunctionSucceeded 并继续执行直到最后一个状态完成。
这里是状态机定义的例子,我没有提供我自己的定义,因为这个例子过于复杂
{
"Comment": "Example state machine",
"StartAt": "State1",
"States": {
"State1": {
"Type": "Task",
"Resource": "arn:aws:lambda:eu-west-2:123456789012:function:State1Function",
"Next": "Finish",
"ResultPath": "$.State1Result",
"Catch": [
{
"ErrorEquals": [
"States.TaskFailed"
],
"Next": "Failure"
}
]
},
"Failure": {
"Type": "Fail",
"Error": "$"
},
"Finish": {
"Type": "Task",
"Resource": "arn:aws:lambda:eu-west-2:123456789012:function:FinishFunction",
"End": true,
"Catch": [
{
"ErrorEquals": [
"States.TaskFailed"
],
"Next": "Failure"
}
]
}
}
}
State1Function 示例:
module.exports = async event => {
not().defined; // but execution does not fail
// do stuff
};
我使用sam local start-lambda 和amazon/aws-stepfunctions-local docker 镜像在本地环境中对其进行测试。用于 State1Function 执行的sam 的输出:
2019-11-19T08:37:40.910Z b2421c61-d31c-1482-6a65-f6cbd42328a7 错误调用错误 {"errorType":"ReferenceError","errorMessage":"not is not defined","stack":[ "ReferenceError: not is not defined"," at Runtime.module.exports [as handler] (/var/task/path/to/state1function.js:7:5)"," at Runtime.handleOnce (/var/runtime /Runtime.js:66:25)"]}
amazon/aws-stepfunctions-local 容器(处理状态机执行)的输出:
2019-11-19 08:37:41.348: arn:aws:states:eu-west-2:123456789012:execution:test:test4 : {"Type":"LambdaFunctionSucceeded","PreviousEventId":4," LambdaFunctionSucceededEventDetails":{"Output":"{\"errorType\":\"ReferenceError\",\"errorMessage\":\"not 未定义\"}"}}
执行继续。
当我停止sam local start-lambda 并且状态机无法调用其中一个步进函数时,输出为:
2019-11-19 08:37:53.406: arn:aws:states:eu-west-2:123456789012:execution:test:test4 : {"Type":"LambdaFunctionFailed","PreviousEventId":23," LambdaFunctionFailedEventDetails":{"Error":"Lambda.SdkClientException","Cause":"无法执行 HTTP 请求:目标服务器响应失败"}}
然后执行失败。我希望在阶跃函数错误上有类似的行为。
处理阶跃函数故障的正确方法是什么?
【问题讨论】:
标签: amazon-web-services state-machine aws-step-functions