-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread-tag.py
More file actions
73 lines (55 loc) · 1.83 KB
/
read-tag.py
File metadata and controls
73 lines (55 loc) · 1.83 KB
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
70
71
72
73
import json
import boto3
def lambda_handler(event, context):
# 1- Working with AWS codecommit
# connect to codecommit
codecommit = boto3.client('codecommit')
# get data from tagged.json file from code commit
response = codecommit.get_file(repositoryName='tagged-instanes', filePath='instances.json')
file_content = response['fileContent'].decode()
json_content = json.loads(file_content)
# separate file content to two lists
instance_IDs_true = []
instance_IDs_false = []
for key, value in json_content.items():
if (value == "true"):
instance_IDs_true.append(key)
elif (value == "false"):
instance_IDs_false.append(key)
# 2- working with AWS EC2
#connect to ec2
ec2 = boto3.resource('ec2')
EC2_RESOURCE = boto3.resource('ec2')
# create tags template
TRUE_TAGS = [
{
'Key': 'Backup',
'Value': 'true'
}
]
FALSE_TAGS = [
{
'Key': 'Backup',
'Value': 'false'
}
]
# make two lists of EC2 instances
if(instance_IDs_true):
true_instances = EC2_RESOURCE.instances.filter(InstanceIds=instance_IDs_true)
else:
true_instances = []
if (instance_IDs_false):
flase_instance = EC2_RESOURCE.instances.filter(InstanceIds=instance_IDs_false)
else:
flase_instance = []
# update backup tag to true
for instance in true_instances:
instance.create_tags(Tags=TRUE_TAGS)
print(f'true Tags successfully added to the instance {instance.id}')
# update backup tag to false
for instance in flase_instance:
instance.create_tags(Tags=FALSE_TAGS)
print(f'false Tags successfully added to the instance {instance.id}')
return {
'statusCode': 200
}