Skip to content

Instantly share code, notes, and snippets.

@ttor
Created September 9, 2019 11:48
Show Gist options
  • Save ttor/b44d671ba4dc770b5d241473a0df39cb to your computer and use it in GitHub Desktop.
Save ttor/b44d671ba4dc770b5d241473a0df39cb to your computer and use it in GitHub Desktop.
Copy tags from EC2 instance to attached EBS volume
#adapted from https://gist.github.com/mlapida/931c03cce1e9e43f147b
import boto3
tags_to_use = ['Owner']
def copy_tags():
instances = boto3.resource('ec2').instances.all()
for instance in instances:
tags = instance.tags
to_tag = [t for t in tags if t['Key'] in tags_to_use]
for vol in instance.volumes.all():
print(vol.id, instance.id,to_tag)
if to_tag:
vol.create_tags(Tags=to_tag)
copy_tags()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment