Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cloudformation): propagate nested stack failure status reason and cover changeset rollbacks (#3921)
Vũ Thế Huya5254defb62d
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.javaindex cfcb72271b..f9239c5a93 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@@ -2418,7 +2418,13 @@ public class CloudFormationService implements ResourceProvider {resource.setStatus("CREATE_COMPLETE");} else {resource.setStatus("CREATE_FAILED");- resource.setStatusReason("Nested stack " + childStackName + " failed: " + childStack.getStatusReason());+ String reason = childStack.getStatusReason();+ if (reason == null || reason.isBlank()) {+ reason = "Nested stack " + childStackName + " rolled back or failed with status " + childStack.getStatus();+ } else {+ reason = "Nested stack " + childStackName + " failed: " + reason;+ }+ resource.setStatusReason(reason);}return resource;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.javaindex bd7f2ddf86..b8c10a11fc 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@@ -765,6 +765,216 @@ class CloudFormationDeletionPolicyIntegrationTest {}}+ @Test+ void createChangeSet_nestedStackPreExistingLogGroup_rollsBackParentStack() throws Exception {+ String suffix = Long.toString(System.nanoTime(), 36);+ String logGroupName = "/aws/lambda/demo-fn-" + suffix;+ String bucketName = "nested-stack-templates-" + suffix;+ String templateUrl = bucketName + "/child-loggroup-" + suffix + ".json";++ // Pre-create log group to induce ResourceAlreadyExists failure+ given()+ .header("X-Amz-Target", "Logs_20140328.CreateLogGroup")+ .contentType("application/x-amz-json-1.1")+ .body("{\"logGroupName\":\"" + logGroupName + "\"}")+ .when().post("/").then().statusCode(200);++ String childTemplate = """+ {+ "Resources": {+ "MyLogGroup": {+ "Type": "AWS::Logs::LogGroup",+ "Properties": { "LogGroupName": "%s" }+ }+ }+ }+ """.formatted(logGroupName);++ given().when().put("/" + bucketName).then().statusCode(200);+ given().contentType("application/json").body(childTemplate)+ .when().put("/" + templateUrl).then().statusCode(200);++ String parentTemplate = """+ {+ "Resources": {+ "ChildStack": {+ "Type": "AWS::CloudFormation::Stack",+ "Properties": { "TemplateURL": "http://localhost/%s" }+ }+ }+ }+ """.formatted(templateUrl);++ String stackName = "parent-nested-fail-" + suffix;+ String changeSetName = "cdk-deploy-change-set-" + suffix;++ try {+ // CDK workflow: CreateChangeSet -> ExecuteChangeSet+ given().contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateChangeSet")+ .formParam("StackName", stackName)+ .formParam("ChangeSetName", changeSetName)+ .formParam("ChangeSetType", "CREATE")+ .formParam("TemplateBody", parentTemplate)+ .when().post("/").then().statusCode(200);++ given().contentType("application/x-www-form-urlencoded")+ .formParam("Action", "ExecuteChangeSet")+ .formParam("StackName", stackName)+ .formParam("ChangeSetName", changeSetName)+ .when().post("/").then().statusCode(200);++ // Await parent stack terminal status+ long deadline = System.currentTimeMillis() + 15_000;+ String parentStatus = "";+ while (System.currentTimeMillis() < deadline) {+ String xml = describeStacks(stackName);+ int start = xml.indexOf("<StackStatus>") + "<StackStatus>".length();+ int end = xml.indexOf("</StackStatus>", start);+ if (start > "<StackStatus>".length() && end > start) {+ parentStatus = xml.substring(start, end);+ if (!parentStatus.endsWith("_IN_PROGRESS")) {+ break;+ }+ }+ Thread.sleep(100);+ }++ assertThat(parentStatus, equalTo("ROLLBACK_COMPLETE"));++ String events = describeStackEvents(stackName);+ assertThat(events, containsString("<LogicalResourceId>ChildStack</LogicalResourceId>"));+ assertThat(events, containsString("<ResourceStatus>CREATE_FAILED</ResourceStatus>"));+ assertThat(events, containsString("<ResourceStatusReason>Nested stack " + stackName+ + "-ChildStack failed: The specified log group already exists: " + logGroupName+ + "</ResourceStatusReason>"));+ } finally {+ deleteStack(stackName);+ }+ }++ @Test+ void updateChangeSet_nestedStackUpdateFails_rollsBackParentStack() throws Exception {+ String suffix = Long.toString(System.nanoTime(), 36);+ String bucketName = "nested-stack-templates-" + suffix;+ String templateUrl = bucketName + "/child-update-" + suffix + ".json";+ String logGroupName = "/aws/lambda/good-fn-" + suffix;++ String childTemplateV1 = """+ {+ "Resources": {+ "MyLogGroup": {+ "Type": "AWS::Logs::LogGroup",+ "Properties": { "LogGroupName": "%s" }+ }+ }+ }+ """.formatted(logGroupName);++ given().when().put("/" + bucketName).then().statusCode(200);+ given().contentType("application/json").body(childTemplateV1)+ .when().put("/" + templateUrl).then().statusCode(200);++ String parentTemplate = """+ {+ "Resources": {+ "ChildStack": {+ "Type": "AWS::CloudFormation::Stack",+ "Properties": { "TemplateURL": "http://localhost/%s" }+ }+ }+ }+ """.formatted(templateUrl);++ String stackName = "parent-update-fail-" + suffix;+ String changeSetName1 = "cdk-deploy-create-" + suffix;++ try {+ // Create stack via ChangeSet+ given().contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateChangeSet")+ .formParam("StackName", stackName)+ .formParam("ChangeSetName", changeSetName1)+ .formParam("ChangeSetType", "CREATE")+ .formParam("TemplateBody", parentTemplate)+ .when().post("/").then().statusCode(200);++ given().contentType("application/x-www-form-urlencoded")+ .formParam("Action", "ExecuteChangeSet")+ .formParam("StackName", stackName)+ .formParam("ChangeSetName", changeSetName1)+ .when().post("/").then().statusCode(200);++ awaitStackStatus(stackName, "CREATE_COMPLETE");++ // Now update child template with an unprovisionable/failing resource placed first+ String badSecretName = "bad-secret-" + suffix;+ String childTemplateV2 = """+ {+ "Resources": {+ "BadSecret": {+ "Type": "AWS::SecretsManager::Secret",+ "Properties": {+ "Name": "%s",+ "SecretString": "explicit",+ "GenerateSecretString": { "PasswordLength": 32 }+ }+ },+ "MyLogGroup": {+ "Type": "AWS::Logs::LogGroup",+ "Properties": { "LogGroupName": "%s" }+ }+ }+ }+ """.formatted(badSecretName, logGroupName);++ given().contentType("application/json").body(childTemplateV2)+ .when().put("/" + templateUrl).then().statusCode(200);++ // Update parent stack via ChangeSet+ String changeSetName2 = "cdk-deploy-update-" + suffix;+ given().contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateChangeSet")+ .formParam("StackName", stackName)+ .formParam("ChangeSetName", changeSetName2)+ .formParam("ChangeSetType", "UPDATE")+ .formParam("TemplateBody", parentTemplate)+ .when().post("/").then().statusCode(200);++ given().contentType("application/x-www-form-urlencoded")+ .formParam("Action", "ExecuteChangeSet")+ .formParam("StackName", stackName)+ .formParam("ChangeSetName", changeSetName2)+ .when().post("/").then().statusCode(200);++ // Await parent stack terminal status+ long deadline = System.currentTimeMillis() + 15_000;+ String parentStatus = "";+ while (System.currentTimeMillis() < deadline) {+ String xml = describeStacks(stackName);+ int start = xml.indexOf("<StackStatus>") + "<StackStatus>".length();+ int end = xml.indexOf("</StackStatus>", start);+ if (start > "<StackStatus>".length() && end > start) {+ parentStatus = xml.substring(start, end);+ if (!parentStatus.endsWith("_IN_PROGRESS")) {+ break;+ }+ }+ Thread.sleep(100);+ }++ assertThat(parentStatus, equalTo("UPDATE_ROLLBACK_COMPLETE"));++ String events = describeStackEvents(stackName);+ assertThat(events, containsString("<LogicalResourceId>ChildStack</LogicalResourceId>"));+ assertThat(events, containsString("<ResourceStatusReason>Nested stack " + stackName+ + "-ChildStack rolled back or failed with status UPDATE_ROLLBACK_COMPLETE</ResourceStatusReason>"));+ assertThat(events, not(containsString("failed: null")));+ } finally {+ deleteStack(stackName);+ }+ }+@Testvoid updateStack_rollbackWithNestedStack_deletesNewlyAddedChildStack() throws InterruptedException {String suffix = Long.toString(System.nanoTime(), 36);