Skip to content

Instantly share code, notes, and snippets.

@rashidcmb
Last active October 25, 2018 15:12
Show Gist options
  • Save rashidcmb/dce152c68aaf5c3943164721285f53e1 to your computer and use it in GitHub Desktop.
Save rashidcmb/dce152c68aaf5c3943164721285f53e1 to your computer and use it in GitHub Desktop.
This simple python script will update the Name tags of EBS volume with attached EC2 instance Name tag.
import boto3
ec2 = boto3.resource('ec2',region_name='us-east-1')
for volume in ec2.volumes.all():
vol = ec2.Volume(volume.id)
ebsId = volume.id
for v in vol.attachments:
ec2Id = v["InstanceId"]
ec2instance = ec2.Instance(ec2Id)
if ec2Id is not None:
for tags in ec2instance.tags:
if tags["Key"] == 'Name':
instancename = tags["Value"]
print(instancename)
volume.create_tags(Tags=[{'Key': 'Name','Value': instancename},])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment