# Get the task token from the event
task_token = event['taskToken']
state_machine_arn = event['stateMachineArn']
# Check if maintenance is active
if maintenance_flag.lower() == 'true':
# Store the task token in S3
s3_key = f"{prefix}/{task_token}.json"
store_token_in_s3(bucket, s3_key, <REDACTED>)
print(f"Maintenance is active, task token stored at {s3_key}")
return {"status": "Maintenance mode, task token stored"}
# Check for existing tokens in S3
token_list = <REDACTED>, prefix)
if token_list:
# Resume StepFunctions for each stored token
for token in token_list:
try:
sf_client.send_task_success(taskToken=token, output=json.dumps({"status": "success"}))
print(f"Resumed task with token: {token}")
delete_token_from_s3(bucket, f"{prefix}/{token}.json")
except ClientError as e:
print(f"Error resuming task with token: {token} - {e}")
# Send success for the current task token
sf_client.send_task_success(taskToken=task_token, output=json.dumps({"status": "success"}))
print(f"Task succeeded for token: {task_token}")
return {"status": "Task succeeded"}
結論から言うと、Lambda関数から自身に対して成功のコールバック(send_task_success)を返すことは可能ですし、正常に動作します。具体的には、StepFunctionsのTaskステートにおける「Wait for Task Token」(待機タスクトークン)の動作は、StepFunctionsがタスクを一時停止して外部のシステム(今回の場合はLambda関数など)が指定されたタイミングでタスクの完了を知らせるという仕組みです。
仕組みの確認:
Wait for : StepFunctionsのTaskステートがタスクトークンを生成し、Lambdaに渡されます。この時点でStepFunctionsはトークンを受け取るまで待機状態になります。