Ad-hoc cleaning up AWS VPC, associated resources

aws clean up

Imagine the situation where you did not use some fancy tools/services like CloudFormation, Terraform and the likes to create your AWS stacks and ended up with some Virtual Private Cloud (VPC) instances that you would need to clean up later on

That task is not as easy as it seems because you need to clean up all AWS resources contained in the VPCs before being able to delete the VPCs themselves. What if you have hundreds of resources provisioned and associated to the VPCs (EC2, ELBs, S3, etc) ?

I was in that situation and could not afford manually destroying those resources and then the VPCs. I decided to go through the AWS SDK and wrote a small utility component that does the job. I’ve wrapped the code inside a Docker container and pushed it to quay.io/dnguyenv/aws-vpc-cleanup:0.0.1. All I need now is this command:

$ docker run --rm -e AWS_SECRET_ACCESS_KEY="[YOUR AWS_SECRET_ACCESS_KEY]" -e AWS_ACCESS_KEY_ID="[YOUR AWS_ACCESS_KEY_ID]" -e AWS_REGION="[YOUR AWS_REGION]" quay.io/dnguyenv/aws-vpc-cleanup:0.0.1 /clean.sh "[YOUR AWS VPC ID]"

Output will look something like this:

Deleting EC2 instances .... 
Deleting ELBs non-classic instances .... 
Deleting ELBs classic instances .... 
Deleting EFS instances .... 
Deleting NAT Gateways instances .... 
Deleting IG instances .... 
Deleting subnets instances .... 
Deleting VPC endpoint instances .... 
...................
Finallay, deleting VPC instances .... 
VPC Deleted