How to use Boto3 for AWS automated snapshots in 3 steps

Updated February 2, 2024

Part 3 - How to write a Python script to automate 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 Boto3 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 have already read part 1 - AWS instance tags or part 2 - AWS Health Checks then you already know how to automate instance tags and health checks.

Yes, We’re Open Source!

Learn more about how we make open source work in our highly regulated industry.

Learn More

How to automate AWS snapshot creation in three easy steps

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.

Step one: Define the client

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 Boto3 documentation. I was able to find a create_snapshot method under EC2, which means we need to define an EC2 client.

Define client code example:ec2_client = boto.3client(‘ec2’)

Step two: Identify the volume ID

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.

Describe volume code example: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.

Filtering tip

If we wanted only to create snapshots for volumes that are currently available, we would do so by filtering like this.

Filter code example: response = ec2_client.describe_volumes(Filters-[{ ‘Name’:’status’,’Values’: [‘avaialble’]}]}

Step three: Parse the response

Once we do that we can see the response will return all volumes with a status of “available.” From here we can parse through the response to find what we need to create the snapshots- volumeId. (Example: XXXXX parsing through response).

Save as a list:

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.

Create Snapshot code example: for x in list: snapshots = ec2_client.create_snapshot(VolumeId=x)

Creating AWS snapshots for automation with Boto3 and Python

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.

With Boto3 and Python, you've armed yourself with the tools to thrive in the world of AWS. So, go forth and automate, simplify and transform your AWS experience. Your coding time is precious, and with Boto 3, you'll use it wisely while conquering the complexities of AWS resource management.


Madison Schott, Associate Data Engineer, LT Data Transformation

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

Related Content