-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
83 lines (72 loc) · 2.3 KB
/
lambda_function.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import json
import boto3
images = []
def lambda_handler(event, context):
instances = []
ec2 = boto3.resource('ec2')
for instance in ec2.instances.all():
if(instance.tags is not None) and (instance.tags[0]['Key'] == 'GoldAMI'):
instances.append(instance.id)
print (instance.id , instance.tags)
client = boto3.client('ec2')
for x in instances:
images.append(client.create_image(InstanceId = x, Name = 'tempImage'))
client = boto3.client('lambda')
client = boto3.client('ec2')
response = client.describe_images(
Filters=[
{
'Name': 'name',
'Values' : [
'Windows_Server-2016-English-Full-Base-2019.08.16'
]
}
]
)
imageId = response['Images'][0]['ImageId']
resource = boto3.resource('ec2')
instance = resource.create_instances(
ImageId = imageId,
InstanceType = 't2.micro',
MinCount = 1,
MaxCount = 1
)
#Powershell
ec2 = boto3.resource('ec2')
inspector = boto3.client('inspector')
instance = instance[0]
response = inspector.create_assessment_target(
assessmentTargetName = 'securityCheck',
resourceGroupArn = instance[0].iam_instance_profile['Arn']
)
#Run Inspector
ec2 = boto3.resource('ec2')
for instance in ec2.instances.all():
if(instance.tags is not None) and (instance.tags[0]['Key'] == 'temp'):
instances = instance.id
client = boto3.client('ec2')
image = client.create_image(InstanceId = instances, Name = 'upImage')
instance = ec2.Instance(instances)
instance.terminate()
client = boto3.client('lambda')
client = boto3.client('ses')
email = "A new AMI is made with the new Windows update. The ami id is __________"
response = client.send_email(
Destination = {
'ToAddresses': [
'string',
]
},
Message={
'Subject': {
'Charset': 'UTF-8',
'Data': 'Windows AMI Update'
},
'Body':{
'Text': {
'Charset': 'UTF-8',
'Data': email
}
}
}
)