AWS Compute Blog
Accelerating workflow development with the TestState API in AWS Step Functions
This post is written by Ben Freiberg, Senior Solutions Architect.
Developers often choose AWS Step Functions to orchestrate the services that comprise their applications. Step Functions is a visual workflow service that makes it easier for developers to build distributed applications, automate processes, orchestrate microservices, and create data and machine learning (ML) pipelines. Step Functions integrates with over 220 AWS services and any publicly accessible HTTP endpoint. Step Functions provides many features that help developers build, such as built-in error handling, real-time and auditable workflow execution history, and large-scale parallel processing.
Several areas can be time consuming for developers when testing Step Functions workflows. For example, authentication with external services, input/output processing, AWS IAM permission, or intrinsic functions. To simplify and speed up resolving these issues, Step Functions released a new capability last year to test individual states: the TestState API. This feature allows you to test states independently from the execution of your workflow. You can change the input and test different scenarios without the need to deploy your workflow or execute the whole state machine. This feature is available for all task, choice, and pass states.
Since developers spend significant time in IDEs and terminals, TestState is also available via an API. This allows you to iterate over changes for an individual state and lets you refine the input/output processing or conditional logic in a choice state without leaving your IDE. In this post, you’ll learn how the TestState API can speed up your testing and development.
Getting started with TestState
Suppose that you are developing a payment processing workflow that consists of three states. First, a Choice
state that checks the type of payment based on the input data. Depending on the type, it calls either an AWS Lambda function or an external endpoint. The task state that invokes the Lambda function includes some input/output processing.
To get started with the TestState API, you must create an IAM role that the service can assume. The role must contain the required IAM permissions for the resources your state is accessing. For information about the permissions a state might need, see IAM permissions to test a state. The following snippet shows the minimal necessary permissions:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"states:TestState",
"iam:PassRole"
],
"Resource": "*"
}
]
}
Next, you must provide the definition of the state being tested. The choice state is configured to check the type of payment and if the voucherId is present, in case of a voucher. The following snippet shows the state definition:
{
"Type": "Choice",
"Choices": [
{
"And": [
{
"Variable": "$.payment.type",
"IsPresent": true
},
{
"Variable": "$.payment.type",
"StringEquals": "voucher"
}
],
"Next": "Process voucher"
},
{
"Variable": "$.payment.type",
"StringEquals": "credit",
"Next": "Call payment provider"
}
],
"Default": "Fail"
}
Using the role and state definition, you can now test it if an input results in the expected next state:
aws stepfunctions test-state
--definition file://choice.json
--role-arn "arn:aws:iam::<account-id>:role/StepFunctions-TestState-Role"
--input '{"payment":{"type":"voucher"}}'
The response shows that the test did not encounter any errors and that the next state would be invoking the Lambda function to process the voucher as expected.
{
"output": "{\"payment\":{\"type\":\"voucher\"}}",
"nextState": "Process voucher",
"status": "SUCCEEDED"
}
Similarly, with a payment type of credit as input, the next state is invoking the third-party endpoint:
aws stepfunctions test-state
--definition file://choice.json
--role-arn "arn:aws:iam::<account-id>:role/StepFunctions-TestState-Role"
--input '{"payment":{"type":"credit"}}'
{
"output": "{\"payment\":{\"type\":\"credit\"}}",
"nextState": "Call payment provider",
"status": "SUCCEEDED"
}
Because the TestState API takes the state definition as an argument, you do not have to redeploy the state machine when changing the state definition. Instead, you can iterate and test your settings by passing the modified state definition to the TestState API.
Using inspection levels
For each state, you can specify the amount of detail you want to view in the test results. These details provide additional information about the state that you are testing. For example, if you’ve used any input and output data processing filters, such as InputPath or ResultPath in a state, you can view the intermediate and final data processing results. Step Functions provides the following levels to specify the details you want to view, INFO, DEBUG, and TRACE. All these levels return the status
and nextState
fields.
Next, the Lambda Invoke state is tested. In this scenario, the state includes input/output processing. The output from the function is transformed by renaming and restructuring the field and then merged with the original input. This is the relevant part of the task definition:
"Process voucher": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {...},
"Retry": [...],
"Next": "Success",
"ResultPath": "$.voucherProcessed",
"ResultSelector": {
"status.$": "$.Payload.result",
"workflowId.$": "$.Payload.workflow"
}
}
This time test using the Step Functions console, which can make it easier to understand the input/output processing steps. To get started, open the state machine in Workflow Studio and select the state, and then choose Test State. Make sure to select DEBUG as the inspection level. After testing the state, switch to the Input/output processing tab to check the intermediate steps.
When you call the TestState API and set the inspectionLevel
parameter to DEBUG
, the API response includes an object called inspectionData
. This object contains fields to help you inspect how data was filtered or manipulated within the state when it was executed. This data is shown in the Input/output processing tab in the console.
Being able to see all the processing steps easily in one place allows developers to spot issues and iterate more quickly, saving time.
Testing third-party endpoint integrations
Applications might call third-party endpoints that require authentication. Step Functions offers the HTTPS endpoint resource to connect to third-party HTTP targets outside of the AWS Cloud.
HTTPS endpoints use Amazon EventBridge connections to manage the authentication credentials for the target. This defines the authorization type used, which can be a basic authentication with a username and password, an API key, or OAuth. EventBridge connections use AWS Secrets Manager to store the secret. This keeps the secrets out of the state machine, reducing the risks of accidentally exposing your secrets in logs or in the state machine definition.
Getting the authentication configuration right might involve several time-consuming iterations. With the TRACE inspection level, developers can see the raw HTTP request and response, which is useful for verifying headers, query parameters, and other API-specific details. This option is only available for the HTTP Task. You can also view the secrets included in the EventBridge connection. To do this, you must set the revealSecrets
parameter to true
in the TestState API. This can help verifying that the correct authentication parameters are used.
To get started, ensure that the execution role used for testing has the necessary permissions, as shown here:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"secretsmanager:GetSecretValue",
"secretsmanager:DescribeSecret"
],
"Resource": "arn:aws:secretsmanager:<your-region>:<account-id>:secret:events!connection/<your-connection-id>"
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "RetrieveConnectionCredentials",
"Effect": "Allow",
"Action": [
"events:RetrieveConnectionCredentials"
],
"Resource": [
"arn:aws:events:<your-region>:<account-id>:connection/<your-connection-id>"
]
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "InvokeHTTPEndpoint",
"Effect": "Allow",
"Action": [
"states:InvokeHTTPEndpoint"
],
"Resource": [
"arn:aws:states:<your-region>:<account-id>:stateMachine:<your-statemachine>"
]
}
]
}
When you test the HTTP task, make sure to set the inspection level to TRACE. Then use the HTTP request and response tab to check the details. This capability saves you time when debugging complex authentication issues.
Automating testing
Testing is not only a manual activity to get the configuration right. Most often, tests are run as part of a suite of tests, which are automatically performed to validate the correct behavior. It also prevents regressions when making changes. The TestState API can easily be integrated in such tests as well.
The following snippet shows a test using the Jest framework in JavaScript. The test checks if the correct next state is produced given a definition and input. The definition resides in a different file, which can also be used for infrastructure as code (IaC) to create the state machine.
const { SFNClient, TestStateCommand } = require("@aws-sdk/client-sfn");
// Import the state definition
const definition = require("./definition.json");
const client = new SFNClient({});
describe("Step Functions", () => {
test("that next state is correct", async () => {
const command = new TestStateCommand({
definition: JSON.stringify(definition),
roleArn: "arn:aws:iam::<account-id>:role/<role-with-sufficient-permissions>",
input: "{}" # Adjust as necessary
});
const data = await client.send(command);
expect(data.status).toBe("SUCCEEDED");
expect(data.nextState).toBe("Success"); # Adjust as necessary
});
});
With automated tests, you can safely change your workflow definitions without the need for manual efforts. That way, you are immediately alerted if a change would result in an incompatibility.
With TestState you can increase your test coverage with less effort because you can test states directly. This is especially helpful for complex workflows and states that require a specific set of circumstances to reach them. It makes it easier to validate the correctness of your error-handling as well. You can now test the potentially many combinations of your configured Retriers and Catchers much easier.
Conclusion
The TestState API helps developers to iterate faster, resolve issues efficiently, and deliver high-quality applications with greater confidence. By enabling developers to test individual states independently and integrating testing into their preferred development workflows, it simplifies the debugging process and reduces context switches. Whether testing input/output processing, authentication with external services, or third-party endpoint integrations, the TestState API can be a useful tool for testing.