fix(cloudformation): tolerate missing stack resources during deletion (#2039)

* fix(cloudformation): tolerate missing stack resources during deletion

* test(cloudformation): align rollback storage fixture

* fix(cloudformation): require precise missing-resource errors
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-08-23 17:29:53 -04:00
1 parent 726c9c7446
commit 12b8ddd629
3 files changed
+262 -7

No files matched your search

@@ -879,7 +879,7 @@ public class CloudFormationService {
* <p>On a <b>create</b>, rolls back by deleting resources created by the failed execution. On
* an <b>update</b>, restores the prior resource, template, output, and export state.
*/
private void rollbackFailedExecution(
void rollbackFailedExecution(
Stack stack,
String region,
boolean isCreate,
@@ -1267,10 +1267,14 @@ public class CloudFormationService {
resource.getResourceType(), "DELETE_IN_PROGRESS", null);
try {
deleteResourcePhysically(resource, region);
resource.setStatus("DELETE_COMPLETE");
addEvent(stack, resource.getLogicalId(), resource.getPhysicalId(),
resource.getResourceType(), "DELETE_COMPLETE", null);
completeResourceDeletion(stack, resource);
} catch (Exception e) {
if (isAlreadyDeleted(e)) {
completeResourceDeletion(stack, resource);
LOG.debugv("Resource {0} ({1}) was already deleted while rolling back stack {2}",
resource.getResourceType(), resource.getPhysicalId(), stack.getStackName());
continue;
}
failedResources.add(resource.getLogicalId());
resource.setStatus("DELETE_FAILED");
resource.setStatusReason(e.getMessage());
@@ -1369,10 +1373,14 @@ public class CloudFormationService {
resource.getResourceType(), "DELETE_IN_PROGRESS", null);
try {
deleteResourcePhysically(resource, region);
resource.setStatus("DELETE_COMPLETE");
addEvent(stack, resource.getLogicalId(), resource.getPhysicalId(),
resource.getResourceType(), "DELETE_COMPLETE", null);
completeResourceDeletion(stack, resource);
} catch (Exception e) {
if (isAlreadyDeleted(e)) {
completeResourceDeletion(stack, resource);
LOG.debugv("Resource {0} ({1}) was already deleted while deleting stack {2}",
resource.getResourceType(), resource.getPhysicalId(), stack.getStackName());
continue;
}
// AWS leaves the stack in DELETE_FAILED when a managed resource cannot be
// deleted (e.g. a non-empty S3 bucket raises BucketNotEmpty). The stack must
// not be reported as a successful deletion while the resource still exists.
@@ -1443,6 +1451,27 @@ public class CloudFormationService {
return true;
}
private void completeResourceDeletion(Stack stack, StackResource resource) {
resource.setStatus("DELETE_COMPLETE");
resource.setStatusReason(null);
addEvent(stack, resource.getLogicalId(), resource.getPhysicalId(),
resource.getResourceType(), "DELETE_COMPLETE", null);
}
/** Returns whether a resource deletion failed solely because the resource is already gone. */
private static boolean isAlreadyDeleted(Throwable failure) {
Set<Throwable> seen = Collections.newSetFromMap(new IdentityHashMap<>());
for (Throwable current = failure; current != null && seen.add(current); current = current.getCause()) {
if (current instanceof AwsException awsException
&& (awsException.getHttpStatus() == 404
|| (awsException.getErrorCode() != null
&& awsException.getErrorCode().endsWith("NotFoundException")))) {
return true;
}
}
return false;
}
private Map<String, Boolean> resolveConditions(JsonNode template, Map<String, String> params,
Stack stack, String region, String accountId) {
Map<String, Boolean> conditions = new HashMap<>();