-
Notifications
You must be signed in to change notification settings - Fork 0
/
autottdTodoHandlerTemplate.py
62 lines (53 loc) · 1.68 KB
/
autottdTodoHandlerTemplate.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
import os
import boto3
import urllib.parse
AMI = os.environ["AMI"]
INSTANCE_TYPE = os.environ["INSTANCE_TYPE"]
KEY_NAME = os.environ["KEY_NAME"]
SECURITY_GROUP_ID = os.environ["SECURITY_GROUP_ID"]
IAM_INSTANCE_PROFILE_ARN = os.environ["IAM_INSTANCE_PROFILE_ARN"]
ec2 = boto3.resource("ec2")
s3 = boto3.client("s3")
logonScript = """<powershell>
REPLACE_LOGON
</powershell>
"""
def lambda_handler(event, context):
# Get the object from the event and show its content type
bucket = event["Records"][0]["s3"]["bucket"]["name"]
key = urllib.parse.unquote_plus(
event["Records"][0]["s3"]["object"]["key"], encoding="utf-8"
)
hash = key.replace("todo/", "").replace(".exe", "")
children = False # TODO
# Replace data in logon script template
userdata = (
logonScript.replace("REPLACE_HASH", hash)
.replace("REPLACE_BUCKET", bucket)
.replace("REPLACE_CHILDREN", str(children))
)
try:
instance = ec2.create_instances(
ImageId=AMI,
InstanceType=INSTANCE_TYPE,
TagSpecifications=[
{
"ResourceType": "instance",
"Tags": [
{"Key": "Name", "Value": hash},
{"Key": "autottd", "Value": ""},
],
}
],
KeyName=KEY_NAME,
IamInstanceProfile={"Arn": IAM_INSTANCE_PROFILE_ARN},
SecurityGroupIds=[SECURITY_GROUP_ID],
UserData=userdata,
MaxCount=1,
MinCount=1,
)
assert len(instance) == 1
return instance[0].id
except Exception as e:
print(e)
return 1