From 9bb1db3a5465fe4617f3288daa3aa2ebf3a5dc6a Mon Sep 17 00:00:00 2001 From: Vishal Goyal Date: Fri, 19 Jun 2026 01:45:53 +0200 Subject: [PATCH] fix(cloudformation): record REVIEW_IN_PROGRESS event on change-set creation (#1431) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(cloudformation): record REVIEW_IN_PROGRESS event on change-set creation A CREATE change set puts a brand-new stack into REVIEW_IN_PROGRESS, but createChangeSet recorded no stack event, so DescribeStackEvents returned an empty list for it. AWS and LocalStack return at least the REVIEW_IN_PROGRESS stack event; tooling that reads StackEvents[0] right after change-set creation (e.g. the AWS SAM CLI's get_last_event_time) therefore fails on floci with "list index out of range" before any resources are deployed. Record the REVIEW_IN_PROGRESS stack-level event when a CREATE change set creates a new stack, scoped to that path (nested stacks and update change sets are unaffected). Adds an integration test. Fixes #1429 Co-Authored-By: Claude Opus 4.8 (1M context) Signed-off-by: Vishal Goyal * fix(cloudformation): address review — atomic new-stack detection + CREATE gate Detect first stack creation via the computeIfAbsent mapping function (runs at most once per key, so it's atomic and won't double-record the event under concurrent CreateChangeSet calls) instead of a non-atomic containsKey pre-check, and only record the REVIEW_IN_PROGRESS event for CREATE-type change sets (null defaults to CREATE, matching createChangeSet). --- .../cloudformation/CloudFormationService.java | 15 +++++++ .../CloudFormationIntegrationTest.java | 42 +++++++++++++++++++ 2 files changed, 57 insertions(+) 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 = """