aws / aws/aws-cdk

(core): refactor failed with (seemingly) identical resources

Open
#35,718 12 comments 4 reactions 0 assignees View on GitHub
@aws-cdk/core bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
74

Description

### Describe the bug

Firstly, I'd like to say thanks so much for implementing this feature! It's timely and should be super useful. I am attempting to perform a refactor, but stuck on one state machine that `cdk refactor` is unable to reconcile as unchanged between stacks. I had initially been having the same issues on another state machine, but realized that some of the unicode characters hadn't translated properly to the CloudFormation template and was able to get `refactor` working there.

This state machine has no diff between the local template and what's on CloudFormation except for the following log groups at the end (also being refactored), but I'm confused why this would have worked on the other state machine that had almost the same log group definitions.

```
{
"CloudWatchLogsLogGroup": {
"LogGroupArn": {
- "Fn::GetAtt": [
- "betapipelinedefinitionloggroup<...>",
- "Arn"
- ]
+ "Fn::ImportValue": "cw-stack:ExportsOutputFnGetAttdalgostatemachineloggroup<...>"
}
}
}
@@ -720,7 +717,7 @@
"Level": "ERROR"
},
"RoleArn": {
- "Fn::ImportValue": "shared-resources-stack:ExportsOutputFnGetAttalgostatemachinerole<...>"
+ "Fn::ImportValue": "iam-stack:ExportsOutputFnGetAttalgostatemachinerole<...>"
},
"StateMachineName": "beta-pipeline-definition",
"StateMachineType": "STANDARD",
@@ -732,15 +729,6 @@
]
},
"Metadata": {
- "aws:cdk:path": "beta-pipeline-stack/beta-pipeline-definition"
+ "aws:cdk:path": "statemachine-stack/beta-pipeline-definition"
}
},
```

Any assistance here would be appreciated or debugging steps that might elucidate why the stacks are showing as not the same.

The exact error I'm seeing is listed below.

Running a verbose output with this didn't seem to give more information about why the stacks don't match. I am including the `statemachine-stack` as an additional stack with `--additional-stack-name=` and the other resources in that stack are not causing a problem.

### Regression Issue

- [ ] Select this option if this issue appears to be a regression.

### Last Known Working CDK Library Version

_No response_

### Expected Behavior

Clean refactor with the `cdk refactor` recognizing that `beta-pipeline-definition` is the same between the old and new stack.

### Current Behavior

```
Refactor failed: A refactor operation cannot add, remove or update resources. Only resource moves and renames are allowed.
The following resources are present only in the AWS environment:
- statemachine-stack/beta-pipeline-definition

The following resources are present only in your CDK application:
- beta-pipeline-stack/beta-pipeline-definition
```

### Reproduction Steps

```bash
cdk refactor --unstable=refactor -c env=dev --dry-run --additional-stack-name=statemachine-stack
```

### Possible Solution

I tried comparing the CloudFormation template for the old resource with the new one that is generated in `cdk.out` using a git diff. Looking for a way to confirm difference/similarities between these two or understand better how the `cdk refactor` command determines whether or not there is a difference.

### Additional Information/Context

_No response_

### AWS CDK Library version (aws-cdk-lib)

2.219.0

### AWS CDK CLI version

2.1029.4 (build 09c0061)

### Node.js Version

v22.17.0

### OS

MacOS 15.6.1 (24G90)

### Language

TypeScript

### Language Version

^5.9.3

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the `cdk refactor --unstable=refactor --dry-run --additional-stack-name=statemachine-stack` command and compare the generated `cdk.out` template with the CloudFormation template. Trace how `cdk refactor` compares the `beta-pipeline-definition` resource, then document or test the condition that makes the apparently equivalent resources differ; done means the command recognizes the resource move without adding or updating it.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cli, cloud, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.