Example project with async lambda and dynamo db
- Step function invoke lambda with the wiatforTaskToken method
- The async lambda can get the task token from event
- When async process finishes, the last step will be calling step function api to sendTaskSccess/fail/heartbeat with token from step 1
- Step function will continue to the next state
- A good idea will be configure a heartbeat/timeout method so it will not wait for function forever.
AWS Step Functions can coordinate asynchronous Lambda functions by using the Wait for Task Token
API. When you invoke a Lambda function asynchronously, Step Functions returns a task token. You can then use the Wait for Task Token
API to wait for the Lambda function to complete.
The following is an example of how to coordinate asynchronous Lambda functions using Step Functions:
- Create a Step Functions state machine that has two states:
- A
Start Lambda
state that invokes the Lambda function asynchronously. - A
Wait for Task Token
state that waits for the Lambda function to complete.
- A
- In the
Start Lambda
state, specify the Lambda function that you want to invoke and the task token that you want to use. - In the
Wait for Task Token
state, specify the task token that you received from theStart Lambda
state.
When the Step Functions execution reaches the Start Lambda
state, it will invoke the Lambda function asynchronously. The Lambda function will then execute and return a task token. The Step Functions execution will then move to the Wait for Task Token
state. The Wait for Task Token
state will wait for the Lambda function to complete and return the task token. When the task token is returned, the Wait for Task Token
state will complete and the Step Functions execution will continue.
Here is an example of the Amazon States Language (ASL) code for the Step Functions state machine:
StartLambda:
"StartAt":"GetManualReview",
"States":{
"GetManualReview":{
"Type":"Task",
"Resource":"arn:aws:states:::lambda:invoke.waitForTaskToken",
"Parameters":{
"FunctionName":"arn:aws:lambda:us-east-1:123456789012:function:get-model-review-decision",
"Payload":{
"model.$":"$.new_model",
"token.$":"$$.Task.Token"
},
"Qualifier":"prod-v1"
},
"End":true
}
}
}
When you invoke a Lambda function, the execution will wait for the function to complete. If you invoke the Lambda function with a callback task, the heartbeat timeout doesn’t start counting until after the Lambda function has completed executing and returned a result. As long as the Lambda function executes, the heartbeat timeout is not enforced.
"MyTaskToken.$": "$$.Task.Token"