Automate Your AWS Infrastructure with Boto 3 — AWS Snapshots

Part 3 — How to write a Python script to automate creation of AWS snapshots

Madison Schott
Sep 16, 2020 · 3 min read
orange, blue, pink, green, and brown gears on a black background
orange, blue, pink, green, and brown gears on a black background

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(
Filters=[
{
'Name': 'status',
'Values': [
'available'
]
}
]
}

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(
VolumeId=x
)

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.

Capital One Tech

The low down on our high tech from the engineering experts…

Madison Schott

Written by

Just a young female millennial navigating the tech world at Capital One as a software engineer with a non-traditional tech background. @madisonmaeorg on Twitter

Capital One Tech

The low down on our high tech from the engineering experts at Capital One. Learn about the solutions, ideas and stories driving our tech transformation.

Madison Schott

Written by

Just a young female millennial navigating the tech world at Capital One as a software engineer with a non-traditional tech background. @madisonmaeorg on Twitter

Capital One Tech

The low down on our high tech from the engineering experts at Capital One. Learn about the solutions, ideas and stories driving our tech transformation.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store