-
Notifications
You must be signed in to change notification settings - Fork 2
/
tag_snitch.py
32 lines (27 loc) · 1.25 KB
/
tag_snitch.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
import boto3
from concurrent.futures import ThreadPoolExecutor as PoolExecutor
from botocore.vendored import requests
import os
ACTIONABLE = ["Project"]
REPORTABLE = ["Name"]
webhook=os.environ.get('WEBHOOK')
account=os.environ.get('ACCOUNT_NAME')
def tag_instance(instance):
tags = [t for t in instance.tags or [] if t['Key'] in ACTIONABLE]
if not tags:
rtags = [t for t in instance.tags or [] if t['Key'] in REPORTABLE]
if rtags:
x = rtags[0]
message="Instance {} in {} account does not have the Project tag, but does have Name {}".format(instance.id,account, x['Value'])
else:
message="Instance {} in {} account does not have the Project tag and no Name tag".format(instance.id,account)
body={ "channel": "#aws-missing-tags", "username": "Missing Tags", "attachments": [{ "color": "red", "fields": [{"title": "missing-tags","value": message }]}]}
requests.post(webhook, json=body)
def ec2():
print('Processing EC2 Instances')
instances = boto3.resource('ec2').instances.all()
with PoolExecutor(max_workers=4) as executor:
for _ in executor.map(tag_instance, instances):
pass
def lambda_handler(event, context):
ec2()