This script automates the process of retrieving AWS instance details and deleting associated resources such as EC2 instances, Auto Scaling Groups (ASG), Elastic Kubernetes Service (EKS) clusters, Virtual Private Clouds (VPCs), subnets, security groups, and IAM instance profiles.
- Retrieve instance details using Instance ID, Public IP, or VPC ID.
- Identify associated Auto Scaling Groups, Security Groups, and IAM Instance Profiles.
- Dry Run Mode to preview deletions before executing them.
- Automated Cleanup of instances, subnets, internet gateways, and VPCs.
- EKS Cluster Cleanup before deleting the VPC.
Set your AWS credentials as environment variables:
export AWS_ACCESS_KEY_ID=your-access-key
export AWS_SECRET_ACCESS_KEY=your-secret-key
export AWS_SESSION_TOKEN=your-session-token # Optional, required for temporary credentialsEnsure you have Python 3 installed with the required libraries:
pip install boto3 argparseRun the script with different options to retrieve and clean up AWS resources.
python script.py --region us-east-1 --instance-id i-0123456789abcdefpython script.py --region us-east-1 --public-ip 34.123.45.67python script.py --region us-east-1 --vpc-id vpc-12345678To actually delete the resources, add the --no-dry-run flag:
python script.py --region us-east-1 --instance-id i-0123456789abcdef --no-dry-runpython script.py --region us-east-1 --vpc-id vpc-12345678 --no-dry-runThe script performs the following steps in order:
- Retrieve AWS Resources: Fetch instances, subnets, VPC, ASG, EKS clusters.
- Dry Run Confirmation: Display resources that will be deleted.
- EKS Cluster Deletion (if applicable).
- Auto Scaling Group Termination (if applicable).
- Detach IAM Profiles (if applicable).
- Terminate EC2 Instances.
- Delete VPC Components:
- Subnets
- Internet Gateways
- VPC Endpoints
- Delete VPC.
- If no matching resources are found, the script prints a message and exits.
- If resources are partially deleted, it will attempt retries.
- If
--no-dry-runis not specified, the script will only display resources without deleting them.
python script.py --region us-west-2 --instance-id i-0abcdef1234567890 --no-dry-run