AWS CDK AWS Step Functions
The `aws-cdk-aws-stepfunctions` library is the AWS Cloud Development Kit (CDK) Construct Library for defining AWS Step Functions state machines in Python. It allows developers to programmatically create and manage Step Functions workflows, including states, tasks, and error handling. As of version `1.204.0`, this package is part of the AWS CDK v1 ecosystem, which typically sees frequent updates aligning with broader CDK releases.
Common errors
-
ModuleNotFoundError: No module named 'aws_cdk.aws_stepfunctions'
cause The Python package for AWS Step Functions constructs is not installed or the virtual environment is not active.fixActivate your Python virtual environment and run `pip install aws-cdk.aws-stepfunctions aws-cdk.core`. -
jsii.errors.JavaScriptError: There are no `app.py`, `app.ts` or `main.py` files in ...
cause The `cdk synth` or `cdk deploy` command was executed in a directory that does not contain the CDK application entry point (e.g., `app.py`).fixNavigate to the root directory of your CDK project where `app.py` resides before running `cdk synth` or `cdk deploy`. -
ClientError: An error occurred (AccessDeniedException) when calling the CreateStateMachine operation: User: arn:aws:iam::xxxxxxxxxxxx:user/your-user is not authorized to perform: states:CreateStateMachine on resource: arn:aws:states:...
cause The AWS IAM user or role attempting to deploy the CDK stack lacks the necessary permissions to create or update Step Functions resources.fixAttach an IAM policy with `states:CreateStateMachine`, `states:UpdateStateMachine`, and `states:DeleteStateMachine` permissions (and potentially permissions for related resources like `iam:PassRole`) to the user or role deploying the stack.
Warnings
- breaking This library (`aws-cdk-aws-stepfunctions`) is part of AWS CDK v1. AWS CDK v2 uses a consolidated `aws-cdk-lib` package. Do not mix v1 and v2 packages in the same project, as it leads to dependency conflicts and runtime errors.
- gotcha AWS Step Functions require appropriate IAM permissions for the state machine to execute and for its tasks (e.g., Lambda, ECS) to interact with other AWS services. Insufficient permissions will result in execution failures that are often hard to debug.
- gotcha State machine definitions can quickly grow in complexity and size, potentially hitting AWS service limits (e.g., 262144 characters for definition JSON). While CDK helps abstract this, highly complex workflows can still exceed limits.
- gotcha CDK's `cdk synth` command generates CloudFormation, but `cdk deploy` requires AWS credentials configured in your environment (e.g., via AWS CLI, environment variables, or IAM roles for EC2/ECS).
Install
-
pip install aws-cdk.aws-stepfunctions -
pip install aws-cdk.core
Imports
- core
import aws_cdk.core
from aws_cdk import core as cdk
- aws_stepfunctions
from aws_cdk.aws_stepfunctions import *
from aws_cdk import aws_stepfunctions as sfn
Quickstart
import os
from aws_cdk import core as cdk
from aws_cdk import aws_stepfunctions as sfn
# Define your CDK Stack
class MyStepFunctionStack(cdk.Stack):
def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# Define a simple Pass state
pass_state = sfn.Pass(self, "MyPassState",
state_json={
"Comment": "A simple pass state for demonstration",
"InputPath": "$",
"OutputPath": "$",
"Result": {"status": "SUCCESS", "source": "CDK"},
"End": True
}
)
# Define a State Machine using the Pass state
sfn.StateMachine(self, "MyCDKStateMachine",
definition=pass_state,
state_machine_name="MyCDKPassStateMachine", # Optional: explicit name
timeout=cdk.Duration.minutes(5) # Optional: default timeout
)
# Instantiate the CDK App and Stack
app = cdk.App()
MyStepFunctionStack(app, "MyStepFunctionStack")
app.synth()