问题描述
我的 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 本身只是对一个 Exception 子类进行了加注,该子类只是一个 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}")