AWS Python Lambda:如何为步骤函数引发异常

AWS Python Lambda: how to raise Exception for step function

我的 AWS 步骤函数有以下流程,我的 Python lambda 应该如何引发 MyCustomError?

就用raise Exception("MyCustomError")?还是我需要做其他事情? https://docs.aws.amazon.com/step-functions/latest/dg/concepts-error-handling.html 的官方文档使用 node.js 作为示例,我没有看到任何 Python 示例。

{
   "Comment": "A Hello World example of the Amazon States Language using an AWS Lambda function",
   "StartAt": "HelloWorld",
   "States": {
      "HelloWorld": {
         "Type": "Task",
         "Resource": "arn:aws:lambda:us-east-1:123456789012:function:FailFunction",
         "Retry": [ {
            "ErrorEquals": ["MyCustomError"],
            "IntervalSeconds": 1,
            "MaxAttempts": 2,
            "BackoffRate": 2.0
         } ],
      "End": true
      }
   }
}

当我需要捕获并重试我们发出的 API 调用时,我做了一些与此非常相似的事情。在第一次连接到 Aurora Serverless 时,启动集群可能需要 30 秒左右的时间。因此,如果我们遇到超时,我只想抛出一个异常,Step Functions 然后会重试。

Step Function 状态如下所示,等待我的自定义异常与标准 Lambda 异常的方式不同:

"Hello World": {
  "Type": "Task",
  "Resource": "arn:aws:lambda:us-east-1:123456789012:function:FailFunction"
  "Retry": [
    {
      "ErrorEquals": [
        "Lambda.ServiceException",
        "Lambda.AWSLambdaException",
        "Lambda.SdkClientException"
      ],
      "IntervalSeconds": 2,
      "MaxAttempts": 6,
      "BackoffRate": 2
    },
    {
      "ErrorEquals": [
        "QueryAPIUnavailableException"
      ],
      "IntervalSeconds": 30,
      "MaxAttempts": 5,
      "BackoffRate": 2
    }
  ],
  "End": true      
}

然后 Lambda 本身只是对异常子类进行加注,它只是一个 pass:

class QueryAPIUnavailableException(Exception): pass

def lambda_handler(event, context):
    message = my_query_api.get_message()
    if (message == 'Endpoint request timed out'):
        logger.info("Query API timed out, throwing exception for Step Function retry")
        raise QueryAPIUnavailableException(message)
    else:
        print(f"Got back message: {message}")