Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
225591b
feat!: modification is forbidden during a refactor
otaviomacedo Jun 27, 2025
88762a0
Merge branch 'main' into otaviom/forbid-modifications
otaviomacedo Jun 30, 2025
b2ad63d
feat: refactor execution
otaviomacedo Jul 1, 2025
9355e19
Better error message
otaviomacedo Jul 4, 2025
06c001f
Remove Rules and Parameters for new stacks
otaviomacedo Jul 4, 2025
97aae1d
Fix test
otaviomacedo Jul 4, 2025
71e2cf4
Don't send CDKMetadata if deployed doesn't have it
otaviomacedo Jul 4, 2025
4a76d0f
Merge branch 'main' into otaviom/isomorphic-refactor-execution
otaviomacedo Jul 16, 2025
e9f45d9
fixes after merge
otaviomacedo Jul 16, 2025
95d8daa
Improve ambiguity message
otaviomacedo Jul 16, 2025
66cc659
Overrides can be construct paths
otaviomacedo Jul 17, 2025
44a9b66
update test: new stack creation without CDKMetadata
otaviomacedo Jul 17, 2025
dfff897
Better message in case of modification
otaviomacedo Jul 18, 2025
d2bda95
More integ tests
otaviomacedo Jul 21, 2025
3341571
Using the deployment role or custom role
otaviomacedo Jul 25, 2025
834b5d1
Merge branch 'main' into otaviom/isomorphic-refactor-execution
otaviomacedo Aug 19, 2025
a709dca
Handling the case where the mappings contain stacks not present locally
otaviomacedo Aug 19, 2025
e8681ba
assumeRole -> assumeRoleArn
otaviomacedo Aug 21, 2025
bc6f519
Fix tests
otaviomacedo Aug 21, 2025
ed9f20f
Add stack refactor ID to unknown error
otaviomacedo Aug 29, 2025
a6a0e60
Deploy to finalize refactor
otaviomacedo Sep 1, 2025
fdf41ea
Admin access limitation
otaviomacedo Sep 2, 2025
ec8da7e
Admin access limitation
otaviomacedo Sep 2, 2025
eace9b7
Block creation of new stacks
otaviomacedo Sep 3, 2025
461b400
Fix integ tests
otaviomacedo Sep 3, 2025
258628c
Merge branch 'main' into otaviom/isomorphic-refactor-execution
otaviomacedo Sep 3, 2025
3fb9eba
Fix integ test
otaviomacedo Sep 3, 2025
4bf28cf
Merge remote-tracking branch 'origin/otaviom/isomorphic-refactor-exec…
otaviomacedo Sep 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Overrides can be construct paths
  • Loading branch information
otaviomacedo committed Jul 17, 2025
commit 66cc6597fdc9ef16b11f3f9af51a0789a010f779
51 changes: 20 additions & 31 deletions packages/@aws-cdk/toolkit-lib/lib/toolkit/toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1113,8 +1113,6 @@ export class Toolkit extends CloudAssemblySourceBuilder {
await ioHelper.defaults.info('Refactoring...');
await context.execute(stackDefinitions, sdkProvider, ioHelper);
await ioHelper.defaults.info('✅ Stack refactor complete');

await ioHelper.notify(IO.CDK_TOOLKIT_I8900.msg(refactorMessage, refactorResult));
} catch (e: any) {
const message = `❌ Refactor failed: ${formatError(e)}`;
await ioHelper.notify(IO.CDK_TOOLKIT_E8900.msg(message, { error: e }));
Expand All @@ -1128,39 +1126,30 @@ export class Toolkit extends CloudAssemblySourceBuilder {
const mappingGroup = options.overrides
?.find(g => g.region === environment.region && g.account === environment.account);

let overrides: ResourceMapping[] = [];
if (mappingGroup != null) {
overrides = Object.entries(mappingGroup.resources ?? {}).map(([source, destination]) => {
const sourceStack = findStack(source, deployedStacks);
const sourceLogicalId = source.split('.')[1];

const destinationStack = findStack(destination, localStacks);
const destinationLogicalId = destination.split('.')[1];

return new ResourceMapping(
new ResourceLocation(sourceStack, sourceLogicalId),
new ResourceLocation(destinationStack, destinationLogicalId),
);
});
}

return overrides;
return mappingGroup == null
? []
: Object.entries(mappingGroup.resources ?? {})
.map(([source, destination]) => new ResourceMapping(
getResourceLocation(source, deployedStacks),
getResourceLocation(destination, localStacks),
));
}

function findStack(location: string, stacks: CloudFormationStack[]): CloudFormationStack {
const result = stacks.find(stack => {
const [stackName, logicalId] = location.split('.');
if (stackName == null || logicalId == null) {
throw new ToolkitError(`Invalid location '${location}'`);
function getResourceLocation(location: string, stacks: CloudFormationStack[]): ResourceLocation {
for (let stack of stacks) {
const [stackName, logicalId] = location.split('.');
if (stackName != null && logicalId != null && stack.stackName === stackName && stack.template.Resources?.[logicalId] != null) {
return new ResourceLocation(stack, logicalId);
} else {
const resourceEntry = Object
.entries(stack.template.Resources ?? {})
.find(([_, r]) => r.Metadata?.['aws:cdk:path'] === location);
if (resourceEntry != null) {
return new ResourceLocation(stack, resourceEntry[0]);
}
return stack.stackName === stackName && stack.template.Resources?.[logicalId] != null;
});

if (result == null) {
throw new ToolkitError(`Cannot find resource in location ${location}`);
}

return result;
}
throw new ToolkitError(`Cannot find resource in location ${location}`);
}

async function confirm(force: boolean): Promise<boolean> {
Expand Down
12 changes: 7 additions & 5 deletions packages/@aws-cdk/toolkit-lib/test/actions/refactor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,12 @@ test('detects modifications to the infrastructure', async () => {
);
});

test('overrides can be used to resolve ambiguities', async () => {
test.each([
// In each case, only one entry is enough to disambiguate the pair.
// In general, for n elements, we can disambiguate with n - 1 entries.
['logical ID', { 'Stack1.CatPhotos': 'Stack1.MyBucket1553EAA46' }],
['construct path', { 'Stack1/CatPhotos/Resource': 'Stack1/MyBucket1/Resource' }],
])('overrides can be used to resolve ambiguities (using %s)', async (_, overrides) => {
// GIVEN
mockCloudFormationClient.on(ListStacksCommand).resolves({
StackSummaries: [
Expand Down Expand Up @@ -405,10 +410,7 @@ test('overrides can be used to resolve ambiguities', async () => {
{
account: '123456789012',
region: 'us-east-1',
resources: {
// Only one override is enough in this case
'Stack1.CatPhotos': 'Stack1.MyBucket1553EAA46',
},
resources: overrides,
},
],
stacks: {
Expand Down
Loading