-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2-spot-fleet-web-app.yaml
308 lines (305 loc) · 7.22 KB
/
ec2-spot-fleet-web-app.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
---
AWSTemplateFormatVersion: 2010-09-09
Description: EC2 Spot Fleet web app workshop template
Mappings:
CidrMappings:
public-subnet-1:
CIDR: 10.0.1.0/24
public-subnet-2:
CIDR: 10.0.2.0/24
vpc:
CIDR: 10.0.0.0/16
Metadata:
Author:
Description: Chad Schmutzer <[email protected]>
License:
Description: 'Copyright 2017 Amazon.com, Inc. and its affiliates. All Rights Reserved.
Licensed under the Amazon Software License (the "License"). You may not use this file
except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/asl/
or in the "license" file accompanying this file. This file is distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under the License.'
Outputs:
awsRegionId:
Description: The AWS Region ID your template was launched in
Value:
Ref: AWS::Region
eventRule:
Description: Events rule
Value:
Ref: eventRule
lambdaFunction:
Description: Lambda function
Value:
Ref: lambdaFunction
loadBalancerSecurityGroup:
Description: Load Balancer security group
Value:
Ref: loadBalancerSecurityGroup
publicSubnet1:
Description: Public subnet 1
Value:
Ref: publicSubnet1
publicSubnet2:
Description: Public subnet 2
Value:
Ref: publicSubnet2
snsTopic:
Description: SNS topic ARN
Value:
Ref: snsTopic
spotFleetRole:
Description: Spot Fleet role
Value:
Fn::GetAtt:
- spotFleetRole
- Arn
Resources:
attachGateway:
DependsOn:
- vpc
- internetGateway
Properties:
InternetGatewayId:
Ref: internetGateway
VpcId:
Ref: vpc
Type: AWS::EC2::VPCGatewayAttachment
eventRule:
DependsOn:
- snsTopic
Properties:
Description: Events rule for EC2 Spot Instance Interruption Notices
EventPattern:
detail-type:
- EC2 Spot Instance Interruption Warning
source:
- aws.ec2
State: ENABLED
Targets:
- Arn:
Ref: snsTopic
Id:
Fn::GetAtt:
- snsTopic
- TopicName
- Arn:
Fn::GetAtt:
- lambdaFunction
- Arn
Id:
Ref: lambdaFunction
Type: AWS::Events::Rule
internetGateway:
DependsOn:
- vpc
Type: AWS::EC2::InternetGateway
lambdaFunction:
DependsOn:
- lambdaFunctionRole
Properties:
Code:
ZipFile: "def handler(event, context):\n return\n"
Handler: index.handler
Role:
Fn::GetAtt:
- lambdaFunctionRole
- Arn
Runtime: python3.6
Type: AWS::Lambda::Function
lambdaFunctionPermission:
Properties:
Action: lambda:InvokeFunction
FunctionName:
Fn::GetAtt:
- lambdaFunction
- Arn
Principal: events.amazonaws.com
SourceArn:
Fn::GetAtt:
- eventRule
- Arn
Type: AWS::Lambda::Permission
lambdaFunctionRole:
Properties:
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Version: 2012-10-17
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Path: /
Policies:
- PolicyDocument:
Statement:
- Action: elasticloadbalancing:DeregisterTargets
Effect: Allow
Resource: '*'
- Action: ec2:DescribeTags
Effect: Allow
Resource: '*'
Version: '2012-10-17'
PolicyName:
Fn::Join:
- '-'
- - Ref: AWS::StackName
- lambdaFunctionRole
Type: AWS::IAM::Role
loadBalancerSecurityGroup:
DependsOn:
- vpc
Properties:
GroupDescription: Allow http to 0.0.0.0/0
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: 80
IpProtocol: tcp
ToPort: 80
VpcId:
Ref: vpc
Type: AWS::EC2::SecurityGroup
publicRoute:
DependsOn:
- publicRouteTable
- internetGateway
- attachGateway
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId:
Ref: internetGateway
RouteTableId:
Ref: publicRouteTable
Type: AWS::EC2::Route
publicRouteTable:
DependsOn:
- vpc
- attachGateway
Properties:
Tags:
- Key: Name
Value: Public Route Table
VpcId:
Ref: vpc
Type: AWS::EC2::RouteTable
publicSubnet1:
DependsOn: attachGateway
Properties:
AvailabilityZone:
Fn::Select:
- 0
- Fn::GetAZs:
Ref: AWS::Region
CidrBlock:
Fn::FindInMap:
- CidrMappings
- public-subnet-1
- CIDR
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: Public Subnet 1
VpcId:
Ref: vpc
Type: AWS::EC2::Subnet
publicSubnet1RouteTableAssociation:
DependsOn:
- publicRouteTable
- publicSubnet1
- attachGateway
Properties:
RouteTableId:
Ref: publicRouteTable
SubnetId:
Ref: publicSubnet1
Type: AWS::EC2::SubnetRouteTableAssociation
publicSubnet2:
DependsOn: attachGateway
Properties:
AvailabilityZone:
Fn::Select:
- 1
- Fn::GetAZs:
Ref: AWS::Region
CidrBlock:
Fn::FindInMap:
- CidrMappings
- public-subnet-2
- CIDR
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: Public Subnet 2
VpcId:
Ref: vpc
Type: AWS::EC2::Subnet
publicSubnet2RouteTableAssociation:
DependsOn:
- publicRouteTable
- publicSubnet2
- attachGateway
Properties:
RouteTableId:
Ref: publicRouteTable
SubnetId:
Ref: publicSubnet2
Type: AWS::EC2::SubnetRouteTableAssociation
snsTopic:
Properties:
DisplayName: SNS Topic for EC2 Spot Instance Interruption Notices
Type: AWS::SNS::Topic
snsTopicPolicy:
DependsOn:
- snsTopic
Properties:
PolicyDocument:
Id:
Fn::GetAtt:
- snsTopic
- TopicName
Statement:
- Action: sns:Publish
Effect: Allow
Principal:
Service:
- events.amazonaws.com
Resource:
Ref: snsTopic
Version: '2012-10-17'
Topics:
- Ref: snsTopic
Type: AWS::SNS::TopicPolicy
spotFleetRole:
Properties:
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- spotfleet.amazonaws.com
Version: 2012-10-17
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetTaggingRole
Path: /
Type: AWS::IAM::Role
vpc:
Properties:
CidrBlock:
Fn::FindInMap:
- CidrMappings
- vpc
- CIDR
EnableDnsHostnames: true
EnableDnsSupport: true
Tags:
- Key: Name
Value: EC2 Spot Fleet web app workshop
Type: AWS::EC2::VPC
...