fix(cloudformation): send the stack's StackId to custom resources (#4861)

AWS documents StackId in every Create, Update and Delete request as the
ARN of the stack that contains the custom resource, and handlers rely on
it. #4973 now passes the engine's stack id and stashes it for Delete, but
an engine with no stack ARN still leaks a non-ARN: Cloud Control's
standalone engine uses the stack name as its id, so its custom resources
got StackId "cloudcontrol".

A stack id is now minted when there is no stack ARN to send. A resource
created through Cloud Control has its minted id stashed at Create, so its
Delete repeats it. A resource stashed by an earlier release with no
__FlociStackId, or with a non-ARN one, gets a freshly minted ARN on
Delete, because the delete path does not reach the stack.

An integration test drives Create, Update and Delete through a mocked
handler and checks the event's StackId against Ref AWS::StackId and
DescribeStacks, including a nested stack's own id, and the docs say so.
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-10-08 14:43:01 -04:00
1 parent ebd364e23e
commit f248eb51af
4 files changed
+276 -5

No files matched your search

@@ -116,9 +116,10 @@ public class CustomResourceCfnProvisioner implements CfnResourceProvisioner {
return;
}
String stackId = stackIdOrMinted(engine.stackId(), region, ctx.accountId(), ctx.stackName());
JsonNode response = invokeCustomResourceHandler(serviceToken, requestType, r.getLogicalId(),
r.getResourceType(), priorPhysicalId, resourceProperties, oldResourceProperties,
region, engine.stackId());
region, stackId);
String status = response.path("Status").asText("FAILED");
if (!"SUCCESS".equals(status)) {
@@ -146,7 +147,7 @@ public class CustomResourceCfnProvisioner implements CfnResourceProvisioner {
// Stash what a later Delete invocation needs (delete() only gets the StackResource).
r.getAttributes().put(CR_SERVICE_TOKEN_ATTR, serviceToken);
r.getAttributes().put(CR_PROPERTIES_ATTR, resourceProperties.toString());
r.getAttributes().put(CR_STACK_ID_ATTR, engine.stackId());
r.getAttributes().put(CR_STACK_ID_ATTR, stackId);
}
@Override
@@ -158,10 +159,10 @@ public class CustomResourceCfnProvisioner implements CfnResourceProvisioner {
}
ObjectNode stashed = readStashedProperties(r);
ObjectNode resourceProperties = stashed != null ? stashed : objectMapper.createObjectNode();
// A resource stashed before the stack id was recorded has none, and delete() has no stack.
String stackId = stackIdOrMinted(r.getAttributes().get(CR_STACK_ID_ATTR), region,
accountFromArn(serviceToken), "");
try {
// Resources stashed before the stack id was recorded fall back to a synthetic id.
String stackId = r.getAttributes().getOrDefault(CR_STACK_ID_ATTR, AwsArnUtils.Arn.of(
"cloudformation", region, accountFromArn(serviceToken), "stack//" + UUID.randomUUID()).toString());
JsonNode response = invokeCustomResourceHandler(serviceToken, "Delete", r.getLogicalId(),
r.getResourceType(), r.getPhysicalId(), resourceProperties, null, region, stackId);
if (!"SUCCESS".equals(response.path("Status").asText("FAILED"))) {
@@ -265,6 +266,19 @@ public class CustomResourceCfnProvisioner implements CfnResourceProvisioner {
return objectMapper.getNodeFactory().textNode(node.asText());
}
/**
* The {@code StackId} a request carries: the id of the stack that contains the resource, the
* value {@code Ref AWS::StackId} returns. A resource with no stack ARN, one provisioned through
* Cloud Control or one stashed before the id was recorded, gets one minted in its place.
*/
private static String stackIdOrMinted(String stackId, String region, String accountId, String stackName) {
if (AwsArnUtils.isArn(stackId)) {
return stackId;
}
return AwsArnUtils.Arn.of("cloudformation", region, accountId, "stack/"
+ (stackName == null ? "" : stackName) + "/" + UUID.randomUUID()).toString();
}
private static String accountFromArn(String arn) {
String account = AwsArnUtils.accountOrDefault(arn, "000000000000");
return account.matches("\\d{12}") ? account : "000000000000";