Sub topic 2
Moving Data Between States
Assume you know JSON, basic IAM, Lambda, and retry and timeout concepts. In Step Functions, each state receives JSON input and produces JSON output. Use these fields to control the data:
InputPathselects data before the state runs.Parametersbuilds the request sent to the service.ResultSelectorreshapes the service result.ResultPathplaces that result into the state input.OutputPathselects what the state passes onward.
The following definition invokes Lambda, keeps only its useful response, and stores it under validation.
{
"StartAt": "ValidateOrder",
"States": {
"ValidateOrder": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "arn:aws:lambda:us-east-1:123456789012:function:ValidateOrder",
"Payload.$": "$.order"
},
"ResultSelector": {
"valid.$": "$.Payload.valid"
},
"ResultPath": "$.validation",
"OutputPath": "$",
"Retry": [
{
"ErrorEquals": ["Lambda.ServiceException", "Lambda.TooManyRequestsException"],
"IntervalSeconds": 2,
"MaxAttempts": 3,
"BackoffRate": 2
}
],
"Catch": [
{
"ErrorEquals": ["States.ALL"],
"ResultPath": "$.error",
"Next": "Failed"
}
],
"Next": "Check"
},
"Check": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.validation.valid",
"BooleanEquals": true,
"Next": "Succeeded"
}
],
"Default": "Failed"
},
"Succeeded": { "Type": "Succeed" },
"Failed": { "Type": "Fail" }
}
}Calling AWS services directly
A Task state can use an optimized integration, such as arn:aws:states:::lambda:invoke, or an AWS SDK integration, such as arn:aws:states:::aws-sdk:dynamodb:putItem. Direct integrations avoid writing a wrapper Lambda. The state machine execution role must allow the exact API action on the required resource.
Integration patterns
The default request-response pattern waits for the API response and continues. Add .sync when a supported integration should wait for a service job to finish. Add .waitForTaskToken when an outside worker must resume the workflow later with SendTaskSuccess or SendTaskFailure.
Handling failures
Retry handles known transient errors with bounded exponential backoff. Catch handles errors that remain or are permanent. Put specific errors before States.ALL, and preserve error details with ResultPath for later diagnosis.
Exercise
Where should a Lambda response be stored if the next state needs it at $.validation?
Show answer
Set ResultPath to "$.validation", optionally using ResultSelector to remove irrelevant fields.