Skip to content

Instantly share code, notes, and snippets.

@jspiewak
Last active November 17, 2017 21:20
Show Gist options
  • Save jspiewak/5f36690bb2c340f818ddcc7080d236ad to your computer and use it in GitHub Desktop.
Save jspiewak/5f36690bb2c340f818ddcc7080d236ad to your computer and use it in GitHub Desktop.
Tag Volumes from Instance
#!/bin/bash
volids=( $(aws ec2 describe-volumes --output text --filters "Name=attachment.status,Values=attached" --query 'Volumes[?!not_null(Tags[?Key == `rs:project`].Value)] | [].[VolumeId]') )
for volid in ${volids[@]}; do
echo "Getting instance for volume ${volid}"
instanceid=$(aws ec2 describe-volumes --output text --volume-ids ${volid} --query 'Volumes[].Attachments[].InstanceId')
echo "Getting rs:* tags for instance ${instanceid}"
tags=$(aws ec2 describe-instances --instance-ids ${instanceid} --query 'Reservations[].Instances[].Tags[?starts_with(Key, `rs:`)][]')
if [ "${tags}" != "" ]; then
aws ec2 create-tags $DRYRUN --resources ${volid} --tags ''"${tags}"''
else
echo "Instance ${instanceid} does not have rs:* tags to copy to volume ${volid}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment