Sub topic 14
Deploying and testing state machines
This chapter assumes you can write a state machine definition in Amazon States Language (ASL), you know what an execution role is, and you have used the AWS CLI or CloudFormation. Here you will deploy a definition as code, test single states without running a whole workflow, and release changes gradually.
Deploy with infrastructure as code
You can create a state machine in the console or with aws stepfunctions create-state-machine. Both are fine for experiments. For anything shared, define the state machine, its role, and the resources it calls in one template. That way every environment gets the same definition.
AWS SAM has a state machine resource that keeps the ASL in its own file. Placeholders such as ${ValidateOrderArn} in the JSON file are filled in through DefinitionSubstitutions. This avoids hard-coding ARNs like the one in the earlier Lambda example.
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Resources:
ValidateOrderFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
CodeUri: src/
OrderWorkflow:
Type: AWS::Serverless::StateMachine
Properties:
DefinitionUri: statemachine/order.asl.json
DefinitionSubstitutions:
ValidateOrderArn: !GetAtt ValidateOrderFunction.Arn
Policies:
- LambdaInvokePolicy:
FunctionName: !Ref ValidateOrderFunctionSAM generates the execution role from Policies, which keeps permissions scoped to the one function. The pipeline is sam build, then sam deploy.
Test one state at a time
The TestState API runs a single state with an input you supply, without creating a state machine. Choice, Pass, Wait, Succeed, and Fail states need no role. A Task state needs a role that can call the service.
aws stepfunctions test-state \
--definition '{"Type":"Choice","Choices":[{"Variable":"$.result.validation.valid","BooleanEquals":true,"Next":"OrderAccepted"}],"Default":"OrderFailed"}' \
--input '{"result":{"validation":{"valid":true}}}'The response reports status and nextState. Here you should see OrderAccepted. Run it again with valid set to false and you should see OrderFailed. Add --inspection-level DEBUG to see how each input and output filter changed the data. This is the fastest way to debug ResultSelector and ResultPath mistakes.
For whole workflows, Step Functions Local is a Docker image that runs state machines on your machine. It supports mock configuration files, so you can force a Lambda task to return a fixed result or fail with a chosen error. This lets you test Retry and Catch paths that are hard to trigger on real services.
| Approach | Tests | Needs AWS calls | Best for |
|---|---|---|---|
| TestState API | One state | Yes, but no execution is created | Data flow and Choice rules |
| Step Functions Local with mocks | Whole workflow | No | Error paths and branching |
| Deployed test stack | Whole workflow and IAM | Yes | Permissions and integrations |
Mocks do not check IAM. Always run at least one end-to-end execution in a real account to confirm the execution role works.
Release changes safely
Updating a state machine changes it for new executions. Executions already running keep the definition they started with. To control rollout, publish a version, then point an alias at it.
aws stepfunctions publish-state-machine-version \
--state-machine-arn arn:aws:states:us-east-1:123456789012:stateMachine:OrderWorkflow
aws stepfunctions create-state-machine-alias \
--name live \
--routing-configuration '[{"stateMachineVersionArn":"arn:aws:states:us-east-1:123456789012:stateMachine:OrderWorkflow:1","weight":100}]'Callers start executions using the alias ARN. To canary a new version, update the alias with two entries, for example weights 90 and 10. If the new version misbehaves, set the old version back to 100.
Exercise
You changed a ResultPath in a Task state and want to check the output shape before deploying. Which tool do you use, and which option shows how each filter changed the data?
Show answer
Use the TestState API (aws stepfunctions test-state) with --inspection-level DEBUG. It runs only that state and shows the data after each input and output filter.
Exercise
You want to confirm that a Catch block handles Lambda.ServiceException without causing a real Lambda failure. What do you use?
Show answer
Step Functions Local with a mock configuration that makes the Lambda task return that error. Mocks do not verify IAM permissions, so you still run one real end-to-end test.