-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEc2customResource.yaml
More file actions
102 lines (96 loc) · 3.61 KB
/
Ec2customResource.yaml
File metadata and controls
102 lines (96 loc) · 3.61 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
AWSTemplateFormatVersion: 2010-09-09
Parameters:
InstanceType:
Description: EC2 instance type
Type: String
Default: t2.micro
ConstraintDescription: Must be a valid EC2 instance type
InstanceAMI:
Type: String
Default: ami-0ed9277fb7eb570c9
InstanceNumber:
Type: String
Default: 4
Resources:
Instance:
Type: 'Custom::EC2instances'
Properties:
ServiceToken: !GetAtt CreateEC2function.Arn
InstanceType: !Ref InstanceType
InstanceAMI: !Ref InstanceAMI
InstanceNumber: !Ref InstanceNumber
CreateEC2function:
Type: 'AWS::Lambda::Function'
Properties:
Code:
ZipFile: !Sub |
import json
import boto3
from botocore.vendored import requests
ec2_client = boto3.client('ec2')
def lambda_handler(event, context):
# immediate response to CF Stack DELETE Action
responseStatus = 'SUCCESS'
responseData = {}
if event['RequestType'] == 'Delete':
sendResponse(event, context, responseStatus, responseData)
else:
numInstances= int(event['ResourceProperties']['InstanceNumber'])
ami= event['ResourceProperties']['InstanceAMI']
type= event['ResourceProperties']['InstanceType']
response = ec2_client.run_instances( ImageId=ami, InstanceType=type,MaxCount=numInstances,MinCount=numInstances);
sendResponse(event, context, responseStatus, responseData)
# send response to the pre-signed S3 URL
def sendResponse(event, context, responseStatus, responseData):
responseBody = {'Status': responseStatus,
'Reason': 'See the details in CloudWatch Log Stream: ' + context.log_stream_name,
'PhysicalResourceId': context.log_stream_name,
'StackId': event['StackId'],
'RequestId': event['RequestId'],
'LogicalResourceId': event['LogicalResourceId'],
'Data': responseData}
print ('RESPONSE BODY:\n' + json.dumps(responseBody))
try:
req = requests.put(event['ResponseURL'], data=json.dumps(responseBody))
if req.status_code != 200:
print(req.text)
raise Exception('Recieved non 200 response while sending response to CF Stack.')
return
except requests.exceptions.RequestException as e:
print(e)
raise
if __name__ == '__main__':
lambda_handler('event', 'handler')
Handler: !Join
- ''
- - index
- .lambda_handler
Role: !GetAtt
- LambdaExecutionRole
- Arn
Runtime: python3.7
Timeout: '30'
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal: {Service: [lambda.amazonaws.com]}
Action: ['sts:AssumeRole']
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: EC2Policy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'ec2:RunInstances'
- 'ec2:DescribeInstances'
- 'ec2:DescribeInstanceStatus'
- 'ec2:TerminateInstances'
Resource: ['*']