Last active
May 30, 2019 15:13
-
-
Save diasjorge/153dba9ea5f9d87351dab29d9ef25746 to your computer and use it in GitHub Desktop.
Tag elastic ips with their instance name using boto 3
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
import boto3 | |
ec2 = boto3.client('ec2', region_name='us-east-2') | |
addresses = ec2.describe_addresses()['Addresses'] | |
for address in addresses: | |
if 'InstanceId' not in address: | |
continue | |
instance = ec2.describe_instances( | |
InstanceIds=[ | |
address['InstanceId'] | |
] | |
)['Reservations'][0]['Instances'][0] | |
tags = instance['Tags'] | |
name = tags | |
name = [tag['Value'] for tag in tags if tag['Key'] == 'Name'][0] | |
if name: | |
response = ec2.create_tags( | |
Resources=[ | |
address['AllocationId'], | |
], | |
Tags=[ | |
{ | |
'Key': 'Name', | |
'Value': name | |
} | |
] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment