Skip to content

Instantly share code, notes, and snippets.

@metalrufflez
Last active September 13, 2019 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metalrufflez/fae4a5dc36e08c7c8d0cc674588e1de1 to your computer and use it in GitHub Desktop.
Save metalrufflez/fae4a5dc36e08c7c8d0cc674588e1de1 to your computer and use it in GitHub Desktop.
#!/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