Repository navigation
fix(cfn,iam): a stack holding an instance profile can be deleted (#581) - #588
Merged
Merged
Conversation
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.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A stack holding an
AWS::IAM::Roleand anAWS::IAM::InstanceProfilethatreferences it — the shape of every "EC2 instance with a role" template — could not be
deleted, and failed inverted from both directions:
The failure landed on the resource that was still present, and a second
DeleteStackfailed identically. There was noRetainResourcesescape 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.
DeleteRoledid not enforce the instance-profile constraint. Reproduced withno CloudFormation involved:
DeleteRoleon a role an instance profile heldsucceeded, 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)", andDeleteConflictis 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
RemoveRoleFromInstanceProfileper role in the profile's declaredRolesbeforeDeleteInstanceProfile, resolved through the deploy's own context soa role whose name was generated (#560) is detached by that generated name.
It lands as a
cfnDeletePreStepshook rather than by weakeningcfnDeleteRequestFunc: that contract is deliberately one request per resource, whichis what keeps every entry in
cfnResourceDeletersa data declaration inspectablewithout dispatching. The pre-steps run through the existing
dispatchResourceDelete, so absent-resource tolerance and event recording areidentical 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/e2evet + test.Live against
substrate server --address :4671with the real AWS CLI(
AWS_MAX_ATTEMPTS=1):And the two fixes together give the release its point — two stacks from one template
now deploy and tear down independently:
ra/rbbothCREATE_COMPLETE,deleting
rbleavesra-MyRole-czo0btk63rc9intact.Tests
Six,
package emulator_test, no wall-clock or network dependency: the #581reproducer reaching
DELETE_COMPLETEwith both resources gone from IAM (assertingnon-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 deletesafter 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
!Refto the role's generated name or silently do nothing.Mutation testing
Four mutants, anchor-count asserted,
gofmt+vetgated:instance_profile:sweep fromDeleteRoleDeleteConflictstatus changed to 400Each half of the fix is independently gated, which is what the "fixing either alone
flips the symptom" property requires.
Compatibility
A test asserting that
DeleteRolesucceeds on a role held by an instance profile willnow see
DeleteConflict/409. That refusal is real IAM behavior; the permissivesuccess was the defect.
Closes #581