Skip to content

Instantly share code, notes, and snippets.

@jspiewak
Created November 17, 2017 20:31
Show Gist options
  • Save jspiewak/d6bc6c4a7e442cc83bd49e58810cc6ab to your computer and use it in GitHub Desktop.
Save jspiewak/d6bc6c4a7e442cc83bd49e58810cc6ab to your computer and use it in GitHub Desktop.
Tag Snapshots from Volume
#!/bin/bash
snapids=( $(aws ec2 describe-snapshots --owner-id self --filter "Name=status,Values=completed" --output text --query 'Snapshots[?!not_null(Tags[?Key == `rs:project`].Value)] | [].[SnapshotId]') )
for snapid in ${snapids[@]}; do
echo "Getting volume for snapshot ${snapid}"
volid=$(aws ec2 describe-snapshots --output text --snapshot-ids ${snapid} --query 'Snapshots[].VolumeId')
echo "Getting rs:* tags for volume ${volid}"
tags=$(aws ec2 describe-volumes --volume-ids ${volid} --query 'Volumes[].Tags[?starts_with(Key, `rs:`)][]' 2>/dev/null)
if [ "${tags}" != "" ]; then
aws ec2 create-tags $DRYRUN --resources ${snapid} --tags ''"${tags}"''
else
echo "Volume ${volid} no longer exists or does not have rs:* tags, unable to tag snapshot ${snapid}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment