Skip to content

Instantly share code, notes, and snippets.

@jlintz
Created May 31, 2018 20:58
Show Gist options
  • Save jlintz/b7e82bb5290049d40cd66a690a548686 to your computer and use it in GitHub Desktop.
Save jlintz/b7e82bb5290049d40cd66a690a548686 to your computer and use it in GitHub Desktop.
Sync tags from EC2 instances to attached EBS volumes
#!/usr/bin/env python
"""
Sync tags from ec2 instances onto the volumes that are attached to them
"""
from boto import connect_ec2
def main():
conn = connect_ec2()
volumes = conn.get_all_volumes(filters={
'attachment.status': 'attached',
})
for vol in volumes:
instance_id = vol.attach_data.instance_id
instance_tags = conn.get_all_instances(
instance_ids=[instance_id])[0].instances[0].tags
# remove AWS specific tags since they are reserved for internal use
for tag, val in instance_tags.items():
if tag.startswith('aws:'):
instance_tags.pop(tag)
print 'Adding tags {0} to {1}'.format(instance_tags, vol.id)
try:
vol.add_tags(instance_tags)
except:
print 'Error adding tags to {0}'.format(vol.id)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment