Automate Your AWS Infrastructure with Boto 3 — AWS Snapshots
Part 3 — How to write a Python script to automate creation of AWS snapshots
When I first joined a DevOps/SRE team, I realized there were a lot of simple AWS infrastructure changes that took up a large chunk of our engineering team’s time. I didn’t want to spend my valuable coding time on these manual, yet essential, tasks so I set out on a mission to automate them. Since I had wanted to build my Python scripting skills anyway, I discovered a way to solve two problems at once — using the software development kit Boto 3 to automate my simple, manual AWS tasks using Python.
For the third installment in this series I wanted to teach you how to write a Python script that can be used to automate the creation of snapshots in the AWS environment. If you came from Part 1 or Part 2 then you already know how to automate instance tags and health checks.
First, let’s go over why you may want to automate creating snapshots. Snapshots are important for data backup and creating new EBS volumes. You may want to automate this to ensure your data is always available in case you need it. Let’s say you have an entire environment worth of EBS volumes that have not been backed up. This would be a great time to run a script that automatically creates snapshots of each volume.
Just like in the other articles, the first thing you must do is define the client. Since we know our goal is to create a snapshot of an EBS volume, we must look for this functionality in the Boto 3 documentation. I was able to find a “create_snapshot” method under EC2, which means we need to define an EC2 client.
ec2_client = boto3.client('ec2')
Now in order to create a snapshot we need to first find the ids of the volumes we are creating snapshots of. We can do this using the “describe_volumes” method.
response = ec2_client.describe_volumes()
If you look at this method you can see there is a parameter to filter by various conditions. This is useful if you want to create snapshots only for volumes with a certain tag, status, creation time, etc.
If we wanted to only create snapshots for volumes that are currently available, we would do so by filtering like this.
response = ec2_client.describe_volumes(
Once we do that we can see the response will return all volumes with a status of “available”. Then from here we can parse through the response to find what we need to create the snapshots- volumeId.
You want to make sure you save these volumeIds as a list. We are going to have to use a for statement to loop through each id in the list, creating its snapshot.
for x in list:
snapshots = ec2_client.create_snapshot(
And there you have it! This is an easy way to automate creating snapshots for your EBS volumes. You can easily save this script and change the filters you use to find the instanceIds in order to utilize it for different use cases within your AWS environment.
Originally published at https://www.capitalone.com.
DISCLOSURE STATEMENT: © 2020 Capital One. Opinions are those of the individual author. Unless noted otherwise in this post, Capital One is not affiliated with, nor endorsed by, any of the companies mentioned. All trademarks and other intellectual property used or displayed are property of their respective owners.