Last active
November 17, 2017 21:20
-
-
Save jspiewak/5f36690bb2c340f818ddcc7080d236ad to your computer and use it in GitHub Desktop.
Tag Volumes from Instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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