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 970a4cc8ea..80b5f032f2 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 @@ -133,12 +133,27 @@ public class CloudFormationService { Map tags, String region) { String resolvedTemplate = resolveTemplate(templateBody, templateUrl); + // Detect first creation atomically: the mapping function runs at most once per key, so the + // flag is only set for the thread that actually creates the stack (no double-recording under + // concurrent CreateChangeSet calls). + boolean[] stackCreated = {false}; Stack stack = stacks.computeIfAbsent(key(stackName, region), k -> { + stackCreated[0] = true; Stack s = newStack(stackName, region); if (tags != null) s.getTags().putAll(tags); return s; }); + // A CREATE change set puts a brand-new stack into REVIEW_IN_PROGRESS. Record the matching + // stack-level event (as AWS and LocalStack do) so DescribeStackEvents is non-empty straight + // after change-set creation — tooling such as the AWS SAM CLI reads StackEvents[0] there and + // otherwise fails with an IndexError. (CreateChangeSet defaults a null type to CREATE.) + boolean isCreateType = changeSetType == null || "CREATE".equalsIgnoreCase(changeSetType); + if (stackCreated[0] && isCreateType) { + addEvent(stack, stack.getStackName(), stack.getStackId(), + "AWS::CloudFormation::Stack", "REVIEW_IN_PROGRESS", "User Initiated"); + } + ChangeSet cs = new ChangeSet(); cs.setChangeSetId(AwsArnUtils.Arn.of("cloudformation", region, regionResolver.getAccountId(), "changeSet/" + changeSetName + "/" + UUID.randomUUID()).toString()); cs.setChangeSetName(changeSetName); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java index c288dc5c7c..60de055cf6 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java @@ -1012,6 +1012,48 @@ class CloudFormationIntegrationTest { .body(containsString("arn-events-stack")); } + @Test + void createChangeSet_recordsReviewInProgressEvent() { + // A CREATE change set for a brand-new stack must record a REVIEW_IN_PROGRESS stack event, + // so DescribeStackEvents is non-empty right after CreateChangeSet (matching AWS/LocalStack). + // Tooling such as the AWS SAM CLI reads StackEvents[0] at this point. + String template = """ + { + "Resources": { + "MyBucket": { + "Type": "AWS::S3::Bucket", + "Properties": { "BucketName": "review-event-bucket" } + } + } + } + """; + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateChangeSet") + .formParam("StackName", "review-event-stack") + .formParam("ChangeSetName", "cs1") + .formParam("ChangeSetType", "CREATE") + .formParam("TemplateBody", template) + .when() + .post("/") + .then() + .statusCode(200); + + // Before the change set is executed the stack is REVIEW_IN_PROGRESS and must already + // carry a matching stack-level event. + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DescribeStackEvents") + .formParam("StackName", "review-event-stack") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("AWS::CloudFormation::Stack")) + .body(containsString("REVIEW_IN_PROGRESS")); + } + @Test void describeDeletedStackEvents_byArn_returnsDeleteCompleteEvents() throws Exception { String template = """