Sub topic 12
Debugging with execution history, logs, and X-Ray
This chapter assumes you can write a state machine with Task, Choice, Retry, and Catch, and that you know how IAM roles and CloudWatch Logs work. It covers three tools for finding out why an execution failed or ran slowly.
Execution history
Every Standard workflow execution records an ordered list of events. Each event has an ID, a timestamp, a type, and details. For one Task state you typically see these events in order:
TaskStateEntered, which includes the state's input.TaskScheduledandTaskStarted.TaskSucceededorTaskFailed, which includes the output or the error and cause.TaskStateExited, which includes the final output afterResultPathandOutputPath.
Comparing the TaskStateEntered input with the TaskStateExited output shows where data was lost or reshaped. This is the fastest way to find a bad ResultPath or ResultSelector.
The console shows the same data in a graph view, a table view, and an event view. Select a state in the graph to see its input, output, and error. Standard executions keep their history for 90 days, and one execution can hold up to 25,000 events.
You can also read the history from the CLI:
aws stepfunctions get-execution-history \
--execution-arn arn:aws:states:us-east-1:123456789012:execution:OrderWorkflow:run-1 \
--reverse-order \
--max-items 10--reverse-order puts the final events first, so the failure appears at the top.
CloudWatch Logs
Express workflows do not keep a history that you can query through the API. Standard workflows also benefit from logs, because logs let you search across many executions. You configure logging per state machine with three settings:
- Level:
ALL,ERROR,FATAL, orOFF. - includeExecutionData: whether state input and output appear in the log events. Turn it on for debugging, but remember that it can write sensitive data to logs.
- Destination: a CloudWatch Logs log group.
Name the log group with the prefix /aws/vendedlogs/states/. Log groups with other names can hit the resource policy size limit when Step Functions sets up log delivery. The execution role also needs the CloudWatch Logs delivery permissions, such as logs:CreateLogDelivery. Logging at ALL on a high-volume Express workflow can become a large cost, so use ERROR in production.
X-Ray tracing
X-Ray shows how long each state took and how calls to downstream services relate to each other. Enable tracing on the state machine. The execution role needs xray:PutTraceSegments, xray:PutTelemetryRecords, xray:GetSamplingRules, and xray:GetSamplingTargets. The console adds these permissions when you enable tracing, but the CLI and infrastructure-as-code tools do not.
Step Functions emits its own segments. A Lambda function appears in the trace in detail only if you also turn on active tracing for that function.
This command turns on both logging and tracing:
aws stepfunctions update-state-machine \
--state-machine-arn arn:aws:states:us-east-1:123456789012:stateMachine:OrderWorkflow \
--logging-configuration '{"level":"ERROR","includeExecutionData":true,"destinations":[{"cloudWatchLogsLogGroup":{"logGroupArn":"arn:aws:logs:us-east-1:123456789012:log-group:/aws/vendedlogs/states/OrderWorkflow:*"}}]}' \
--tracing-configuration enabled=trueChoosing a tool
Common mistakes
- Empty logs.
includeExecutionDatais off, or the level isFATALorOFF. Raise the level or enable the setting. - No trace data. The execution role lacks the X-Ray permissions, or tracing is off on the state machine.
- Log delivery errors on creation. The log group name does not start with
/aws/vendedlogs/states/, or the role lackslogs:CreateLogDelivery. - Missing history for an Express run. Express executions have no
GetExecutionHistorydata. Use the logs.
Exercise
A Standard execution failed at ValidateOrder, and you suspect a wrong ResultPath dropped part of the input. Which events do you compare, and which tool do you use?
Show answer
Use the execution history. Compare the input in the TaskStateEntered event with the output in the TaskStateExited event for ValidateOrder. If fields from the original input are missing in the output, the ResultPath is overwriting the input, for example by using "$" instead of "$.result".
Exercise
You enabled X-Ray on a state machine with the CLI, but no traces appear. Name two likely causes.
Show answer
The execution role is missing the X-Ray permissions (xray:PutTraceSegments, xray:PutTelemetryRecords, xray:GetSamplingRules, xray:GetSamplingTargets), or tracing was not actually enabled in the state machine's tracing configuration. Lambda functions also show detailed segments only if active tracing is on for them.