-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-stop-environment-cf.yaml
314 lines (309 loc) · 8.52 KB
/
start-stop-environment-cf.yaml
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
AWSTemplateFormatVersion: 2010-09-09
Metadata:
'AWS::CloudFormation::Designer':
42b391b8-8760-4e05-86e6-b2603aa9e203:
size:
width: 60
height: 60
position:
x: 60
'y': 80
z: 0
embeds: []
isassociatedwith:
- 2d605179-bffb-473b-a272-092cc16c474c
2d605179-bffb-473b-a272-092cc16c474c:
size:
width: 60
height: 60
position:
x: -72.5
'y': 77.5
z: 0
embeds: []
86669b58-9605-4dcf-9e41-4b8f2293aedf:
size:
width: 60
height: 60
position:
x: -120
'y': 170
z: 0
embeds: []
3260e887-1314-461c-951b-758fcc6b42dd:
size:
width: 60
height: 60
position:
x: 90
'y': 170
z: 0
embeds: []
0d95330d-54f4-4e82-8dbd-c876a633acc8:
size:
width: 60
height: 60
position:
x: -40
'y': 170
z: 0
embeds: []
isassociatedwith:
- 86669b58-9605-4dcf-9e41-4b8f2293aedf
8650c417-22c8-48a9-a520-1ba12e31f41f:
size:
width: 60
height: 60
position:
x: -120
'y': 280
z: 0
embeds: []
7fcdae94-571e-4802-a9f8-197eca66b085:
size:
width: 60
height: 60
position:
x: 90
'y': 280
z: 0
embeds: []
1660e06c-bfa8-4a8f-b7cf-70ab5df022fc:
size:
width: 60
height: 60
position:
x: 180
'y': 170
z: 0
embeds: []
isassociatedwith:
- 86669b58-9605-4dcf-9e41-4b8f2293aedf
- 3260e887-1314-461c-951b-758fcc6b42dd
Parameters:
SshKeyNameParameter:
Type: String
Default: Your-SSH-Key-Name
Description: The EC2 keypair name for instance SSH access.
Resources:
ManageEnvironmentIamPolicy:
Type: 'AWS::IAM::Policy'
Properties:
PolicyName: ManageEnvPolicy
Roles:
- Ref: ManageEnvRole
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:DeleteLogGroup'
- 'logs:DeleteLogStream'
- 'logs:DescribeLogGroups'
- 'logs:GetLogEvents'
- 'logs:PutLogEvents'
- 'logs:PutRetentionPolicy'
Resource: 'arn:aws:logs:*:*:*'
- Effect: Allow
Action:
- 'iam:AddRoleToInstanceProfile'
- 'iam:CreateInstanceProfile'
- 'iam:CreateRole'
- 'iam:DeleteInstanceProfile'
- 'iam:DeleteRole'
- 'iam:DeleteRolePolicy'
- 'iam:GetRole'
- 'iam:PassRole'
- 'iam:PutRolePolicy'
- 'iam:RemoveRoleFromInstanceProfile'
Resource: '*'
- Effect: Allow
Action:
- 'cloudformation:*'
- 'ec2:*'
- 'rds:*'
- 'autoscaling:*'
- 'elasticloadbalancing:*'
- 'elasticfilesystem:*'
- 'elasticloadbalancing:*'
- 'lambda:*'
- 'dynamodb:*'
- 'sqs:*'
Resource: '*'
- Effect: Deny
Action: 'aws-portal:*Billing'
Resource: '*'
- Effect: Deny
Action:
- 'cloudtrail:DeleteTrail'
- 'cloudtrail:StopLogging'
- 'cloudtrail:UpdateTrail'
Resource: '*'
Metadata:
'AWS::CloudFormation::Designer':
id: 42b391b8-8760-4e05-86e6-b2603aa9e203
ManageEnvRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: ManageEnvRole
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Action: 'sts:AssumeRole'
Principal:
Service:
- lambda.amazonaws.com
Effect: Allow
Sid: ''
Metadata:
'AWS::CloudFormation::Designer':
id: 2d605179-bffb-473b-a272-092cc16c474c
StartEnvironment:
Type: 'AWS::Lambda::Function'
Properties:
Handler: index.handler
MemorySize: 128
Timeout: 15
Role: !GetAtt
- ManageEnvRole
- Arn
Runtime: nodejs6.10
Code:
ZipFile: |
exports.handler = function(event, context, callback) {
var AWS = require('aws-sdk');
var cloudformation = new AWS.CloudFormation();
var params = {
StackName: event.stackName, /* required */
Capabilities: [
'CAPABILITY_IAM'
],
EnableTerminationProtection: false,
OnFailure: 'ROLLBACK', // DO_NOTHING | ROLLBACK | DELETE,
Parameters: [
{
ParameterKey: 'KeyName',
ParameterValue: event.keyPairName
},
{
ParameterKey: 'ManagerSize',
ParameterValue: event.managerSize || '1'
},
{
ParameterKey: 'ClusterSize',
ParameterValue: event.clusterSize || '1'
}
],
Tags: [
{
Key: 'CloudFormationStack',
Value: event.stackName
}
],
TemplateURL: 'https://editions-us-east-1.s3.amazonaws.com/aws/stable/Docker.tmpl',
TimeoutInMinutes: 20
};
cloudformation.createStack(params, function(err, data) {
if (err) {
callback("Error creating the Stack: "+err);
}
else {
callback(null, "Success creating the Stack.");
}
});
}
Metadata:
'AWS::CloudFormation::Designer':
id: 86669b58-9605-4dcf-9e41-4b8f2293aedf
StopEnvironment:
Type: 'AWS::Lambda::Function'
Properties:
Handler: index.handler
MemorySize: 128
Timeout: 15
Role: !GetAtt
- ManageEnvRole
- Arn
Runtime: nodejs6.10
Code:
ZipFile: |
exports.handler = function(event, context, callback) {
var AWS = require('aws-sdk');
var cloudformation = new AWS.CloudFormation();
var params = {
StackName: event.stackName /* required */
};
cloudformation.deleteStack(params, function(err, data) {
if (err) {
callback("Error deleting the Stack: "+err);
}
else {
callback(null, "Success deleting the Stack.");
}
});
}
Metadata:
'AWS::CloudFormation::Designer':
id: 3260e887-1314-461c-951b-758fcc6b42dd
AllowCloudwatchStartEnv:
Type: 'AWS::Lambda::Permission'
Properties:
Action: 'lambda:InvokeFunction'
FunctionName: !GetAtt
- StartEnvironment
- Arn
Principal: events.amazonaws.com
SourceArn: !GetAtt
- StartRule
- Arn
Metadata:
'AWS::CloudFormation::Designer':
id: 0d95330d-54f4-4e82-8dbd-c876a633acc8
StartRule:
Type: 'AWS::Events::Rule'
Properties:
Name: StartEnvironmentRule
ScheduleExpression: cron(30 14 ? * 2-6 *)
Targets:
- Arn: !GetAtt
- StartEnvironment
- Arn
Id: start_environment_rule_target
Input: !Join
- ''
- - '{ "stackName": "MyStack", "keyPairName": "'
- Ref: SshKeyNameParameter
- '" }'
Metadata:
'AWS::CloudFormation::Designer':
id: 8650c417-22c8-48a9-a520-1ba12e31f41f
StopRule:
Type: 'AWS::Events::Rule'
Properties:
Name: StopEnvironmentRule
ScheduleExpression: cron(0 15 ? * 2-6 *)
Targets:
- Arn: !GetAtt
- StopEnvironment
- Arn
Id: stop_environment_rule_target
Input: '{ "stackName": "MyStack" }'
Metadata:
'AWS::CloudFormation::Designer':
id: 7fcdae94-571e-4802-a9f8-197eca66b085
AllowCloudwatchStopEnv:
Type: 'AWS::Lambda::Permission'
Properties:
Action: 'lambda:InvokeFunction'
FunctionName: !GetAtt
- StopEnvironment
- Arn
Principal: events.amazonaws.com
SourceArn: !GetAtt
- StopRule
- Arn
Metadata:
'AWS::CloudFormation::Designer':
id: 1660e06c-bfa8-4a8f-b7cf-70ab5df022fc