-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_tagger.py
69 lines (57 loc) · 1.9 KB
/
resource_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import boto3
import json
def lambda_handler(event, context):
message = json.loads(event['Records'][0]['Sns']['Message'])
# print( 'Hello-json {}'.format(message))
# -------------------- Debug ---------------------------
# print( 'Hello {}'.format(event))
# print( 'User Name- {}'.format(message['detail']['userIdentity']['principalId']))
# print( 'Instance ID- {}'.format(message['detail']['responseElements']['instancesSet']['items'][0]['instanceId']))
# Variables
instanceId = message['detail']['responseElements']['instancesSet']['items'][0]['instanceId']
userNameSTring = message['detail']['userIdentity']['principalId']
region = message['region']
# Checks if the user is an okta user
if ":" in userNameSTring:
userName = userNameSTring.split(":")[1]
else:
userName = message['detail']['userIdentity']['userName']
print('Instance Id - ', instanceId)
print('User Name - ', userName)
tagKey = 'owner'
tagValue = userName
# ---------------------- Body ----------------------
# EC2 tagging
client = boto3.client('ec2', region_name=region)
response = client.create_tags(
Resources=[
instanceId
],
Tags=[
{
'Key': tagKey,
'Value': tagValue
},
{
'Key': 'auto:stop',
'Value': '0 1 * * SAT'
},
]
)
# Volume tagging
ec2 = boto3.resource('ec2', region_name=region)
instance = ec2.Instance(instanceId)
volumes = instance.volumes.all()
for volume in volumes:
volID = volume.id
print("volume - ", volID)
volume = ec2.Volume(volID)
tag = volume.create_tags(
Tags=[
{
'Key': tagKey,
'Value': tagValue
},
]
)
print(response)