Sub topic 3
Doing work with Task states
This chapter assumes you can read a basic Amazon States Language (ASL) definition, you know what a Lambda function is, and you understand retries and timeouts. Here you will learn how a Task state calls a Lambda function and what it returns, and which fields control that call.
What a Task state does
A Task state is a state that does one unit of work. A state machine uses Task states for every action that is not pure flow control. The work can be a Lambda invocation, a call to another AWS service, or an activity worker that you run yourself.
Every Task state has a Type of Task and a Resource. The Resource is an ARN that tells Step Functions what to call. Use the optimized Lambda integration, arn:aws:states:::lambda:invoke, rather than the bare function ARN. The optimized form lets you pass parameters, and Step Functions retries a few Lambda service errors for you.
A Task state that calls Lambda
The following state machine has one Task state. It runs and finishes as it stands, provided the function exists in your account.
{
"StartAt": "ValidateOrder",
"States": {
"ValidateOrder": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "arn:aws:lambda:us-east-1:123456789012:function:ValidateOrder:$LATEST",
"Payload.$": "$"
},
"ResultSelector": {
"validation.$": "$.Payload"
},
"ResultPath": "$.result",
"TimeoutSeconds": 30,
"End": true
}
}
}Read the fields in the order Step Functions applies them.
Parametersbuilds the request. The keyPayload.$ends in.$, so its value is a JSONPath expression and not a literal."$"sends the entire state input to the function.- The service call runs. The optimized integration returns an object that wraps your function's return value in a
Payloadfield. It also adds metadata such asStatusCode. ResultSelectortrims that object. Here it keeps onlyPayloadand renames itvalidation.ResultPathdecides where the trimmed result goes.$.resultputs it under a new key and keeps the original input. If you leaveResultPathout, the result replaces the whole input.TimeoutSecondsfails the task withStates.Timeoutif it runs longer than 30 seconds.
If the input is {"orderId": "1001"} and the function returns {"valid": true}, the state's output is:
{
"orderId": "1001",
"result": {
"validation": { "valid": true }
}
}The next state can now test $.result.validation.valid, and it still has orderId.
Retry and Catch on a Task
A Task state is also where you attach Retry and Catch. Both are fields of the Task state itself.
Retryre-runs the task when its error name matchesErrorEquals.IntervalSeconds,MaxAttempts, andBackoffRateset the delay. An interval of 2 and a rate of 2.0 waits 2, then 4, then 8 seconds.Catchruns after retries are used up, or when no retrier matches. It sends the execution to the state named inNext. ItsResultPathcontrols where the error object is stored, so use$.errorto keep the original input.
Step Functions checks Retry first and Catch second. Chapter 8 covers the details.
Common mistakes
- Forgetting the
.$suffix."Payload": "$"sends the literal string$, not your input. - Reading
$.validationwhen the Lambda output sits under$.Payload. Without aResultSelector, the function's return value is at$.Payload. - Leaving out
TimeoutSeconds. A stuck task can then wait far longer than you intend. - Using the same
ResultPathfor the result and for the error. One overwrites the other.
Exercise
A Task state has "ResultSelector": {"validation.$": "$.Payload"} and "ResultPath": "$.result". The input is {"orderId": "7"} and the Lambda function returns {"valid": false}. What is the state's output?
Show answer
{"orderId": "7", "result": {"validation": {"valid": false}}}. ResultSelector takes Payload from the integration response and renames it validation. ResultPath places that object under $.result and keeps the original input.