Skip to content

Instantly share code, notes, and snippets.

@diasjorge
Last active May 30, 2019 15:13
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 diasjorge/153dba9ea5f9d87351dab29d9ef25746 to your computer and use it in GitHub Desktop.
Save diasjorge/153dba9ea5f9d87351dab29d9ef25746 to your computer and use it in GitHub Desktop.
Tag elastic ips with their instance name using boto 3
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