Skip to content

Instantly share code, notes, and snippets.

@dgulinobw
Last active November 12, 2021 18:09
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 dgulinobw/b32cc4dfb07781574b4a08e38afb14e4 to your computer and use it in GitHub Desktop.
Save dgulinobw/b32cc4dfb07781574b4a08e38afb14e4 to your computer and use it in GitHub Desktop.
EC2: set tags on resources to match the tags of the instances they are attached to.
#!/usr/bin/env python
import boto3
import collections
import datetime
import time
ec = boto3.client('ec2',region_name="us-east-1")
reservations = ec.describe_instances().get('Reservations',[])
instances = [
i for r in reservations
for i in r['Instances']
]
print("Total instances: ", len(instances))
instances = { i.get("InstanceId") : i for i in instances }
volumes = ec.describe_volumes().get('Volumes',[])
print("Total volumes: ", len(volumes))
volumes = { i.get("VolumeId") : i for i in volumes }
def tag_eips():
eips = ec.describe_addresses().get('Addresses',[])
print("Total eips: ", len(eips))
count = 0
for eip in eips:
if eip.get('Domain') == 'standard':
continue
instance_id = eip.get('InstanceId',None)
if instance_id == None:
continue
instance = instances.get(instance_id,None)
if instance == None:
continue
print(eip)
print(eip['AllocationId'])
cluster, role, environment, product, name = get_instance_tags(instance)
ec.create_tags(
Resources=[eip['AllocationId']],
Tags=[
{'Key': 'Name', 'Value': name},
{'Key': 'cluster', 'Value': cluster },
{'Key': 'role', 'Value': role },
{'Key': 'environment', 'Value': environment },
{'Key': 'product', 'Value': product }
])
count += 1
print("Total eips tagged: ", count)
def tag_volumes():
local_volumes = ec.describe_volumes().get('Volumes',[])
print(local_volumes[0])
#print("Total volumes: ", len(volumes))
count = 0
for vol in local_volumes:
volume_id = vol.get('VolumeId')
volume = volumes.get(volume_id,None)
if volume == None:
continue
attachments = volume.get('Attachments',{})
if attachments:
instance_id = attachments[0].get('InstanceId',None)
device_name = attachments[0].get('Device',None)
else:
continue
instance = instances.get(instance_id,None)
if instance == None:
continue
print(vol['VolumeId'])
cluster, role, environment, product, name = get_instance_tags(instance)
ec.create_tags(
Resources=[vol['VolumeId']],
Tags=[
{'Key': 'Name', 'Value': name},
{'Key': 'instance_id', 'Value': instance_id },
{'Key': 'cluster', 'Value': cluster },
{'Key': 'role', 'Value': role },
{'Key': 'environment', 'Value': environment },
{'Key': 'product', 'Value': product }
])
count += 1
print("Total volumes tagged: ", count)
def tag_enis():
enis = ec.describe_network_interfaces().get('NetworkInterfaces',[])
print("Total enis: ", len(enis))
count = 0
for eni in enis:
instance_id = eni.get('Attachment',{}).get('InstanceId',None)
if instance_id == None:
continue
instance = instances.get(instance_id,None)
if instance == None:
continue
print(eni['NetworkInterfaceId'])
cluster, role, environment, product, name = get_instance_tags(instance)
ec.create_tags(
Resources=[eni['NetworkInterfaceId']],
Tags=[
{'Key': 'Name', 'Value': name},
{'Key': 'cluster', 'Value': cluster },
{'Key': 'role', 'Value': role },
{'Key': 'environment', 'Value': environment },
{'Key': 'environment', 'Value': environment },
{'Key': 'product', 'Value': product }
])
count += 1
print("Total enis tagged: ", count)
def tag_snapshots():
snapshots = ec.describe_snapshots().get('Snapshots',[])
print("Total snapshots: ", len(snapshots))
count = 0
for snap in snapshots:
volume_id = snap.get('VolumeId')
volume = volumes.get(volume_id,None)
if volume == None:
continue
instance_id = volume.get('Attachments',{})[0].get('InstanceId',None)
device_name = volume.get('Attachments',{})[0].get('Device',None)
if instance_id == None:
continue
instance = instances.get(instance_id,None)
if instance == None:
continue
print(snap['SnapshotId'])
cluster, role, environment, product, name = get_instance_tags(instance)
ec.create_tags(
Resources=[snap['SnapshotId']],
Tags=[
{'Key': 'Name', 'Value': name},
{'Key': 'volume', 'Value': volume_id},
{'Key': 'device', 'Value': device_name},
{'Key': 'instance_id', 'Value': instance_id },
{'Key': 'lambda_EbsSnapshotBackups', 'Value': 'true' },
{'Key': 'cluster', 'Value': cluster },
{'Key': 'role', 'Value': role },
{'Key': 'environment', 'Value': environment },
{'Key': 'product', 'Value': product }
])
count += 1
print("Total snaphots tagged: ", count)
def get_instance_tags(instance):
try:
cluster = [
t.get('Value') for t in instance['Tags']
if t.get('Key') == 'cluster'][0]
except:
cluster = ''
try:
role = [
t.get('Value') for t in instance['Tags']
if t.get('Key') == 'role'][0]
except:
role = ''
try:
environment = [
t.get('Value') for t in instance['Tags']
if t.get('Key') == 'environment'][0]
except:
environment = 'pro'
try:
product = [
t.get('Value') for t in instance['Tags']
if t.get('Key') == 'product'][0]
except:
product = 'republicwireless'
name = instance['InstanceId']
for tag in instance.get('Tags') or []:
if tag.get('Key') == 'Name':
name = tag['Value']
return cluster, role, environment, product, name
def main():
tag_eips()
tag_volumes()
tag_snapshots()
tag_enis()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment