AWS - Script to create EC2 snapshots and automatically rename them

Solution 1:

I'm not positive I understand your question completely, but if what you want is to be able to generate a list of your volumes, along with name tags, something like this might work:

aws ec2 describe-tags --query "Tags[*].{Name:Value,ResourceId:ResourceId}" --filters "Name=key,Values=Name"  --filters "Name=resource-type,Values=volume" --output json

Basically what that says is "Give me the resource id and the value of the tag "name" for every resource of type "Volume". In this case, I specified json as the output. You may also specify "text" or "table" depending on your needs.

Another approach:

aws ec2 describe-volumes --query "Volumes[*].{ID:VolumeId}" --output text

This would return a list of your volumes. If you piped this to a text file, the file would just contain a list of volume identifiers - one per line.

You could then get the Name tag for each volume in the list with something like this:

aws ec2 describe-tags --query "Tags[*].{Name:Value,ResourceId:ResourceId}" --filters "Name=key,Values=Name"  --filters "Name=resource-type,Values=volume" --filters "Name=resource-id,Values=vol-2e293949" --output json

This is basically saying "give me the resource id and the value of the tag name for the specified id vol-2e293949.

As you can see the CLI commands can be hard to read and the filtering and querying is a bit difficult. (These examples use a recent version of the AWS CLI)

Solution 2:

As for extracting the snapshot ID from your create-snapshot command, you can do it without needing awk/sed/grep/etc. by using a few CLI features (query and output).

For example:

$ SNAP_ID=`aws ec2 create-snapshot --cli-input-json file://$temp_file --query 'SnapshotId' --output text`
$
$ echo "snap id: "$SNAP_ID
snap id: snap-aaaabbbb
$
$ aws ec2 create-tags --resources $SNAP_ID --tags Key=Name,Value=$SOME_NAME
$

Note that with an output of json instead of "text" it wraps the returned snapshot id with double quotes per JSON standard.

Solution 3:

A while back I wrote this bash script to do exactly this for me. Basically, you install it on each EC2 instance that you want to back up, and providing you give it a private key, a certificate file and the EC2 API tools, this will self-discover the instance ID, the volumes attached to it, and create a snapshot for each volume.

You must tag the volumes with Name and Device. Name is just a user-friendly name that appears in the snapshot's description. Device is is the actual device name on the server. /dev/sdf for instance. Finally, it will rotate your snapshots once a predefined limit has been reached. The default is 50. When the 51st snapshot is created, the oldest one will be deleted.