Skip to content

fix: prevent fullDiff mutation corrupting cdk import change set#1602

Open
saeekailas wants to merge 3 commits into
aws:mainfrom
saeekailas:main
Open

fix: prevent fullDiff mutation corrupting cdk import change set#1602
saeekailas wants to merge 3 commits into
aws:mainfrom
saeekailas:main

Conversation

@saeekailas
Copy link
Copy Markdown

@saeekailas saeekailas commented Jun 5, 2026

Problem

cdk import was failing with:

You have modified resources [...] in your template that are not
being imported. Update, create or delete operations cannot be
executed during import operations.

This error was appearing even when the user had not modified any
resources. It affected any stack containing a resource with a
multi-element DependsOn array whose natural order is not alphabetical.

A CDK L2 lambda.Function reliably triggers this because CDK generates:

DependsOn: [ FnServiceRoleDefaultPolicy, FnServiceRole ]

which is not in alphabetical order. After the bug, the submitted
change set had them reversed, which CloudFormation treated as a
modification and rejected the import.


Root Cause

Two issues working together:

  1. fullDiff() in diff-template.ts called normalize() directly on the
    caller's template objects, sorting DependsOn arrays in-place.
    This means fullDiff() was secretly mutating whatever object you
    passed into it.

  2. ResourceImporter in importer.ts cached the deployed template and
    reused the exact same object for both:

    • passing to fullDiff() during import discovery (which mutated it)
    • building the IMPORT change set template afterward

So by the time the change set was submitted, DependsOn arrays were
reordered and CloudFormation saw that as a modification.

Fix

packages/@aws-cdk/cloudformation-diff/lib/diff-template.ts

Clone both templates before calling normalize() so fullDiff() is
completely side-effect free and never mutates the caller's objects.

packages/@aws-cdk/toolkit-lib/lib/api/resource-import/importer.ts

Deep clone the deployed template when storing it in _currentTemplate
so no downstream mutation can ever corrupt the cached value.

Also clone inside currentTemplateWithAdditions() before appending
new resources so the cache stays a clean mirror of the deployed state.

Testing

  • Added unit test that calls fullDiff() with a template containing
    a multi-element DependsOn in non-alphabetical order and asserts
    the original object is unchanged after the call.

  • Manually verified cdk import succeeds on a stack containing a
    CDK L2 lambda.Function (the original reproduction case).

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

Related

Fixes #1575

@aws-cdk-automation aws-cdk-automation requested a review from a team June 5, 2026 06:37
@saeekailas saeekailas changed the title fix(import): resolve cdk import failure caused by fullDiff mutating DependsOn arrays fix: prevent fullDiff mutation corrupting cdk import change set Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

1 participant