[英]AWS Python Lambda: how to raise Exception for step function
我的 AWS 步驟 function 有以下流程,我的 Python lambda 應該如何引發 MyCustomError?
只需使用raise Exception("MyCustomError")
? 還是我需要做點別的? The official doc at https://docs.aws.amazon.com/step-functions/latest/dg/concepts-error-handling.html use node.js as example, and I don't see any Python examples.
{
"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 然后會重試。
步驟 Function state 看起來像這樣,與標准 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}")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.