-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvpc_tagger.py
51 lines (45 loc) · 2.01 KB
/
vpc_tagger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import boto3
from concurrent.futures import ThreadPoolExecutor as PoolExecutor
# Tags that are copyable to subnets, route tables, and IGWs
COPYABLE = ["Name", "Environment", "Project"]
def vpc(region):
print('Processing VPCs')
client = boto3.client('ec2',region_name=region)
ec2 = boto3.resource('ec2',region_name=region)
vpcs = client.describe_vpcs()
for vpc in vpcs['Vpcs']:
ID = vpc['VpcId']
try:
tags = [t for t in vpc['Tags'] or [] if t['Key'] in COPYABLE]
if not tags:
continue
except:
continue
# copy down VPC tags to internet gateways
igw_filter = [{'Name':'attachment.vpc-id', 'Values': [ID] }]
internet_gateways = client.describe_internet_gateways(Filters=igw_filter)
igws = internet_gateways['InternetGateways']
if igws:
for igw in igws:
ec2_igw = ec2.InternetGateway(igw['InternetGatewayId'])
ec2_igw.create_tags(Tags=tags)
# copy down VPC tags to subnets and route tables
subnet_filter = [{'Name':'vpc-id', 'Values': [ID] }]
subnets = client.describe_subnets(Filters=subnet_filter)
for subnet in subnets['Subnets']:
ec2_subnet = ec2.Subnet(subnet['SubnetId'])
ec2_subnet.create_tags(Tags=tags)
subnet_ID = subnet['SubnetId']
route_filter= [{'Name':'vpc-id', 'Values': [ID], 'Name': 'association.subnet-id', 'Values': [ subnet_ID ] }]
route = client.describe_route_tables(Filters=route_filter)
rts = route['RouteTables']
if rts:
for rt in rts:
ec2_routeTable = ec2.RouteTable(rt['RouteTableId'])
ec2_routeTable.create_tags(Tags=tags)
def lambda_handler(event, context):
client = boto3.client('ec2')
regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
for region in regions:
print('Starting Region = {}'.format(region))
vpc(region)