A CLI tool to delete a bulk AWS snapshots and keep the expected snapshots based on snapshot age

When operating EC2, we might usually run into the situation of removing the old unused snapshots, and when you have e.g. hundreds of old snapshots, this becomes hard to delete manually. Or you probably want to find a way to automate this process when new snapshots become old snapshots after a while. In this post, I’m sharing a CLI script for deleting AWS snapshots in bulk that I’ve written in the Go language. The script deletes snapshots based on snapshot age and the expected number of snapshots we want to keep according to CLI’s supplied options.

Presequisites

  • Golang version 1.22.5 or 1.22+
  • Configure your AWS credentials (I prefer to run this tool on EC2 where it uses IAM role instead of configuring our own credentials). This tool assumes you have configured your AWS Credentials. If you haven’t, you either configure your environment:

In Linux:

export AWS_DEFAULT_REGION='your region'
export AWS_ACCESS_KEY_ID='with token Access ID'
export AWS_SECRET_ACCESS_KEY='with token AWS Secret'

or by AWS CLI – See https://docs.aws.amazon.com/cli/v1/userguide/cli-configure-files.html:

$ aws configure
AWS Access Key ID [None]: 'your access key ID'
AWS Secret Access Key [None]: 'your secret access key'
Default region name [None]: us-east-1
Default output format [None]: json

Setup

Download the ec2-delete-snapshots tool by git command to your local directory:

git clone https://gitlab.com/binhdt2611/ec2-delete-snapshots.git

Build a binary CLI:

cd ec2-delete-snapshots/
make build

The Makefile will automatically compile and build the source code into a binary CLI called at ./bin/ec2-delete-snapshots in the current ec2-delete-snapshot directory.

Usage

NOTE: We must provide a volume-id that snapshots associated to

Usage: ec2-delete-snapshots [options] <volume-id>

Options:
  -dryrun
        Checks whether you have the required permissions for the action,
        without actually making the request when deleting Snapshot
  -keep-first-monthly int
        Keep snapshots created on the start day of each of the past months
  -keep-first-weekly int
        Keep snapshots created on the start day of each recent week
  -keep-most-recent int
        Keep number of snapshots created recently
  -profile string
        Specify aws profile that you want to use for the creadential to access AWS. (default "default")
  -region string
        Specify aws profile that you want to use for creadential to access AWS. (default "us-east-1")

Positional Argument:
  volume-id   Must be provided without a flag

Example: To keep the most 3 recent snapshots, 3 snapshots of the first days of recent weeks, and 8 snapshots of the first days of recent months. These snapshots are associated with volume-id e.g. vol-0ekfa2k3kj1k1kl2l

./bin/ec2-delete-snapshots -keep-most-recent 3 -keep-first-weekly 3 -keep-first-monthly 8 vol-0ekfa2k3kj1k1kl2l

Note: If you don’t specify any -keep-* options, it will delete all snapshots of the supplied volume-id

Recommend: You can create a Bash script to collect all volume IDs and use this CLI to delete the old snapshots of the volume IDs you want to delete while keeping the expected version of snapshots.


Discover more from Turn DevOps Easier

Subscribe to get the latest posts sent to your email.

By Binh

Leave a Reply

Your email address will not be published. Required fields are marked *

Content on this page