Sub topic 7
Request-response, .sync, and callback patterns
This chapter assumes you can read a basic Amazon States Language (ASL) Task state and have used a service integration such as lambda:invoke. It covers the three integration patterns that decide when a Task state finishes.
The three patterns
A service integration pattern controls how long Step Functions waits after it calls a service. You pick the pattern with a suffix on the Resource ARN.
| Pattern | Resource suffix | Task finishes when | Typical use |
|---|---|---|---|
| Request-response | none | The service returns its HTTP response | Lambda invoke, DynamoDB PutItem, SNS publish |
| Run a job (.sync) | `.sync` | The job reaches a terminal state | Batch jobs, ECS tasks, Glue jobs, nested executions |
| Wait for callback | `.waitForTaskToken` | A worker returns the task token | Human approval, external systems |
Request-response
This is the default. Step Functions calls the API and moves on as soon as it gets the HTTP response. For an asynchronous service, that response only means the request was accepted. For example, sns:publish returns a message ID, not proof that anyone read the message. The lambda:invoke call in the earlier chapters is also request-response, and it waits for the function's result because the default invocation type is synchronous.
Run a job with .sync
Add .sync when the service starts long work and you want the state machine to wait for the outcome. Step Functions starts the job, then tracks it until it succeeds, fails, or times out. You write no polling loop.
{
"StartAt": "RunChildWorkflow",
"States": {
"RunChildWorkflow": {
"Type": "Task",
"Resource": "arn:aws:states:::states:startExecution.sync:2",
"Parameters": {
"StateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:OrderWorkflow",
"Input": {
"orderId.$": "$.orderId"
}
},
"TimeoutSeconds": 3600,
"End": true
}
}
}The :2 suffix on startExecution.sync:2 returns the child output as parsed JSON instead of an escaped string. If the child execution fails, the Task state fails with that error, so your Retry and Catch fields apply as usual.
Step Functions manages the tracking itself, using events or polling depending on the service. To do that, the execution role needs extra permissions, such as EventBridge managed rules. The chapter on execution roles covers this.
Wait for a callback with .waitForTaskToken
Use this pattern when something outside AWS-managed jobs must finish the work. Step Functions generates a unique task token, available as $$.Task.Token in the context object. You pass the token to the external worker and the execution pauses.
{
"StartAt": "WaitForApproval",
"States": {
"WaitForApproval": {
"Type": "Task",
"Resource": "arn:aws:states:::sqs:sendMessage.waitForTaskToken",
"Parameters": {
"QueueUrl": "https://sqs.us-east-1.amazonaws.com/123456789012/approvals",
"MessageBody": {
"orderId.$": "$.orderId",
"taskToken.$": "$$.Task.Token"
}
},
"TimeoutSeconds": 86400,
"End": true
}
}
}A worker that reads the message resumes the execution:
import json
import boto3
sfn = boto3.client("stepfunctions")
def approve(task_token: str, order_id: str) -> None:
sfn.send_task_success(
taskToken=task_token,
output=json.dumps({"orderId": order_id, "approved": True}),
)To report a failure, the worker calls SendTaskFailure with an error name and cause. For long work, it can call SendTaskHeartbeat so the state fails fast if the worker dies.
Common errors
- No timeout on a callback. Without
TimeoutSeconds, a lost token leaves the execution waiting until the one-year Standard workflow limit. Always set a timeout, and addHeartbeatSecondswhen the worker can send heartbeats. - Sending the token as a plain field. The token must come from
$$.Task.Token, so the parameter name needs the.$suffix. - Passing output as an object.
SendTaskSuccessexpectsoutputas a JSON string, so usejson.dumps. - Using callbacks in Express workflows. Express workflows support only request-response. Use Standard for
.syncand.waitForTaskToken. - Assuming request-response means done. An accepted request is not a finished job. Use
.syncwhen you need completion.
Exercise
A state machine starts an AWS Glue job with glue:startJobRun and the next state reads the job's output files. The next state sometimes runs before the files exist. What change fixes this, and why?
Show answer
Change the Resource to arn:aws:states:::glue:startJobRun.sync. Without .sync, the request-response pattern ends the Task as soon as Glue accepts the job. With .sync, Step Functions waits until the job reaches a terminal state, so the next state runs only after the job finishes.
Exercise
Why must a .waitForTaskToken state set TimeoutSeconds, and which two calls can a worker make to finish the state?
Show answer
If the token is lost or the worker never responds, the execution waits until the maximum Standard duration of one year. A timeout makes the state fail with States.Timeout so Retry or Catch can handle it. The worker finishes the state with SendTaskSuccess or SendTaskFailure.