fix(cloudformation): reject resources that depend on each other in a cycle (#5208)

* fix(cloudformation): reject resources that depend on each other in a cycle

A template whose resources form a dependency cycle was created anyway: the
topological sort appended the resources it could not order and created them in
template order. CloudFormation rejects such a template with a ValidationError
naming the resources, before any of them is created.

The sort now throws that error when resources remain unsorted, and the
synchronous pre-check that runs before CreateStack, UpdateStack and
CreateChangeSet calls the sort instead of repeating its dependency checks, so
the request fails before any stack state is created.

* fix(cloudformation): keep dependency order when deleting a stack saved with a cyclic template

A stack saved before circular templates were rejected can still carry one.
Deleting it hit the new circular-dependency error while ordering its resources,
so the delete fell back to insertion order for every resource and could remove
a resource before one still using it. The delete now orders what it can and
places the circular resources after it, as before.

* fix(cloudformation): order resources that depend on a cycle when deleting a saved stack

A resource that depends on a dependency cycle cannot be ordered either, so the
delete of a stack saved with a cyclic template ranked such resources in
template order and could remove a target group before the listener using it.
When the sort stalls it now releases a resource on the cycle itself and goes on,
so everything that depends on the cycle keeps its dependency order.

* fix(cloudformation): delete linked dependency cycles in dependency order

When a saved stack's template has a cycle that depends on another cycle, the
sort could release a resource of the dependent cycle before the cycle it uses
was placed, so the delete removed that cycle's resources first. When the sort
stalls it now releases a resource from the first strongly connected component
Tarjan's algorithm completes over the resources left, which depends on no other
cycle.

* fix(cloudformation): order a saved cyclic stack in one pass and name only the cycle

Breaking one cycle at a time searched again at every stall and recursed, so a
long dependency chain could exhaust the thread's stack and stop the delete of a
stack saved with a cyclic template. The resources left at a stall are now
ordered in one pass by their strongly connected components, dependencies
first, with an iterative Tarjan search. The circular-dependency error names only
the components that are cycles, not resources that merely depend on one.

* test(cloudformation): cover circular dependency rejection with the AWS SDK

* test(cloudformation): assert only the cycle's resources in the SDK test's message
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-10-07 16:39:14 -04:00
1 parent 0f94d6213c
commit 13738f89de
6 files changed
+466 -32

No files matched your search

@@ -0,0 +1,80 @@
package com.floci.test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
import software.amazon.awssdk.services.cloudformation.model.CloudFormationException;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.QueueDoesNotExistException;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@DisplayName("CloudFormation CreateStack with a dependency cycle")
class CloudFormationCircularDependencyTest {
private static CloudFormationClient cfn;
private static SqsClient sqs;
private String stackName;
@BeforeAll
static void clients() {
cfn = TestFixtures.cloudFormationClient();
sqs = TestFixtures.sqsClient();
}
@BeforeEach
void setup() {
stackName = TestFixtures.uniqueName("compat-cfn-cycle");
}
@AfterEach
void cleanup() {
try {
cfn.deleteStack(r -> r.stackName(stackName));
} catch (CloudFormationException ignored) {
// The stack is never created; this only covers the rejection regressing.
}
}
@AfterAll
static void closeClients() {
cfn.close();
sqs.close();
}
@Test
@DisplayName("rejects the template, names the cycle and creates nothing")
void rejectsCycleBeforeCreatingResources() {
String template = """
{"Resources":{
"Downstream":{"Type":"AWS::SQS::Queue","DependsOn":"FirstQueue",
"Properties":{"QueueName":"%1$s-downstream"}},
"FirstQueue":{"Type":"AWS::SQS::Queue","DependsOn":"SecondQueue",
"Properties":{"QueueName":"%1$s-first"}},
"SecondQueue":{"Type":"AWS::SQS::Queue","DependsOn":"FirstQueue",
"Properties":{"QueueName":"%1$s-second"}}}}
""".formatted(stackName);
assertThatThrownBy(() -> cfn.createStack(r -> r.stackName(stackName).templateBody(template)))
.isInstanceOfSatisfying(CloudFormationException.class, e -> {
assertThat(e.awsErrorDetails().errorCode()).isEqualTo("ValidationError");
assertThat(e.awsErrorDetails().errorMessage())
.contains("Circular dependency between resources", "FirstQueue", "SecondQueue");
});
assertThatThrownBy(() -> cfn.describeStacks(r -> r.stackName(stackName)))
.isInstanceOf(CloudFormationException.class)
.hasMessageContaining("does not exist");
for (String suffix : List.of("downstream", "first", "second")) {
assertThatThrownBy(() -> sqs.getQueueUrl(r -> r.queueName(stackName + "-" + suffix)))
.isInstanceOf(QueueDoesNotExistException.class);
}
}
}