From fbc0d4d3f2b2c45569f2e63784e8d9ecc5b7563a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C5=A9=20Th=E1=BA=BF=20Huy?= <31652864+vuthehuyht@users.noreply.github.com> Date: Sat, 19 Sep 2026 09:51:38 +0700 Subject: [PATCH] fix(cloudformation): update nested stacks in place (#3859) --- .../cloudformation/CloudFormationService.java | 22 +++++-- ...ormationDeletionPolicyIntegrationTest.java | 63 +++++++++++++++++++ 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java index 49052d97a8..6415f8cb21 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java @@ -1206,7 +1206,7 @@ public class CloudFormationService implements ResourceProvider { if ("AWS::CloudFormation::Stack".equals(type)) { resource = executeNestedStack(stack, logicalId, props.isMissingNode() ? null : props, - engine, region, accountId, isCreate); + engine, region, accountId, isCreate, previousResource); } else { resource = provisioner.provision(logicalId, type, props.isMissingNode() ? null : props, engine, region, accountId, stack.getStackName(), @@ -2293,7 +2293,8 @@ public class CloudFormationService implements ResourceProvider { private StackResource executeNestedStack(Stack parentStack, String logicalId, JsonNode props, CloudFormationTemplateEngine engine, String region, - String accountId, boolean isCreate) { + String accountId, boolean isCreate, + StackResource previousResource) { StackResource resource = new StackResource(); resource.setLogicalId(logicalId); resource.setResourceType("AWS::CloudFormation::Stack"); @@ -2308,9 +2309,18 @@ public class CloudFormationService implements ResourceProvider { String childTemplate = fetchTemplateFromS3(templateUrl); String childStackName = parentStack.getStackName() + "-" + logicalId; - Stack childStack = newStack(childStackName, region, accountId); - childStack.setStatus("CREATE_IN_PROGRESS"); - stacks.put(stackKey(accountId, childStackName, region), childStack); + Stack childStack = null; + boolean childCreate = isCreate || previousResource == null + || previousResource.getPhysicalId() == null; + if (!childCreate) { + childStack = resolveStack(previousResource.getPhysicalId(), region, accountId); + childCreate = childStack == null; + } + if (childCreate) { + childStack = newStack(childStackName, region, accountId); + childStack.setStatus("CREATE_IN_PROGRESS"); + stacks.put(stackKey(accountId, childStackName, region), childStack); + } Map childParams = new LinkedHashMap<>(); if (props != null && props.has("Parameters") && props.get("Parameters").isObject()) { @@ -2318,7 +2328,7 @@ public class CloudFormationService implements ResourceProvider { childParams.put(e.getKey(), engine.resolve(e.getValue()))); } - executeTemplate(childStack, childTemplate, childParams, isCreate, region, accountId); + executeTemplate(childStack, childTemplate, childParams, childCreate, region, accountId); resource.setPhysicalId(childStack.getStackId()); resource.getAttributes().put("Arn", childStack.getStackId()); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDeletionPolicyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDeletionPolicyIntegrationTest.java index 7aa903b4fd..84bb3ca9b6 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDeletionPolicyIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDeletionPolicyIntegrationTest.java @@ -366,6 +366,50 @@ class CloudFormationDeletionPolicyIntegrationTest { } } + @Test + void updateNestedStackReusesChildStackAndUpdatesNamedLambda() throws InterruptedException { + String suffix = Long.toString(System.nanoTime(), 36); + String functionName = "cfn-nested-update-func-" + suffix; + String bucketName = "nested-stack-templates-" + suffix; + String templateUrl = bucketName + "/child-lambda-update.json"; + String parentTemplate = """ + { + "Resources": { + "ChildStack": { + "Type": "AWS::CloudFormation::Stack", + "Properties": { "TemplateURL": "http://localhost/%s" } + } + } + } + """.formatted(templateUrl); + String stackName = "parent-nested-lambda-update-" + suffix; + + given().header("Authorization", CUSTOM_AUTH) + .when().put("/" + bucketName).then().statusCode(200); + given().header("Authorization", CUSTOM_AUTH) + .contentType("application/json").body(nestedLambdaTemplate(functionName, 3)) + .when().put("/" + templateUrl).then().statusCode(200); + String parentStackId = createStack(stackName, parentTemplate, CUSTOM_AUTH); + try { + awaitStackStatus(parentStackId, "CREATE_COMPLETE", CUSTOM_AUTH); + String childStackId = getNestedStackId(parentStackId, "ChildStack", CUSTOM_AUTH); + + given().header("Authorization", CUSTOM_AUTH) + .contentType("application/json").body(nestedLambdaTemplate(functionName, 9)) + .when().put("/" + templateUrl).then().statusCode(200); + updateStack(stackName, parentTemplate, CUSTOM_AUTH); + awaitStackStatus(parentStackId, "UPDATE_COMPLETE", CUSTOM_AUTH); + + assertThat(getNestedStackId(parentStackId, "ChildStack", CUSTOM_AUTH), equalTo(childStackId)); + given().header("Authorization", CUSTOM_AUTH) + .when().get("/2015-03-31/functions/" + functionName) + .then().statusCode(200) + .body("Configuration.Timeout", equalTo(9)); + } finally { + deleteStack(stackName); + } + } + @Test void nestedStackWithNonEmptyBucketFailsDeletionOnUpdate_leavesChildAsDeleteFailedAndTracksInParent() throws InterruptedException { String suffix = Long.toString(System.nanoTime(), 36); @@ -967,6 +1011,25 @@ class CloudFormationDeletionPolicyIntegrationTest { return xml.substring(physStart, physEnd); } + private static String nestedLambdaTemplate(String functionName, int timeout) { + return """ + { + "Resources": { + "Function": { + "Type": "AWS::Lambda::Function", + "Properties": { + "FunctionName": "%s", + "Runtime": "nodejs20.x", + "Handler": "index.handler", + "Timeout": %d, + "Role": "arn:aws:iam::000000000000:role/cfn-test-lambda-role" + } + } + } + } + """.formatted(functionName, timeout); + } + private static String createStack(String stackName, String template) { return createStack(stackName, template, null); }