fix(cloudformation): implement nested stack deployment via AWS::CloudFormation::Stack (#1052)

Previously AWS::CloudFormation::Stack was stubbed — it returned a synthetic
stack ID but never fetched or deployed the child template, so all resources
defined in nested stacks were silently skipped.

Intercept AWS::CloudFormation::Stack in CloudFormationService.executeTemplate()
before delegating to the provisioner. Resolve TemplateURL via the template
engine, fetch the child template from the bootstrap S3 bucket, create a child
Stack, and execute it recursively on the same thread. Child outputs are surfaced
as Outputs.X attributes on the parent resource so Fn::GetAtt cross-stack
references resolve correctly.

Removes the provisionNestedStack stub from CloudFormationResourceProvisioner.

Closes #1051
This commit is contained in:
Matt Lishman authored and GitHub committed 2026-05-26 21:21:46 -05:00
1 parent b07c9ef28b
commit ec948fe3a8
3 files changed
+141 -15

No files matched your search

@@ -153,7 +153,6 @@ public class CloudFormationResourceProvisioner {
case "AWS::KMS::Key" -> provisionKmsKey(resource, properties, engine, region, accountId);
case "AWS::KMS::Alias" -> provisionKmsAlias(resource, properties, engine, region);
case "AWS::SecretsManager::Secret" -> provisionSecret(resource, properties, engine, region, accountId, stackName);
case "AWS::CloudFormation::Stack" -> provisionNestedStack(resource, properties, engine, region);
case "AWS::CDK::Metadata" -> provisionCdkMetadata(resource);
case "AWS::S3::BucketPolicy" -> provisionS3BucketPolicy(resource, properties, engine);
case "AWS::SQS::QueuePolicy" -> provisionSqsQueuePolicy(resource, properties, engine);
@@ -1180,17 +1179,6 @@ public class CloudFormationResourceProvisioner {
return password;
}
// ── Nested Stack ──────────────────────────────────────────────────────────
private void provisionNestedStack(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String region) {
// Nested stacks are stubbed — return a synthetic stack ID
String nestedId = AwsArnUtils.Arn.of("cloudformation", region, "", "stack/nested-" + UUID.randomUUID().toString().substring(0, 8) + "/").toString();
r.setPhysicalId(nestedId);
r.getAttributes().put("Arn", nestedId);
r.getAttributes().put("Outputs.BootstrapVersion", "21");
}
// ── EventBridge ─────────────────────────────────────────────────────────
private void provisionEventBridgeRule(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
@@ -307,9 +307,15 @@ public class CloudFormationService {
}
addEvent(stack, logicalId, null, type, "CREATE_IN_PROGRESS", null);
resource = provisioner.provision(logicalId, type, props.isMissingNode() ? null : props,
engine, region, accountId, stack.getStackName(),
resource.getPhysicalId(), resource.getAttributes());
if ("AWS::CloudFormation::Stack".equals(type)) {
resource = executeNestedStack(stack, logicalId,
props.isMissingNode() ? null : props,
engine, region, accountId, isCreate);
} else {
resource = provisioner.provision(logicalId, type, props.isMissingNode() ? null : props,
engine, region, accountId, stack.getStackName(),
resource.getPhysicalId(), resource.getAttributes());
}
stack.getResources().put(logicalId, resource);
physicalIds.put(logicalId, resource.getPhysicalId());
@@ -562,6 +568,49 @@ public class CloudFormationService {
&& normalizedHost.endsWith("." + normalizedSuffix);
}
private StackResource executeNestedStack(Stack parentStack, String logicalId, JsonNode props,
CloudFormationTemplateEngine engine, String region,
String accountId, boolean isCreate) {
StackResource resource = new StackResource();
resource.setLogicalId(logicalId);
resource.setResourceType("AWS::CloudFormation::Stack");
String templateUrl = props != null ? engine.resolve(props.path("TemplateURL")) : null;
if (templateUrl == null || templateUrl.isBlank()) {
resource.setStatus("CREATE_FAILED");
resource.setStatusReason("Missing TemplateURL");
return resource;
}
String childTemplate = fetchTemplateFromS3(templateUrl);
String childStackName = parentStack.getStackName() + "-" + logicalId;
Stack childStack = newStack(childStackName, region);
childStack.setStatus("CREATE_IN_PROGRESS");
stacks.put(key(childStackName, region), childStack);
Map<String, String> childParams = new LinkedHashMap<>();
if (props != null && props.has("Parameters") && props.get("Parameters").isObject()) {
props.get("Parameters").fields().forEachRemaining(e ->
childParams.put(e.getKey(), engine.resolve(e.getValue())));
}
executeTemplate(childStack, childTemplate, childParams, isCreate, region, accountId);
resource.setPhysicalId(childStack.getStackId());
resource.getAttributes().put("Arn", childStack.getStackId());
childStack.getOutputs().forEach((k, v) -> resource.getAttributes().put("Outputs." + k, v));
if ("CREATE_FAILED".equals(childStack.getStatus()) || "UPDATE_FAILED".equals(childStack.getStatus())) {
resource.setStatus("CREATE_FAILED");
resource.setStatusReason("Nested stack " + childStackName + " failed: " + childStack.getStatusReason());
} else {
resource.setStatus("CREATE_COMPLETE");
}
return resource;
}
private Stack newStack(String stackName, String region) {
Stack stack = new Stack();
stack.setStackName(stackName);