fix(cloudformation): AWS::IAM::ManagedPolicy exposes PolicyArn for Fn::GetAtt (#2056)

CloudFormation documents PolicyArn as the attribute for this type, so a
template written against AWS reads it back with
Fn::GetAtt [<policy>, PolicyArn]. The provisioner only recorded the value under
"Arn", so that GetAtt did not resolve and the unresolved literal propagated into
whatever consumed it — typically a role's ManagedPolicyArns, which then failed
with "Policy <logicalId>.PolicyArn does not exist" and rolled the stack back,
several resources away from the cause.

Both keys are now recorded; "Arn" stays for anything already reading it.
This commit is contained in:
lex00 authored and GitHub committed 2026-08-05 22:16:48 -05:00
1 parent 875e0f2d3b
commit f71903379c
2 files changed
+28

No files matched your search

@@ -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<String> attachedRoleNames = new LinkedHashSet<>();
@@ -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");