重试AWS步骤功能中的逻辑

时间:2019-07-24 01:41:25

标签: aws-lambda aws-step-functions

我正在测试步进功能的重试逻辑。 从理论上讲,如果lambda失败,那么应该遵循以下步骤执行3次。

{
  "StartAt": "Bazinga",
  "States": {
    "Bazinga": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": {
        "FunctionName": "arn:aws:lambda:ap-southeast-2:518815385770:function:errorTest:$LATEST",
        "Payload": {
          "Input.$": "$"
        }
      },
      "Retry" : [
        {
          "ErrorEquals": [ "States.All", "States.Timeout" ],
          "IntervalSeconds": 1,
          "MaxAttempts": 3,
          "BackoffRate": 1.0
        }
      ],
       "Next": "Fail"
    },
    "Fail": {
      "Type": "Fail"
    }
  }
}

它调用的lambda设置为在3秒内超时。 λ冻结4秒。这意味着Lambda超时并引发States.Timeout错误。代码如下:

function sleep(ms){
    return new Promise(resolve=>{
        setTimeout(resolve,ms)
    })
}

exports.handler = async (event) => {
    console.log('------------> executing ....')
    await sleep(4000)
};

问题是步骤功能不重试任务。可以从以下CloudWatch日志中确认。


05:59:36
START RequestId: dd1a2ee9-f389-44be-aaa6-07f2ca7983b0 Version: $LATEST

05:59:36
2019-07-24T05:59:36.340Z dd1a2ee9-f389-44be-aaa6-07f2ca7983b0 INFO ------------> executing ....

05:59:39
END RequestId: dd1a2ee9-f389-44be-aaa6-07f2ca7983b0

05:59:39
REPORT RequestId: dd1a2ee9-f389-44be-aaa6-07f2ca7983b0 Duration: 3003.29 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 26 MB

05:59:39
2019-07-24T05:59:39.317Z dd1a2ee9-f389-44be-aaa6-07f2ca7983b0 Task timed out after 3.00 seconds 

不知道出了什么问题。感谢您的任何帮助。

1 个答案:

答案 0 :(得分:0)

要回答我自己的问题,我放置的重试逻辑存在两个问题。

  1. States.All应该是States.ALL(请注意L的情况)
  2. lambda超时时,引发的错误是Lambda.Unknown而不是States.Timeout

我用以下代码更新了step函数,现在它可以工作了:

{
  "StartAt": "Bazinga",
  "States": {
    "Bazinga": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": {
        "FunctionName": "arn:aws:lambda:ap-southeast-2:518815385770:function:errorTest:$LATEST",
        "Payload": {
          "Input.$": "$"
        }
      },
      "Retry" : [
        {
          "ErrorEquals": [ "States.Timeout", "Lambda.Unknown" ],
          "IntervalSeconds": 1,
          "MaxAttempts": 3,
          "BackoffRate": 1.0
        }
      ],
       "Next": "Fail"
    },
    "Fail": {
      "Type": "Fail"
    }
  }
}