diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java index 5555e2f2ef..c7f9134242 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java @@ -2201,7 +2201,13 @@ public class CloudFormationResourceProvisioner { var policy = iamService.createPolicy(policyName, "/", null, document, Map.of()); r.getAttributes().put(ROLLBACK_OWNED_ATTR, "true"); r.setPhysicalId(policy.getArn()); + // PolicyArn is the attribute CloudFormation documents for this type, and what a template + // written against AWS asks for. Without it Fn::GetAtt does not resolve and the unresolved + // literal reaches whatever consumed it — a role's ManagedPolicyArns, typically, which then + // fails with "policy does not exist" and rolls the stack back. "Arn" stays for callers + // already using it. r.getAttributes().put("Arn", policy.getArn()); + r.getAttributes().put("PolicyArn", policy.getArn()); r.getAttributes().put("ManagedPolicyRoleTargets", String.join("\n", roleNames)); LinkedHashSet attachedRoleNames = new LinkedHashSet<>(); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIamAttachmentProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIamAttachmentProvisionerTest.java index 22051fa071..6f270e6822 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIamAttachmentProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIamAttachmentProvisionerTest.java @@ -190,6 +190,28 @@ class CloudFormationIamAttachmentProvisionerTest { verify(iamService, never()).deleteRole("existing-role"); } + @Test + void managedPolicyExposesPolicyArnForGetAtt() { + // PolicyArn is the attribute CloudFormation documents for AWS::IAM::ManagedPolicy. Without + // it Fn::GetAtt does not resolve and the unresolved literal reaches the consuming resource. + String policyArn = "arn:aws:iam::" + ACCOUNT_ID + ":policy/test-policy"; + IamPolicy policy = new IamPolicy(); + policy.setArn(policyArn); + when(iamService.createPolicy("test-policy", "/", null, policyDocument(), Map.of())) + .thenReturn(policy); + + StackResource result = provision("ManagedPolicy", "AWS::IAM::ManagedPolicy", """ + { + "ManagedPolicyName": "test-policy", + "PolicyDocument": {"Version": "2012-10-17", "Statement": []} + } + """); + + assertEquals(policyArn, result.getAttributes().get("PolicyArn")); + assertEquals(policyArn, result.getAttributes().get("Arn")); + assertEquals(policyArn, result.getPhysicalId()); + } + @Test void cleanupFailureDoesNotMaskPrimaryFailureOrSkipDeletion() { IamRole role = role("cleanup-role");