fix(cloudformation): fail stack delete when a managed resource cannot be deleted (#1554)

DeleteStack swallowed every resource-deletion failure and always reported
DELETE_COMPLETE. Deleting a stack that owned a non-empty AWS::S3::Bucket
therefore "succeeded" while the bucket and its objects were left behind,
which diverges from AWS (S3 refuses to delete a non-empty bucket, so the
stack must end in DELETE_FAILED).

- CloudFormationResourceProvisioner#delete no longer wraps resource deletion
  in a catch-all; failures propagate to the stack-level handler. The lenient
  *Safe helpers and the intentional KMS key no-op are preserved.
- CloudFormationService#deleteStackResources now records per-resource
  failures: the resource is marked DELETE_FAILED with a reason and event,
  remaining resources are still attempted, and the stack settles into
  DELETE_FAILED (retaining the stack and its exports) instead of being
  reported as deleted. Resources left in DELETE_FAILED are re-attempted on a
  subsequent delete, matching AWS retry behavior.

This corrects the same silent-success deviation for every managed resource
type, not just S3 buckets.

Adds a Java integration regression test and AWS CLI compatibility tests
covering both the non-empty (DELETE_FAILED) and empty (DELETE_COMPLETE)
bucket cases.

Fixes #1539
This commit is contained in:
Dixit R Jain authored and GitHub committed 2026-06-25 22:47:19 -05:00
1 parent 56f846988f
commit 0530ca11ab
4 files changed
+292 -59

No files matched your search

@@ -1138,6 +1138,95 @@ class CloudFormationIntegrationTest {
.body(containsString("does not exist"));
}
// Regression: issue #1539. Deleting a stack that owns a NON-EMPTY S3 bucket must leave the
// stack in DELETE_FAILED (S3 refuses to delete a non-empty bucket) and keep the bucket — it
// must not silently report DELETE_COMPLETE while the bucket and its objects still exist.
@Test
void deleteStack_withNonEmptyS3Bucket_failsAndKeepsBucket() throws Exception {
String bucket = "cfn-nonempty-delete-test-bucket";
String template = """
{
"Resources": {
"MyBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": "%s"
}
}
}
}
""".formatted(bucket);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStack")
.formParam("StackName", "cfn-nonempty-delete-stack")
.formParam("TemplateBody", template)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<StackId>"));
// The managed bucket exists.
given()
.header("Host", bucket + ".localhost")
.when()
.get("/")
.then()
.statusCode(200);
// Put an object so the bucket is non-empty.
given()
.contentType("text/plain")
.body("hello floci 1539")
.when()
.put("/" + bucket + "/object.txt")
.then()
.statusCode(200);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DeleteStack")
.formParam("StackName", "cfn-nonempty-delete-stack")
.when()
.post("/")
.then()
.statusCode(200);
// The stack must settle into DELETE_FAILED — never DELETE_COMPLETE.
String statusXml = null;
long deadline = System.currentTimeMillis() + 10_000;
while (System.currentTimeMillis() < deadline) {
statusXml = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStacks")
.formParam("StackName", "cfn-nonempty-delete-stack")
.when()
.post("/")
.then()
.statusCode(200)
.extract().asString();
if (statusXml.contains("<StackStatus>DELETE_FAILED</StackStatus>")
|| statusXml.contains("<StackStatus>DELETE_COMPLETE</StackStatus>")) {
break;
}
Thread.sleep(200);
}
assertThat(statusXml, containsString("<StackStatus>DELETE_FAILED</StackStatus>"));
assertThat(statusXml, not(containsString("<StackStatus>DELETE_COMPLETE</StackStatus>")));
// The managed bucket must still exist after the failed delete.
given()
.header("Host", bucket + ".localhost")
.when()
.get("/")
.then()
.statusCode(200);
}
@Test
void describeDeletedStack_byArn_expiresAfterRetentionWindow() throws Exception {
String template = """