-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
62 lines (50 loc) · 1.88 KB
/
lambda_function.py
File metadata and controls
62 lines (50 loc) · 1.88 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
import json
import boto3
REGION = 'us-east-1'
AWS_ACCESS_KEY = <your AWS access key>
AWS_SECRET = <your AWS secret>
AMI = 'ami-07d0cf3af28718ef8'
INSTANCE_TYPE = 't2.micro'
def lambda_handler(event, context):
print('received request: ' + str(event))
aws_service = event['currentIntent']['slots']['ServiceType']
instance_type = event['currentIntent']['slots']['InstanceType']
os = event['currentIntent']['slots']['OS']
key = event['currentIntent']['slots']['Key']
print ("aws_service = {} instance_type = {} os = {} key = {}".format(aws_service,
instance_type,
os,
key))
msg = ""
if aws_service.lower() != 'ec2':
msg = "Unsupported AWS service "
if os.lower() == 'ubuntu':
ami = 'ami-07d0cf3af28718ef8'
else:
msg += "Unsupported operating system "
if instance_type.lower() != "t2.micro":
msg += "Unsupported instance type "
if msg == "":
EC2 = boto3.client('ec2', aws_access_key_id=AWS_ACCESS_KEY, aws_secret_access_key=AWS_SECRET, region_name=REGION)
instance = EC2.run_instances(
ImageId=ami,
InstanceType=instance_type,
MinCount=1,
MaxCount=1,
InstanceInitiatedShutdownBehavior='terminate',
KeyName=key
)
msg = "New instance created {}".format(instance['Instances'][0]['InstanceId'])
print (msg)
response = {
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
"contentType": "SSML",
"content": msg
},
}
}
print('response = ' + str(response))
return response