fix(cloudformation): tolerate an already-gone EC2 instance or security group on stack delete (#3842)

Ec2InstanceCfnProvisioner.delete and Ec2SecurityGroupCfnProvisioner.delete called terminateInstances / deleteSecurityGroup directly, so a group or instance removed out of band turned DeleteStack into DELETE_FAILED. Wrap both in CfnDeletes.safeDelete with the specific not-found code (InvalidInstanceID.NotFound, InvalidGroup.NotFound), per AGENTS.md step 5, so already-gone is tolerated while a real error such as DependencyViolation still propagates.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-18 07:41:10 -05:00
1 parent 938eca1fe9
commit 6e034837ad
4 files changed
+52 -2

No files matched your search

@@ -137,6 +137,9 @@ public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner {
@Override
public void delete(String resourceType, String physicalId, String region) {
ec2Service.terminateInstances(region, List.of(physicalId));
// Tolerate an instance already terminated out of band, so DeleteStack does not fail on it.
CfnDeletes.safeDelete("EC2 instance", physicalId,
() -> ec2Service.terminateInstances(region, List.of(physicalId)),
"InvalidInstanceID.NotFound");
}
}
@@ -106,7 +106,10 @@ public class Ec2SecurityGroupCfnProvisioner implements CfnResourceProvisioner {
@Override
public void delete(String resourceType, String physicalId, String region) {
ec2Service.deleteSecurityGroup(region, physicalId);
// Tolerate a group already deleted out of band, so DeleteStack does not fail on it.
CfnDeletes.safeDelete("security group", physicalId,
() -> ec2Service.deleteSecurityGroup(region, physicalId),
"InvalidGroup.NotFound");
}
/** The VPC a security group would land in for this template value: the default when omitted. */