Skip to content

Instantly share code, notes, and snippets.

@dgulinobw
Last active February 22, 2019 23:47
Show Gist options
  • Select an option

  • Save dgulinobw/108f4abad7ea3965a133b72504e75878 to your computer and use it in GitHub Desktop.

Select an option

Save dgulinobw/108f4abad7ea3965a133b72504e75878 to your computer and use it in GitHub Desktop.
Tag ec2 resources based on associated instance's tags.
#!/usr/bin/env python
from __future__ import print_function
import sys
import os
import json
import boto3
from botocore.exceptions import ClientError
regions = ["us-east-1","us-east-2","us-west-1", "us-west-2"]
ownerids = ['$OWNER_ID']
required_tags = ["environment","product","role","cluster"]
product_substrings = [
'build',
'vpn']
def dict_to_kv(d):
kv_list = []
for key,value in d.items():
kv_list.append({'Key':key, 'Value':value})
return kv_list
def get_tag(name,tags):
tag = [tag['Value'] for tag in tags if tag['Key'] == name]
if tag:
tag = tag[0]
return tag
else:
return None
def get_tags(name_list,tags):
d = dict()
for name in name_list:
value = get_tag(name,tags)
if value:
d[name] = value
return dict_to_kv(d)
def tag_resource(ec2,resource_id,tags):
tag_kv_list = get_tags(required_tags,tags)
if tag_kv_list:
print(resource_id, tag_kv_list)
try:
ec2.create_tags(Resources=[resource_id], Tags=tag_kv_list)
except ClientError as ce:
print(ce)
## Instances tag environment by Name tag
def tag_instance_by_name_tag():
for region in regions:
ec2=boto3.client('ec2', region )
paginator = ec2.get_paginator('describe_instances')
pages = paginator.paginate()
for page in pages:
reservations = page['Reservations']
for reservation in reservations:
instances = reservation['Instances']
for instance in instances:
tags = instance['Tags']
name = [tag['Value'] for tag in tags if tag['Key'] == "Name"]
if name:
name = name[0]
print(instance['InstanceId'], name)
if name.count('-pro-') > 0:
ec2.create_tags(Resources=[instance['InstanceId']], Tags=[{'Key':'environment', 'Value':'pro'}])
if name.count('-qa-') > 0:
ec2.create_tags(Resources=[instance['InstanceId']], Tags=[{'Key':'environment', 'Value':'qa'}])
if any(substring in name for substring in product_substrings):
ec2.create_tags(Resources=[instance['InstanceId']], Tags=[{'Key':'product', 'Value':'other_product'}])
else:
ec2.create_tags(Resources=[instance['InstanceId']], Tags=[{'Key':'product', 'Value':'main_product'}])
# Volumes tag environment by Name tag
def tag_volume_by_name_tag():
for region in regions:
ec2=boto3.client('ec2', region )
paginator = ec2.get_paginator('describe_volumes')
pages = paginator.paginate()
for page in pages:
#print(page)
volumes = page['Volumes']
for volume in volumes:
#print(volume)
tags = volume.get('Tags')
if tags:
#print(tags)
name = [tag['Value'] for tag in tags if tag['Key'] == "Name"]
if name:
name = name[0]
print(volume['VolumeId'], name)
try:
if name.count('-pro-') > 0:
ec2.create_tags(Resources=[volume['VolumeId']], Tags=[{'Key':'environment', 'Value':'pro'}])
if name.count('-qa-') > 0:
ec2.create_tags(Resources=[volume['VolumeId']], Tags=[{'Key':'environment', 'Value':'qa'}])
except ClientError as ce:
print(ce)
# Volumes: tag by associated instance tags
def tag_volume_by_instance_name_tag():
for region in regions:
ec2=boto3.client('ec2', region )
ec2_resource = boto3.resource('ec2', region)
paginator = ec2.get_paginator('describe_volumes')
pages = paginator.paginate()
for page in pages:
volumes = page['Volumes']
for volume in volumes:
volume=ec2_resource.Volume(volume['VolumeId'])
try:
attachments = volume.attachments
if attachments:
instance_id = attachments[0]['InstanceId']
instance = ec2_resource.Instance(instance_id)
tags = instance.tags
tag_resource(ec2,instance_id,tags)
except ClientError as ce:
print(ce)
# Snapshots: tag by associated volume by associated instance tags
def tag_snapshot_by_instance_name_tag():
for region in regions:
ec2=boto3.client('ec2', region )
ec2_resource = boto3.resource('ec2', region)
paginator = ec2.get_paginator('describe_snapshots')
pages = paginator.paginate(OwnerIds=ownerids)
for page in pages:
snapshots = page['Snapshots']
for snapshot in snapshots:
snapshot=ec2_resource.Snapshot(snapshot['SnapshotId'])
try:
volume = snapshot.volume
instance_id = volume.attachments[0]['InstanceId']
instance = ec2_resource.Instance(instance_id)
tags = instance.tags
tag_resource(ec2,instance_id,tags)
except ClientError as ce:
print(ce)
# snapshot tag environment by snapshot Name tag
def tag_snapshot_by_snapshot_name_tag():
tags = snapshot.get('Tags')
if tags:
name = [tag['Value'] for tag in tags if tag['Key'] == "Name"]
if name:
name = name[0]
print(snapshot['SnapshotId'], name)
try:
if name.count('-pro-') > 0:
ec2.create_tags(Resources=[snapshot['SnapshotId']], Tags=[{'Key':'environment', 'Value':'pro'}])
if name.count('-qa-') > 0:
ec2.create_tags(Resources=[snapshot['SnapshotId']], Tags=[{'Key':'environment', 'Value':'qa'}])
except ClientError as ce:
print(ce)
# s3
## Instances tag environment by Name tag
def tag_s3_by_s3_name():
for region in ["us-east-1"]:
s3=boto3.client('s3', region )
s3_resource = boto3.resource('s3', region)
buckets = s3.list_buckets()['Buckets']
for bucket in buckets:
name = bucket['Name']
bucket = s3_resource.Bucket(bucket['Name'])
bucket_tagging = s3_resource.BucketTagging(bucket.name)
print(name)
try:
if name.count('_pro') > 0 or name.count('-pro') > 0:
bucket_tagging.put(Tagging = {'TagSet': [{'Key': 'environment', 'Value': 'pro'}]})
if name.count('_qa') > 0 or name.count('-qa') > 0:
bucket_tagging.put(Tagging = {'TagSet': [{'Key': 'environment', 'Value': 'qa'}]})
except ClientError as ce:
print(ce)
#ENI
def tag_eni_by_instance_tag_name():
for region in regions:
ec2=boto3.client('ec2', region )
ec2_resource = boto3.resource('ec2', region)
paginator = ec2.get_paginator('describe_network_interfaces')
pages = paginator.paginate()
for page in pages:
interfaces = page['NetworkInterfaces']
for interface in interfaces:
interface=ec2_resource.NetworkInterface(interface['NetworkInterfaceId'])
try:
attachment = interface.attachment
if attachment:
instance_id = attachment.get("InstanceId")
if instance_id:
instance = ec2_resource.Instance(instance_id)
tags = instance.tags
tag_resource(ec2,instance_id,tags)
except ClientError as ce:
print(ce)
def tag_elb_eni_by_tag_name():
#no api
pass
def tag_eip_by_instance_tag():
for region in regions:
ec2=boto3.client('ec2', region )
ec2_resource = boto3.resource('ec2', region)
eips = ec2.describe_addresses(Filters = [{'Name': 'domain', 'Values': ['vpc']}])['Addresses']
try:
for eip in eips:
instance_id = eip.get('InstanceId')
eip_id = eip.get('AllocationId')
if instance_id:
instance = ec2_resource.Instance(instance_id)
tags = instance.tags
tag_resource(ec2,instance_id,tags)
except ClientError as ce:
print(ce)
def main(argv, stdout, environ):
tag_s3_by_s3_name()
tag_instance_by_name_tag()
tag_volume_by_instance_name_tag()
tag_snapshot_by_instance_name_tag()
tag_eip_by_instance_tag()
tag_eni_by_instance_tag_name()
if __name__ == "__main__":
main(sys.argv, sys.stdout, os.environ)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment