Last active
September 13, 2019 19:25
-
-
Save metalrufflez/fae4a5dc36e08c7c8d0cc674588e1de1 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# WARNING: Will if instance contains ';' in either tag key or value | |
# Will fix it later, maybe | |
id=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) | |
az=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone/) | |
region=${az%[a-z]} | |
IFS=$'\n' | |
volumes=($(aws --region $region ec2 describe-volumes --filters --filters Name=attachment.instance-id,Values=$id | grep VOLUMES | grep -o "vol-[a-z0-9]*")) | |
instance_tags=($(aws --region $region ec2 describe-tags --filters "Name=resource-id,Values=$id" --query 'Tags[*].[Key,Value]' --output text | tr '\t' ';')) | |
for vol in ${volumes[@]}; do | |
for tag in ${instance_tags[@]}; do | |
aws --region $region ec2 create-tags --resources $vol --tags Key=\"${tag%;*}\",Value=\"${tag#*;}\" | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment