How to Manage EBS Volumes Snapshots in AWS

Amazon Elastic Block Store (EBS) Volumes are highly available and reliable storage volumes that can be attached to any running EC2 instance that is in the same Availability Zone. When we attach the EBS volumes to EC2 instances they act as physical drive disks that we can format and use to install our operating system, our files and so on.

EBS Snapshot is one of the most useful tools offered by Amazon to make a copy of your volume. There are several reasons to do snapshots:

  • Point-in-time recovery of your volumes when you delete the data or volumes accidentally
  • Create an identical Volume in another Zone or Region
  • Create a Disaster Recovery environment in another Region
  • Remove the unused volumes for cost saving, but data needs to preserved for future use
  • Using the same kind of volumes for Dev and Staging environments
  • When AWS Zones or Regions are down

Amazon is providing various interfaces to create a snapshot of a Volume like Amazon AWS console, Command-line, APIs and SDKs. From AWS Console, you can create snapshots for any volume from either Volumes tab or from the Snapshots tab.

Unfortunately, AWS doesn’t have the scheduling feature with already set times like daily, hourly, weekly and etc.
You need to automate snapshots of volumes by yourself, using either command-line or APIs or SDKs. Here I am showing a script where you can automate the snapshot operations of volumes on regular intervals using AWS command-line tools.

A script to automate your Snapshots with EBS (Elastic Block Storage)

This script needs mainly 3 parameters:

  • Volumes-list: Get all the volumes list in a file called “volumes-list” line by line as VolumeID: VolumeName
  • Retention Period: How many latest snapshots you want to keep for a volume. Beyond that, you can purge the volumes.
  • AWS Keys/ IAM Role: Use IAM role of EC2 Instance or AWS Keys. It would be good, if you use the IAM role for your EC2 instances to avoid the specifying keys, if not use the AWS Keys with EBS privileges and update them in the script.

Let’s see how it works:

  • Creation of the snapshot for each Volume reading it from the volume-list file.

aws ec2 create-snapshot –volume-id $VOL_ID –description “$DESCRIPTION” –region $REGION &>> $SNAP_CREATION

  • Describe the list of completed snapshots for each Volume by excluding AMI snapshots.

aws ec2 describe-snapshots –query Snapshots[*].[SnapshotId,VolumeId,Description,StartTime] –output text –filters “Name=status,Values=completed” “Name=volume-id,Values=$VOL_ID” | grep -v “CreateImage” > $SNAPSHOT_INFO

  • Delete the snapshots of a Volume which exceeds the Retention period time.

aws ec2 delete-snapshot –snapshot-id $SNAP_ID –region $REGION –output text
Here is the complete script  (Evernote) and sample volume-list file for your reference.

When you take the snapshots of your volumes, it would be a good idea to ignore the Auto Scaling instances volumes, because those instances are stateless. Remember that auto-scaling machines will have an AMI to store their required config and data.

The sample script is to give you an idea of we can do the snapshots management, but you can always extend it to meet your needs.

Cloud Academy