Skip to content

fix(cfn,iam): a stack holding an instance profile can be deleted (#581) - #588

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/581-instance-profile-delete
Aug 6, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/581-instance-profile-delete

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

A stack holding an AWS::IAM::Role and an AWS::IAM::InstanceProfile that
references it — the shape of every "EC2 instance with a role" template — could not be
deleted, and failed inverted from both directions:

delete: DELETE_FAILED
  ProbeRole     DELETE_COMPLETE
  ProbeProfile  DELETE_FAILED  DeleteConflict
get-role     probe-role-1     → NoSuchEntity
get-profile  probe-profile-1  → still exists

The failure landed on the resource that was still present, and a second
DeleteStack failed identically. There was no RetainResources escape either:
retaining the profile leaves it behind for good.

Two defects, both here

Fixing either alone flips the symptom rather than fixing it, as the issue says.

1. DeleteRole did not enforce the instance-profile constraint. Reproduced with
no CloudFormation involved: DeleteRole on a role an instance profile held
succeeded, and the profile was then left listing a role that no longer existed —
the dangling reference that made the stack unfixable. The reference is explicit:
"Before attempting to delete a role, remove the following attached items: … Instance
profile (RemoveRoleFromInstanceProfile)", and DeleteConflict is 409.
Substrate now refuses with exactly that code and status — the same shape it already
used for the attached-policies case two blocks up — and names the profiles holding
the role, since the reference specifies "The error message describes these entities"
and a caller otherwise cannot tell what to detach from. A role held by two profiles
is refused until both release it.

2. The CFN delete of an instance profile did not detach its roles. The sweep now
dispatches one RemoveRoleFromInstanceProfile per role in the profile's declared
Roles before DeleteInstanceProfile, resolved through the deploy's own context so
a role whose name was generated (#560) is detached by that generated name.

It lands as a cfnDeletePreSteps hook rather than by weakening
cfnDeleteRequestFunc: that contract is deliberately one request per resource, which
is what keeps every entry in cfnResourceDeleters a data declaration inspectable
without dispatching. The pre-steps run through the existing
dispatchResourceDelete, so absent-resource tolerance and event recording are
identical for both. A profile declaring no roles emits nothing.

Ordering was already correct in the priority table (profile 2, role 1, swept
descending) and the stack still failed — so the order is asserted from the recorded
events rather than trusted.

Verification

Full gate green: gofmt, go build, go vet, golangci-lint (0 issues),
make test (race), make docs-reference-check docs-versions, docs site build,
test/e2e vet + test.

Live against substrate server --address :4671 with the real AWS CLI
(AWS_MAX_ATTEMPTS=1):

create: CREATE_COMPLETE        profile roles: probe-role-1
delete → stack gone (not DELETE_FAILED)
get-role     probe-role-1     → NoSuchEntity
get-profile  probe-profile-1  → NoSuchEntity

# defect 1 directly
aws iam delete-role --role-name direct-role
→ DeleteConflict: Cannot delete entity, must remove roles from instance profile
  first. Instance profiles holding direct-role: direct-profile
# the role is still there, and deletes after remove-role-from-instance-profile

And the two fixes together give the release its point — two stacks from one template
now deploy and tear down independently: ra/rb both CREATE_COMPLETE,
deleting rb leaves ra-MyRole-czo0btk63rc9 intact.

Tests

Six, package emulator_test, no wall-clock or network dependency: the #581
reproducer reaching DELETE_COMPLETE with both resources gone from IAM (asserting
non-vacuously that the profile really held the role first); the dispatch order
from recorded events (RemoveRoleFromInstanceProfile, DeleteInstanceProfile,
DeleteRole); the direct-IAM refusal including that the role survives it and deletes
after a detach; a role in two profiles refused until both release it, with the
released one no longer named; a profile with no roles dispatching only its own
delete; and the #560 interaction — both names generated, so the detach must resolve
!Ref to the role's generated name or silently do nothing.

Mutation testing

Four mutants, anchor-count asserted, gofmt+vet gated:

Mutant Verdict
Drop the instance_profile: sweep from DeleteRole CAUGHT (both direct-IAM tests)
Drop the profile pre-step CAUGHT (all three CFN tests)
DeleteConflict status changed to 400 CAUGHT (both direct-IAM tests)
Stop at the first holding profile CAUGHT (the two-profile test)

Each half of the fix is independently gated, which is what the "fixing either alone
flips the symptom" property requires.

Compatibility

A test asserting that DeleteRole succeeds on a role held by an instance profile will
now see DeleteConflict/409. That refusal is real IAM behavior; the permissive
success was the defect.

Closes #581

DeleteStack on a stack with an IAM role and an instance profile that references
it could never converge, and failed inverted from both directions: the role
reached DELETE_COMPLETE while still attached, and the profile that referenced it
failed with DeleteConflict. The failure therefore landed on the resource still
present, a retry failed identically, and RetainResources was no escape.

Two independent defects, and fixing either alone flips the symptom:

DeleteRole now enforces the instance-profile constraint. It previously succeeded
on a role a profile held, leaving the profile listing a role that no longer
existed. The reference requires the profile be removed first, so substrate
refuses with DeleteConflict/409 — the code, status and shape already used for
attached policies — naming the profiles that hold the role.

The CloudFormation delete of an instance profile detaches its roles first, one
RemoveRoleFromInstanceProfile per declared role, resolved through the deploy's
own context so a generated role name is detached by that name. It lands as a
pre-step hook rather than by weakening the one-request-per-resource deleter
contract that keeps each table entry a data declaration.
@scttfrdmn
scttfrdmn merged commit eedf4b0 into main Aug 6, 2026
@scttfrdmn scttfrdmn mentioned this pull request Aug 6, 2026
@scttfrdmn
scttfrdmn deleted the fix/581-instance-profile-delete branch August 9, 2026 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CloudFormation: DeleteStack never detaches a role from an instance profile, so the stack ends DELETE_FAILED and can never converge

1 participant