Repository navigation
Description
Verified against ghcr.io/scttfrdmn/substrate:0.92.0 (/health reports v0.92.0), not read from the changelog.
What happens
A stack holding an AWS::IAM::Role and an AWS::IAM::InstanceProfile that references it deletes inverted from both directions: the role reaches DELETE_COMPLETE while still attached, and the profile that references it fails.
create: CREATE_COMPLETE
profile roles after create: ['probe-role-1']
delete: DELETE_FAILED
ProbeRole DELETE_COMPLETE
ProbeProfile DELETE_FAILED DeleteConflict: Cannot delete entity, must detach all roles first.
role: gone (NoSuchEntity)
profile: STILL EXISTS
Retrying DeleteStack does not help — it returns DELETE_FAILED again and the profile still exists, so the stack can never converge. There is no RetainResources escape either, since the resource that fails is the one still present.
Real CloudFormation calls RemoveRoleFromInstanceProfile as part of deleting an AWS::IAM::InstanceProfile, and orders the two resources by the Ref dependency, so this stack deletes cleanly.
There are two distinct defects here
1. DeleteRole does not enforce the instance-profile constraint
Reproduced with direct IAM calls, no CloudFormation involved:
DeleteRole on an attached role: SUCCEEDED (real IAM raises DeleteConflict)
profile now lists roles: ['direct-role']
DeleteInstanceProfile: refused — DeleteConflict
Real IAM refuses DeleteRole with DeleteConflict while the role is in any instance profile. Substrate allows it, which leaves the profile holding a dangling reference to a role that no longer exists — and DeleteInstanceProfile then refuses because of that reference. RemoveRoleFromInstanceProfile for the deleted role does still succeed, so it is recoverable by hand:
RemoveRoleFromInstanceProfile: succeeded
DeleteInstanceProfile: succeeded -> recoverable
but nothing in the CloudFormation path performs that call, so the stack cannot recover itself.
2. AWS::IAM::InstanceProfile deletion does not detach its roles
Even with defect 1 fixed, deleting the profile has to remove its roles first. Real CloudFormation does this; substrate issues a bare DeleteInstanceProfile, which IAM correctly refuses.
Fixing 1 alone would flip the symptom rather than fix it: the role delete would then be the one to fail, and the stack would still end DELETE_FAILED.
Reproducer
import time, boto3, botocore
kw = dict(endpoint_url="http://localhost:4566", region_name="us-east-1",
aws_access_key_id="test", aws_secret_access_key="test")
cfn, iam = boto3.client("cloudformation", **kw), boto3.client("iam", **kw)
TEMPLATE = """
AWSTemplateFormatVersion: '2010-09-09'
Resources:
ProbeRole:
Type: AWS::IAM::Role
Properties:
RoleName: probe-role-1
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal: {Service: ec2.amazonaws.com}
Action: sts:AssumeRole
ProbeProfile:
Type: AWS::IAM::InstanceProfile
Properties:
InstanceProfileName: probe-profile-1
Roles: [!Ref ProbeRole]
"""
cfn.create_stack(StackName="probe-ip", TemplateBody=TEMPLATE,
Capabilities=["CAPABILITY_NAMED_IAM"])
time.sleep(3)
cfn.delete_stack(StackName="probe-ip")
time.sleep(3)
print(cfn.describe_stacks(StackName="probe-ip")["Stacks"][0]["StackStatus"])
for r in cfn.describe_stack_resources(StackName="probe-ip")["StackResources"]:
print(" ", r["LogicalResourceId"], r["ResourceStatus"], r.get("ResourceStatusReason", ""))
iam.get_instance_profile(InstanceProfileName="probe-profile-1") # succeeds; it should be goneThe direct-IAM half:
TRUST = ('{"Version":"2012-10-17","Statement":[{"Effect":"Allow",'
'"Principal":{"Service":"ec2.amazonaws.com"},"Action":"sts:AssumeRole"}]}')
iam.create_role(RoleName="direct-role", AssumeRolePolicyDocument=TRUST)
iam.create_instance_profile(InstanceProfileName="direct-profile")
iam.add_role_to_instance_profile(InstanceProfileName="direct-profile", RoleName="direct-role")
iam.delete_role(RoleName="direct-role") # real IAM: DeleteConflict. substrate: 200Why it matters downstream
Found in parsl-ephemeral-provider, whose detached mode deploys a bastion from a CloudFormation template carrying exactly this role + instance-profile pair. Two consequences:
- A leaked profile per test. Every integration test that deploys and tears down that stack leaves
probe-profile-1's equivalent behind. Because the profile keeps its physical name, and because names are account-global (see CloudFormation: a resource with no explicit physical name gets the logical ID, so a second stack from the same template always collides #560), the next run of the same test collides onCreateInstanceProfile— so the emulator accumulates state that makes a passing suite fail on its second run. - Teardown correctness is untestable here. The provider's own IAM teardown must issue
RemoveRoleFromInstanceProfile→DeleteInstanceProfile→ detach managed → delete inline →DeleteRole, in that order, because real IAM rejects any other. An emulator that acceptsDeleteRoleon an attached role will pass a teardown that is wrong against real AWS — which is precisely the class of defect that shipped a leak in that project (its feat: RDS plugin — CreateDBInstance, DescribeDBInstances, ModifyDBInstance, DeleteDBInstance #132 and fix: Suppress spurious "failed to close HTTP response body" SDK warning in test output #229, ~94 orphaned roles in a live account before it was caught).
The second point is the one I would weight: an emulator that is more permissive than IAM on an ordering constraint silently certifies broken cleanup code.
Relationship to #560
Independent, but they compound. #560 means a stack's resources take their logical IDs as physical names; this issue means those names survive teardown. Together, any template deployed twice collides on a name the first stack promised to delete. Both need to land before the downstream project can move its pin off 0.88.0.
Activity
scttfrdmn commentedon Aug 6, 2026
Both defects reproduce on
main(97718ff), including the inversion, and the stack cannot converge.Via CloudFormation.
create→CREATE_COMPLETE, profile roles['probe-role-1'].delete→DELETE_FAILED, withProbeRole DELETE_COMPLETEandProbeProfile DELETE_FAILED DeleteConflict. Afterwardsget-role→NoSuchEntitybut the profile still exists. A secondDeleteStackreturnsDELETE_FAILEDagain and the profile is still there — so there is no retry path, and noRetainResourcesescape either, because the resource that fails is the one still present.Defect 1 in isolation, no CloudFormation involved. Direct IAM calls:
DeleteRoleon a role held by an instance profile succeeded. The profile then lists a role that no longer exists, andDeleteInstanceProfilerefuses it withDeleteConflict. Recoverable by hand (RemoveRoleFromInstanceProfile, then delete), but nothing in the CFN delete path makes that call.The
DeleteRolereference documents exactly what is missing: "Before attempting to delete a role, remove the following attached items: … Instance profile (RemoveRoleFromInstanceProfile)".DeleteConflict— "attempted to delete a resource that has attached subordinate entities" — is HTTP 409, and substrate already returns that code and status for the attached-policies case two blocks up indeleteRole(emulator/iam_plugin.go:489), so this is filling in a case the same function already handles for a sibling constraint.listInstanceProfiles(iam_plugin.go:2626) already does theinstance_profile:sweepDeleteRoleneeds.Design note on the second half.
cfnDeleteRequestFuncreturns exactly one request per resource, deliberately, "so every entry stays a data declaration" (emulator/cfn_delete.go:11-27). Rather than weaken that contract for one type, the profile's detach lands as an optional pre-step — acfnDeletePreStepsmap consulted bydeleteOneResourcebefore the main request and dispatched through the existingdispatchResourceDelete, so absent-resource tolerance and event recording are unchanged. The profile's pre-step emits oneRemoveRoleFromInstanceProfileper role in its declaredRoles.On ordering:
AWS::IAM::Roleis priority 1 andAWS::IAM::InstanceProfileis 2, and the delete sweep sorts descending, so the profile is already deleted before the role — which is why the pre-step is sufficient on its own. But the probe shows the role reachingDELETE_COMPLETEregardless, so the test will assert the dispatch order rather than trust the priority table.As the issue says, fixing either defect alone flips the symptom instead of fixing it, so both land in one PR. Milestoned into v0.94.0.
fix(cfn,iam): a stack holding an instance profile can be deleted (#581)…