Using Python with boto3 to clean up your EC2 instances!
For this project I built a “set” of Python scripts that allow a user to create an number of EC2 instances with tags, then delete all instances with a certain tag (and excluding my Cloud9 instance). It’s a great little “end of the day” script to clean up extra instances and keep costs down.
Before we destroy, we must create!
Seeing as this project, and the development thereof, requires instances to terminate, let’s make a quick script to spin up n EC2 instances:
As you can see, boto3 really does all the hard lifting for us. Simply import it, then create a client with the AWS service you need, and you’re off to the races!
Now for the fun part!
we all know this bad boy is the reason you’re here ;)
You may remember my comment from earlier regarding protecting my Cloud9 instance? Well, it turns out when you write a script to terminate instances… it’s super easy to nuke your own working environment! I’m not ashamed to admit this happened more than once! Finally, however, I got my logic right, and my Cloud9 environment was safe at last!
Conclude the shenanigans!
This one was pretty short and straightforward, so thank you for reading, and please feel free to check out this, and my other Python adventures on my GitHub!