diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md
index 24e3a2eb1b..7e63aa3be5 100644
--- a/docs/services/cloudformation.md
+++ b/docs/services/cloudformation.md
@@ -1060,6 +1060,41 @@ A stack that reached `UPDATE_ROLLBACK_FAILED` under an earlier Floci version kep
state before the update. `ContinueUpdateRollback` then retries every `UPDATE_FAILED` resource and
keeps the template the stack holds.
+## Stack status events
+
+Every status a stack records, root or nested, goes to the default EventBridge bus of the stack's
+account and Region, with source `aws.cloudformation` and detail type
+`CloudFormation Stack Status Change`, as
+[CloudFormation sends it](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/eventbridge-integration.html).
+`resources` holds the stack's ID, and the
+[detail](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/event-detail-stack-status-change.html)
+holds `stack-id`, `client-request-token` where there is one, and `status-details` with `status`,
+`detailed-status` and `status-reason`, each of the last two empty where there is nothing to say. A
+resource's progress sends nothing, a parent's record of a nested stack included. Each status is sent
+once.
+
+A failed create or update ends as it does on AWS. A root whose nested stack fails goes from
+`CREATE_IN_PROGRESS` to `ROLLBACK_IN_PROGRESS` and `ROLLBACK_COMPLETE`, or from `UPDATE_IN_PROGRESS`
+through `UPDATE_ROLLBACK_IN_PROGRESS` and `UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS` to
+`UPDATE_ROLLBACK_COMPLETE`, with no `CREATE_FAILED` or `UPDATE_FAILED`. A nested stack whose create
+fails sends `CREATE_FAILED`, and its rollback shows as `DELETE_IN_PROGRESS` and `DELETE_COMPLETE`.
+A failure in a root's own resource with its nested stack healthy, and a delete that fails, send the
+statuses CloudFormation documents from the same code; neither was read against AWS.
+
+The `client-request-token` is the `ClientRequestToken` of the operation that caused the status:
+`ExecuteChangeSet`, `CreateStack`, `UpdateStack`, `DeleteStack` or `ContinueUpdateRollback`, called
+directly or from a state machine's `aws-sdk:cloudformation` task. A rollback's statuses carry the token
+of the create or update that failed. The key is absent, never empty, on
+a nested stack's events, on the `REVIEW_IN_PROGRESS` of a new stack, and on the events of an operation
+given no token.
+
+Events are delivered in order by one background worker, apart from the stack operation and outside any
+lock on the stack. The status an operation starts with is sent as the operation begins to run, so one
+the operation limit refuses sends nothing. A delivery that fails is logged and changes nothing about
+the operation, and a slow target delays later events, never a stack. The worker's queue is in memory: an event still in it when
+Floci stops is dropped and counted in the log, and is not sent on the next start, even where the stack
+is persisted.
+
## Deleted Stacks
A deleted stack is kept for a short window afterwards rather than being forgotten at
diff --git a/src/main/java/io/floci/aws/services/cloudformation/CloudFormationEventPublisher.java b/src/main/java/io/floci/aws/services/cloudformation/CloudFormationEventPublisher.java
new file mode 100644
index 0000000000..89167ba3ee
--- /dev/null
+++ b/src/main/java/io/floci/aws/services/cloudformation/CloudFormationEventPublisher.java
@@ -0,0 +1,144 @@
+package io.floci.aws.services.cloudformation;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import io.floci.aws.core.common.RequestScopes;
+import io.floci.aws.services.cloudformation.model.Stack;
+import io.floci.aws.services.eventbridge.EventBridgeService;
+import jakarta.annotation.PostConstruct;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import org.jboss.logging.Logger;
+
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Publishes AWS-shaped {@code aws.cloudformation} "CloudFormation Stack Status Change" events to the
+ * default EventBridge bus of the stack's account and Region, one per status recorded for a stack.
+ *
+ *
Unlike the other publishers this one only queues, and one daemon worker delivers in order. A
+ * status can be recorded while the service holds the stack's lock in {@code stacks.compute}, and
+ * delivery calls a rule's targets before it returns, so delivering in place would hold that lock and
+ * the operation for as long as the targets take.
+ *
+ *
The queue is in memory. An event still in it when Floci stops is dropped, and counted in the log:
+ * it is not sent on the next start, even where the stack itself is persisted.
+ */
+@ApplicationScoped
+public class CloudFormationEventPublisher {
+
+ private static final Logger LOG = Logger.getLogger(CloudFormationEventPublisher.class);
+ static final String SOURCE = "aws.cloudformation";
+ static final String DETAIL_TYPE = "CloudFormation Stack Status Change";
+ private static final long STATUS_WRITE_WAIT_NANOS = TimeUnit.SECONDS.toNanos(1);
+
+ private record StatusChange(Stack stack, String accountId, String statusWhenRecorded, String status,
+ String reason, String token) {
+ }
+
+ private final BlockingQueue pending = new LinkedBlockingQueue<>();
+ private final EventBridgeService eventBridgeService;
+ private final ObjectMapper objectMapper;
+ private Thread worker;
+
+ @Inject
+ public CloudFormationEventPublisher(EventBridgeService eventBridgeService, ObjectMapper objectMapper) {
+ this.eventBridgeService = eventBridgeService;
+ this.objectMapper = objectMapper;
+ }
+
+ @PostConstruct
+ void start() {
+ worker = new Thread(this::deliverInOrder, "cloudformation-event-publisher");
+ worker.setDaemon(true);
+ worker.start();
+ }
+
+ @PreDestroy
+ void stop() {
+ worker.interrupt();
+ }
+
+ /** The worker's state: sleeping in {@link #awaitStatusWrite} is TIMED_WAITING, stopped is TERMINATED. */
+ Thread.State workerState() {
+ return worker.getState();
+ }
+
+ /**
+ * Queues one event for {@code status} and returns at once, so it is safe under the stack's lock.
+ * The token is the one on the stack now, the operation that caused this status.
+ */
+ public void stackStatusChanged(Stack stack, String accountId, String status, String reason) {
+ pending.add(new StatusChange(stack, accountId, stack.getStatus(), status, reason,
+ stack.getClientRequestToken()));
+ }
+
+ private void deliverInOrder() {
+ StatusChange held = null;
+ try {
+ while (true) {
+ held = pending.take();
+ awaitStatusWrite(held);
+ if (Thread.interrupted()) {
+ throw new InterruptedException();
+ }
+ deliver(held);
+ held = null;
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ int dropped = pending.size() + (held != null ? 1 : 0);
+ if (dropped > 0) {
+ LOG.warnv("Dropping {0} queued CloudFormation Stack Status Change event(s): Floci is stopping", dropped);
+ }
+ }
+ }
+
+ /**
+ * A failed path records its event before it writes the status, the volatile publishing write, so
+ * that a reader who sees the status also sees the event in DescribeStackEvents. A reader the bus
+ * event wakes must see the status too, so delivery waits out the few statements between the two.
+ * Bounded, since a status left unwritten must not hold up every later event.
+ */
+ private static void awaitStatusWrite(StatusChange change) throws InterruptedException {
+ if (Objects.equals(change.statusWhenRecorded(), change.status())) {
+ return;
+ }
+ long deadline = System.nanoTime() + STATUS_WRITE_WAIT_NANOS;
+ while (Objects.equals(change.statusWhenRecorded(), change.stack().getStatus())
+ && System.nanoTime() < deadline) {
+ Thread.sleep(1);
+ }
+ }
+
+ private void deliver(StatusChange change) {
+ String stackId = change.stack().getStackId();
+ String region = change.stack().getRegion();
+ try {
+ String detail = objectMapper.writeValueAsString(buildDetail(change));
+ RequestScopes.runAs(change.accountId(), region,
+ () -> eventBridgeService.publishServiceEvent(SOURCE, DETAIL_TYPE, detail, stackId, region));
+ } catch (Exception e) {
+ LOG.warnv("Failed to emit CloudFormation Stack Status Change for {0}: {1}", stackId, e.getMessage());
+ }
+ }
+
+ // As CloudFormation sent them on 2026-10-30: the key is absent, not empty, where there is no token,
+ // and detailed-status and status-reason are present and empty where there is nothing to say.
+ private ObjectNode buildDetail(StatusChange change) {
+ ObjectNode detail = objectMapper.createObjectNode();
+ detail.put("stack-id", change.stack().getStackId());
+ if (change.token() != null && !change.token().isEmpty()) {
+ detail.put("client-request-token", change.token());
+ }
+ ObjectNode statusDetails = detail.putObject("status-details");
+ statusDetails.put("status", change.status());
+ statusDetails.put("detailed-status", "");
+ statusDetails.put("status-reason", change.reason() != null ? change.reason() : "");
+ return detail;
+ }
+}
diff --git a/src/main/java/io/floci/aws/services/cloudformation/CloudFormationQueryHandler.java b/src/main/java/io/floci/aws/services/cloudformation/CloudFormationQueryHandler.java
index ea35ff8fa0..7d03d304fb 100644
--- a/src/main/java/io/floci/aws/services/cloudformation/CloudFormationQueryHandler.java
+++ b/src/main/java/io/floci/aws/services/cloudformation/CloudFormationQueryHandler.java
@@ -125,7 +125,8 @@ public class CloudFormationQueryHandler {
templateBody, templateUrl, parameters, capabilities, tags, region,
terminationProtection, stackPolicy);
try {
- cfnService.executeChangeSet(initial.getStackId(), initial.getChangeSetId(), region);
+ cfnService.executeChangeSetWithToken(initial.getStackId(), initial.getChangeSetId(), region,
+ params.getFirst("ClientRequestToken"));
} catch (AwsException e) {
if (terminationProtection) {
cfnService.clearProtectionAfterRejectedCreate(stackName, initial, region);
@@ -171,7 +172,8 @@ public class CloudFormationQueryHandler {
cfnService.deleteChangeSet(stackName, cs.getChangeSetName(), region);
return xmlError("ValidationError", "No updates are to be performed.", 400);
}
- cfnService.executeChangeSet(cs.getStackId(), cs.getChangeSetId(), region);
+ cfnService.executeChangeSetWithToken(cs.getStackId(), cs.getChangeSetId(), region,
+ params.getFirst("ClientRequestToken"));
String xml = new XmlBuilder()
.start("UpdateStackResponse", CF_NS)
@@ -190,7 +192,7 @@ public class CloudFormationQueryHandler {
String stackName = params.getFirst("StackName");
List retainResources = extractList(params, "RetainResources.member.");
try {
- cfnService.deleteStack(stackName, retainResources, region);
+ cfnService.deleteStackForRequest(stackName, retainResources, region, params.getFirst("ClientRequestToken"));
String xml = new XmlBuilder()
.start("DeleteStackResponse", CF_NS)
.raw(AwsQueryResponse.responseMetadata())
@@ -208,10 +210,12 @@ public class CloudFormationQueryHandler {
String stackName = params.getFirst("StackName");
List resourcesToSkip = extractList(params, "ResourcesToSkip.member.");
boolean forceRollback = Boolean.parseBoolean(params.getFirst("ForceRollback"));
- // RoleARN and ClientRequestToken are accepted and have no effect: Floci assumes no role
- // for stack operations and does not deduplicate retried requests.
+ // RoleARN is accepted and has no effect: Floci assumes no role for stack operations.
+ // ClientRequestToken goes on the status events the operation sends; a repeated one is not
+ // refused, as Floci does not deduplicate retried requests.
try {
- cfnService.continueUpdateRollback(stackName, resourcesToSkip, forceRollback, region);
+ cfnService.continueUpdateRollback(stackName, resourcesToSkip, forceRollback, region,
+ params.getFirst("ClientRequestToken"));
} catch (AwsException e) {
return xmlError(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
}
@@ -362,7 +366,8 @@ public class CloudFormationQueryHandler {
String stackName = params.getFirst("StackName");
String changeSetName = params.getFirst("ChangeSetName");
try {
- cfnService.executeChangeSetForRequest(stackName, changeSetName, region);
+ cfnService.executeChangeSetForRequest(stackName, changeSetName, region,
+ params.getFirst("ClientRequestToken"));
} catch (AwsException e) {
return xmlError(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
}
diff --git a/src/main/java/io/floci/aws/services/cloudformation/CloudFormationService.java b/src/main/java/io/floci/aws/services/cloudformation/CloudFormationService.java
index c4ff48adfa..973e169a06 100644
--- a/src/main/java/io/floci/aws/services/cloudformation/CloudFormationService.java
+++ b/src/main/java/io/floci/aws/services/cloudformation/CloudFormationService.java
@@ -128,7 +128,10 @@ public class CloudFormationService implements ResourceProvider {
// The execution running on a stack, by stack id, completed once it and any delete it deferred end.
private final ConcurrentHashMap> runningExecutions = new ConcurrentHashMap<>();
// A delete requested while its stack's execution runs, by stack id, with the resources it retains.
- private final ConcurrentHashMap> deferredDeletes = new ConcurrentHashMap<>();
+ private final ConcurrentHashMap deferredDeletes = new ConcurrentHashMap<>();
+
+ /** A DeleteStack accepted while an execution runs: what it retains, and the token its statuses carry. */
+ private record DeferredDelete(List retain, String clientRequestToken) {}
private final ExecutorService executor = newOperationExecutor();
static ThreadPoolExecutor newOperationExecutor() {
@@ -149,6 +152,8 @@ public class CloudFormationService implements ResourceProvider {
private final SamTransformProcessor samTransformProcessor;
private final AwsIncludeProcessor awsIncludeProcessor;
private final Clock clock;
+ // Null on a service built by hand, as a unit test builds one, which then sends no events.
+ private final CloudFormationEventPublisher eventPublisher;
// Persisted state so stacks survive a restart (criteria #10, #11). The in-memory maps above are
// the live working copy; these backends are write-through + loaded on startup. Legacy records
@@ -157,13 +162,24 @@ public class CloudFormationService implements ResourceProvider {
private final AccountAwareStorageBackend exportBackend;
- @Inject
public CloudFormationService(CfnResourceDispatcher dispatcher, S3Service s3Service,
SsmService ssmService, GetStackOutputRoleAuthorizer stackOutputRoles,
CfnDynamicReferences dynamicReferences,
ObjectMapper objectMapper, EmulatorConfig config,
RegionResolver regionResolver, Clock clock,
StorageFactory storageFactory) {
+ this(dispatcher, s3Service, ssmService, stackOutputRoles, dynamicReferences, objectMapper, config,
+ regionResolver, clock, storageFactory, null);
+ }
+
+ @Inject
+ public CloudFormationService(CfnResourceDispatcher dispatcher, S3Service s3Service,
+ SsmService ssmService, GetStackOutputRoleAuthorizer stackOutputRoles,
+ CfnDynamicReferences dynamicReferences,
+ ObjectMapper objectMapper, EmulatorConfig config,
+ RegionResolver regionResolver, Clock clock,
+ StorageFactory storageFactory,
+ CloudFormationEventPublisher eventPublisher) {
this.dispatcher = dispatcher;
this.s3Service = s3Service;
this.ssmService = ssmService;
@@ -175,6 +191,7 @@ public class CloudFormationService implements ResourceProvider {
this.samTransformProcessor = new SamTransformProcessor(objectMapper);
this.awsIncludeProcessor = new AwsIncludeProcessor(objectMapper, s3Service);
this.clock = clock;
+ this.eventPublisher = eventPublisher;
this.stackBackend = storageFactory.create(
"cloudformation", "cloudformation-stacks.json", new TypeReference