Skip to content

Commit 274cc2b

Browse files
committed
Improved destroy.sh and added conditional failure to CF
1 parent b8a7523 commit 274cc2b

2 files changed

Lines changed: 66 additions & 41 deletions

File tree

helpers/vs-code-ec2.yaml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,18 @@ Resources:
183183
- export TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds:60")
184184
- export AWS_REGION=$(curl -H "X-aws-ec2-metadata-token:${TOKEN}" -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/\(.*\)[a-z]/\1/') && echo "export AWS_REGION=${AWS_REGION}" >> /home/ec2-user/.bashrc
185185
- export ALLOWED_IP="{{allowedIp}}"
186-
- "git clone -b feat/gitea https://github.com/aws-samples/eks-saas-gitops.git /home/ec2-user/environment/eks-saas-gitops"
186+
- "git clone https://github.com/aws-samples/eks-saas-gitops.git /home/ec2-user/environment/eks-saas-gitops"
187187
- "chown -R ec2-user:ec2-user /home/ec2-user/environment"
188188
- "sudo -u ec2-user nohup /usr/bin/code-server --port 8080 --host 0.0.0.0 > /dev/null 2>&1 &"
189-
- 'cd /home/ec2-user/environment/eks-saas-gitops/terraform && chmod +x install.sh && sudo -u ec2-user ./install.sh ${AWS_REGION} "{{allowedIp}}" > /home/ec2-user/environment/terraform-install.log 2>&1'
190189
- "export WAIT_HANDLE_URL=$(aws ssm get-parameter --name '/eks-saas-gitops/waitcondition-url' --query 'Parameter.Value' --output text --region $AWS_REGION)"
191-
- 'curl -X PUT -H ''Content-Type: application/json'' --data-binary ''{"Status" : "SUCCESS", "Reason" : "Environment Completed", "UniqueId" : "123456", "Data" : "Complete"}'' "$WAIT_HANDLE_URL"'
190+
- "cd /home/ec2-user/environment/eks-saas-gitops/terraform"
191+
- "chmod +x install.sh"
192+
- 'sudo -u ec2-user ./install.sh ${AWS_REGION} "{{allowedIp}}" > /home/ec2-user/environment/terraform-install.log 2>&1'
193+
- 'if [ $? -eq 0 ]; then'
194+
- ' curl -X PUT -H ''Content-Type: application/json'' --data-binary ''{"Status" : "SUCCESS", "Reason" : "Environment Completed", "UniqueId" : "123456", "Data" : "Complete"}'' "$WAIT_HANDLE_URL"'
195+
- 'else'
196+
- ' curl -X PUT -H ''Content-Type: application/json'' --data-binary ''{"Status" : "FAILURE", "Reason" : "Terraform installation failed", "UniqueId" : "123456", "Data" : "Failed"}'' "$WAIT_HANDLE_URL"'
197+
- 'fi'
192198

193199
SSMBootstrapAssociation:
194200
Type: AWS::SSM::Association

terraform/destroy.sh

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ cd "$TERRAFORM_DIR"
1515
# Set terraform variables
1616
export TF_VAR_aws_region="${AWS_REGION}"
1717

18-
# First, ensure the Gitea token is available in Terraform state
19-
echo "Retrieving Gitea token into Terraform state..."
20-
terraform apply --target data.aws_ssm_parameter.gitea_token --auto-approve
18+
# Backup and remove Gitea provider and resources
19+
echo "Removing Gitea provider and resources from Terraform configuration..."
20+
cp providers.tf providers.tf.backup
21+
cp main.tf main.tf.backup
22+
23+
# Remove Gitea provider from providers.tf
24+
sed -i '/^provider "gitea"/,/^}/d' providers.tf
25+
26+
# Remove Gitea resources from main.tf (gitea_repository and related data sources)
27+
sed -i '/^resource "gitea_repository"/,/^}/d' main.tf
28+
sed -i '/^data "aws_ssm_parameter" "gitea_token"/,/^}/d' main.tf
2129

2230
# Skip provider verification since Gitea server will be destroyed
2331
export TF_SKIP_PROVIDER_VERIFY=1
@@ -33,46 +41,57 @@ fi
3341

3442
# Clean up ECR repositories
3543
echo "Cleaning up ECR repositories..."
36-
for repo in $(aws ecr describe-repositories --region $AWS_REGION --query 'repositories[].repositoryName' --output text); do
37-
echo "Deleting images from $repo..."
38-
aws ecr batch-delete-image \
39-
--repository-name "$repo" \
40-
--image-ids "$(aws ecr list-images --repository-name "$repo" --query 'imageIds[*]' --output json)" \
41-
--region $AWS_REGION || true
44+
for repo in $(aws ecr describe-repositories --region $AWS_REGION --query 'repositories[].repositoryName' --output text 2>/dev/null || echo ""); do
45+
if [ -n "$repo" ]; then
46+
echo "Deleting images from $repo..."
47+
aws ecr batch-delete-image \
48+
--repository-name "$repo" \
49+
--image-ids "$(aws ecr list-images --repository-name "$repo" --query 'imageIds[*]' --output json)" \
50+
--region $AWS_REGION || true
51+
fi
4252
done
4353

44-
echo "Destroying resources in specific order..."
45-
46-
# First destroy EKS node groups with multiple attempts
47-
echo "Destroying EKS node groups..."
48-
for i in {1..3}; do
49-
echo "Attempt $i to destroy EKS node groups..."
50-
terraform destroy -target=module.eks.aws_eks_node_group.managed_ng -auto-approve && break || {
51-
echo "Node group destroy failed, waiting 60 seconds before retry..."
52-
sleep 60
53-
}
54+
# Clean up load balancers with eks-saas-gitops tag
55+
echo "Cleaning up load balancers with eks-saas-gitops tag..."
56+
# Clean up Application/Network Load Balancers (ELBv2)
57+
for lb_arn in $(aws elbv2 describe-load-balancers --region $AWS_REGION --query 'LoadBalancers[].LoadBalancerArn' --output text 2>/dev/null || echo ""); do
58+
if [ -n "$lb_arn" ] && aws elbv2 describe-tags --resource-arns "$lb_arn" --region $AWS_REGION --query 'TagDescriptions[0].Tags[?contains(Key, `eks-saas-gitops`) || contains(Value, `eks-saas-gitops`) || Key == `kubernetes.io/cluster/eks-saas-gitops`]' --output text | grep -q .; then
59+
echo "Deleting ALB/NLB: $lb_arn"
60+
aws elbv2 delete-load-balancer --load-balancer-arn "$lb_arn" --region $AWS_REGION || true
61+
fi
62+
done
63+
# Clean up Classic Load Balancers (ELB)
64+
for lb_name in $(aws elb describe-load-balancers --region $AWS_REGION --query 'LoadBalancerDescriptions[].LoadBalancerName' --output text 2>/dev/null || echo ""); do
65+
if [ -n "$lb_name" ] && aws elb describe-tags --load-balancer-names "$lb_name" --region $AWS_REGION --query 'TagDescriptions[0].Tags[?Key==`kubernetes.io/cluster/eks-saas-gitops`]' --output text | grep -q .; then
66+
echo "Deleting classic load balancer: $lb_name"
67+
aws elb delete-load-balancer --load-balancer-name "$lb_name" --region $AWS_REGION || true
68+
fi
5469
done
5570

56-
# Wait for node groups to be fully deleted
57-
echo "Waiting for node groups to be fully deleted..."
58-
sleep 60
59-
60-
# Then destroy EKS cluster
61-
echo "Destroying EKS cluster..."
62-
terraform destroy -target=module.eks -auto-approve || true
63-
64-
# Then destroy VPC
65-
echo "Destroying VPC and related resources..."
66-
terraform destroy -target=module.vpc -auto-approve || true
71+
# Clean up remaining ENIs in VPC
72+
echo "Cleaning up remaining ENIs..."
73+
for vpc_id in $(aws ec2 describe-vpcs --region $AWS_REGION --filters "Name=tag:Name,Values=eks-saas-gitops" --query 'Vpcs[].VpcId' --output text 2>/dev/null || echo ""); do
74+
if [ -n "$vpc_id" ]; then
75+
for eni_id in $(aws ec2 describe-network-interfaces --region $AWS_REGION --filters "Name=vpc-id,Values=$vpc_id" --query 'NetworkInterfaces[?Status==`available`].NetworkInterfaceId' --output text 2>/dev/null || echo ""); do
76+
if [ -n "$eni_id" ]; then
77+
echo "Deleting ENI: $eni_id"
78+
aws ec2 delete-network-interface --network-interface-id "$eni_id" --region $AWS_REGION || true
79+
fi
80+
done
81+
fi
82+
done
6783

68-
# Clean up IAM roles that might prevent reprovisioning
69-
echo "Destroying IAM roles..."
70-
terraform destroy -target=module.ebs_csi_irsa_role -auto-approve || true
71-
terraform destroy -target=module.image_automation_irsa_role -auto-approve || true
72-
terraform destroy -target=module.gitops_saas_infra -auto-approve || true
84+
# Wait for cleanup
85+
echo "Waiting for cleanup to complete..."
86+
sleep 30
7387

74-
# Finally, attempt to destroy everything else
75-
echo "Running final terraform destroy..."
88+
# Run single terraform destroy
89+
echo "Running terraform destroy..."
7690
terraform destroy -auto-approve
7791

78-
echo "Infrastructure destruction completed."
92+
# Restore original files
93+
echo "Restoring original Terraform files..."
94+
mv providers.tf.backup providers.tf
95+
mv main.tf.backup main.tf
96+
97+
echo "Infrastructure destruction completed."

0 commit comments

Comments
 (0)