Skip to content

Commit 87df570

Browse files
Delete orphaned subnets in eksctl_delete_cluster_cf_stack (#749)
*Issue #, if available:* Cluster deletion fails with `DependencyViolation` when CloudFormation tries to delete the VPC. This happens when a previous stack creation partially failed and rolled back, leaving orphaned subnets behind that CloudFormation no longer tracks but that still block VPC deletion. *Description of changes:* In `eksctl_delete_cluster_cf_stack`, before initiating the `CloudFormation` stack deletion, we now query and delete any subnets tagged with the stack name. These are subnets that were created by CloudFormation during a failed/rolled-back stack creation and were not cleaned up, causing subsequent VPC deletion to fail. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. Signed-off-by: Renan Magagnin <renanmag@amazon.co.uk>
1 parent 0c91582 commit 87df570

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

tests/e2e-kubernetes/scripts/eksctl.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,21 @@ function eksctl_delete_cluster_cf_stack() {
209209
return 0
210210
fi
211211

212+
# Delete any subnets orphaned by a CloudFormation SDK retry during stack creation.
213+
# CreateSubnet is not idempotent: if the first API call succeeds but the response times out
214+
# (a transient issue), the SDK retries and gets a CIDR conflict error. CFN sees only the
215+
# failure, records no PhysicalResourceId, and cannot clean up the subnet during rollback.
216+
# These orphaned subnets are still tagged with the stack name but are unknown to CFN,
217+
# and will block VPC deletion.
218+
SUBNETS=$(aws ec2 describe-subnets --region ${REGION} \
219+
--filters "Name=tag:aws:cloudformation:stack-name,Values=${STACK_NAME}" \
220+
--query 'Subnets[*].SubnetId' --output text)
221+
if [ -n "$SUBNETS" ]; then
222+
for SUBNET_ID in $SUBNETS; do
223+
aws ec2 delete-subnet --region ${REGION} --subnet-id ${SUBNET_ID} || true
224+
done
225+
fi
226+
212227
aws cloudformation delete-stack --region ${REGION} --stack-name ${STACK_NAME}
213228

214229
# GuardDury creates resources (namely an endpoint and a security group), which are not handled by eks cfn stack and prevents it from being deleted

0 commit comments

Comments
 (0)