(aws-stepfunctions): Unable to import an existing StateMachine with CDK
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the bug
**Unable to import an existing StateMachine with CDK.**
CDK version: 2.1031.1 (build a560d1e)
Test region: us-east-1
No similar issues found.
**What I think it's the root cause: CDK tries to locate the resource with the name and for some reason that is not working. We can change it so it finds with the ARN.**
**Steps to replicate the issue:**
1. Used an existing 'test-import-state-machine-role' execution role in my account.
2. Created a stack with a S3 bucket and a StateMachine resource.
3. Removed the StateMachine from the CDK template and ran "cdk deploy".
4. Added the resource back to the template, and ran "cdk import"
5. When prompted, selected yes:
CdkStepfunctionsImportTestStack/TestStateMachine/Resource (AWS::StepFunctions::StateMachine): import with StateMachineName=test-import-state-machine (y/n) y
Error:
```
Failed to create ChangeSet cdk-deploy-change-set on CdkStepfunctionsImportTestStack: FAILED, State machine does not exist (Service: null, Status Code: 0, Request ID: null)
```
Workaround:
1. Create a resource mapping without triggering the import operation:
cdk import --record-resource-mapping mapping.json
2. when prompted, select Y
3. Edit mapping.json, replacing "StateMachineName" with "Arn", e.g.:
```
{
"TestStateMachine3C216BE3": {
"Arn": "arn-here"
}
}
```
4. run cdk import --resource-mapping mapping.json
Done. You can delete the mapping.json and continue to run updates normally.
CDK test template:
```
from aws_cdk import (
Stack,
aws_stepfunctions as sfn,
aws_iam as iam,
RemovalPolicy,
aws_s3 as s3
)
from constructs import Construct
class CdkStepfunctionsImportTestStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
bucket = s3.Bucket (
self,
"Bucket",
removal_policy=RemovalPolicy.DESTROY
)
# Import the existing role by ARN (hard-coded temporarily)
existing_role = iam.Role.from_role_arn(
self,
"ImportedStateMachineRole",
f"arn:aws:iam::{self.account}:role/test-import-state-machine-role",
mutable=False
)
# Create a simple Pass state for testing
pass_state = sfn.Pass(
self, "PassState",
comment="A simple pass state for testing import"
)
# Create the state machine with the existing role
state_machine = sfn.StateMachine(
self, "TestStateMachine",
state_machine_name="test-import-state-machine",
definition=pass_state,
state_machine_type=sfn.StateMachineType.STANDARD,
role=existing_role # Use the existing role
)
# Apply deletion policy to retain on delete
state_machine.node.default_child.apply_removal_policy(RemovalPolicy.RETAIN)
```
### Regression Issue
- [ ] Select this option if this issue appears to be a regression.
### Last Known Working CDK Library Version
_No response_
### Expected Behavior
.
### Current Behavior
.
### Reproduction Steps
.
### Possible Solution
_No response_
### Additional Information/Context
_No response_
### AWS CDK Library version (aws-cdk-lib)
.
### AWS CDK CLI version
.
### Node.js Version
.
### OS
.
### Language
Python
### Language Version
_No response_
### Other information
_No response_
Contributor guide
Research direction
Start with the CDK import flow for AWS::StepFunctions::StateMachine and reproduce the issue using the supplied Python stack and cdk import commands. Compare the StateMachineName mapping with the documented ARN workaround; done means importing the existing state machine succeeds without manually editing mapping.json.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, python, typescript
- Domain
- cloud, infrastructure
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100