-
Notifications
You must be signed in to change notification settings - Fork 53
/
elb-to-ec2-target-group-cf-template.yml
405 lines (371 loc) · 11.8 KB
/
elb-to-ec2-target-group-cf-template.yml
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# Copyright [2024] [Phil Chen]
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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.
AWSTemplateFormatVersion: 2010-09-09
Description: Creates a VPC and a ELB with a EC2 Target Group Associated
Parameters:
SSHKeyName:
Description: 'Name of the ec2 key you need one to use this template'
Type: 'AWS::EC2::KeyPair::KeyName'
Default: 'choose-key'
Mappings:
RegionToAmazonAMI:
us-east-1:
HVM64: ami-09479453c5cde9639
us-east-2:
HVM64: ami-023c8dbf8268fb3ca
us-west-1:
HVM64: ami-06ff511fea5db2c99
us-west-2:
HVM64: ami-0bb5806b2e825a199
NetworkToSubnet:
"10.0.0.0":
PubSubnetZoneA: "10.0.10.0/24"
PrivSubnetZoneA: "10.0.20.0/24"
PubSubnetZoneB: "10.0.30.0/24"
PrivSubnetZoneB: "10.0.40.0/24"
Resources:
# VPC
VPC:
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock:
'Fn::Join': [ '/', [ '10.0.0.0', '16' ] ]
EnableDnsSupport: true
EnableDnsHostnames: true
InstanceTenancy: 'default'
Tags:
- Key: 'Name'
Value:
Ref: 'AWS::StackName'
# Internet accessable subnet in the first availability zone
PubSubnetZoneA:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone:
Fn::Select:
- '0'
- Fn::GetAZs:
Ref: 'AWS::Region'
CidrBlock:
Fn::FindInMap:
- NetworkToSubnet
- '10.0.0.0'
- PubSubnetZoneA
MapPublicIpOnLaunch: 'True'
VpcId:
Ref: 'VPC'
Tags:
- Key: 'Name'
Value:
'Fn::Join': [ ':', [ 'Public', 'Zone A', !Ref 'AWS::StackName' ] ]
# Non-internet accessable subnet in the first availability zone
PrivSubnetZoneA:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone:
Fn::Select:
- '0'
- Fn::GetAZs:
Ref: 'AWS::Region'
CidrBlock:
Fn::FindInMap:
- NetworkToSubnet
- '10.0.0.0'
- PrivSubnetZoneA
MapPublicIpOnLaunch: 'False'
VpcId:
Ref: 'VPC'
Tags:
- Key: 'Name'
Value:
'Fn::Join': [ ':', [ 'Private', 'Zone A', !Ref 'AWS::StackName' ] ]
# Internet accessable subnet in the second availability zone
PubSubnetZoneB:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone:
Fn::Select:
- '1'
- Fn::GetAZs:
Ref: 'AWS::Region'
CidrBlock:
Fn::FindInMap:
- NetworkToSubnet
- '10.0.0.0'
- PubSubnetZoneB
MapPublicIpOnLaunch: 'True'
VpcId:
Ref: 'VPC'
Tags:
- Key: 'Name'
Value:
'Fn::Join': [ ':', [ 'Public', 'Zone B', !Ref 'AWS::StackName' ] ]
# Non-internet accessable subnet in the second availability zone
PrivSubnetZoneB:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone:
Fn::Select:
- '1'
- Fn::GetAZs:
Ref: 'AWS::Region'
CidrBlock:
Fn::FindInMap:
- NetworkToSubnet
- '10.0.0.0'
- PrivSubnetZoneB
MapPublicIpOnLaunch: 'False'
VpcId:
Ref: 'VPC'
Tags:
- Key: 'Name'
Value:
'Fn::Join': [ ':', [ 'Private', 'Zone B', !Ref 'AWS::StackName' ] ]
# Gateway to the internet
InternetGateway:
Type: 'AWS::EC2::InternetGateway'
Properties:
Tags:
- Key: 'Name'
Value:
Ref: 'AWS::StackName'
# Associate the gateway to the VPC
GatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId:
Ref: 'InternetGateway'
VpcId:
Ref: 'VPC'
# Routing table for the public subnet in availability zone A
RouteTablePubZoneA:
Type: AWS::EC2::RouteTable
Properties:
VpcId:
Ref: 'VPC'
Tags:
- Key: 'Name'
Value:
'Fn::Join': [ ':', [ 'Public', 'Zone A', !Ref 'AWS::StackName' ] ]
# Routing table for the public subnet in availability zone B
RouteTablePubZoneB:
Type: AWS::EC2::RouteTable
Properties:
VpcId:
Ref: 'VPC'
Tags:
- Key: 'Name'
Value:
'Fn::Join': [ ':', [ 'Public', 'Zone B', !Ref 'AWS::StackName' ] ]
# Route traffic through the internet gateway
RoutePubZoneA:
Type: AWS::EC2::Route
Properties:
DestinationCidrBlock: '0.0.0.0/0'
GatewayId:
Ref: 'InternetGateway'
RouteTableId:
Ref: 'RouteTablePubZoneA'
# Route traffic through the internet gateway
RoutePubZoneB:
Type: AWS::EC2::Route
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId:
Ref: 'InternetGateway'
RouteTableId:
Ref: 'RouteTablePubZoneB'
# Associate public subnet to its routing table
RouteAssociationPubSubnetZoneA:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: 'PubSubnetZoneA'
RouteTableId:
Ref: 'RouteTablePubZoneA'
# Associate public subnet to its routing table
RouteAssociationPubSubnetZoneB:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: 'PubSubnetZoneB'
RouteTableId:
Ref: 'RouteTablePubZoneB'
# Routing table for the private subnet in availability zone A
RouteTablePrivZoneA:
Type: AWS::EC2::RouteTable
Properties:
VpcId:
Ref: 'VPC'
Tags:
- Key: 'Name'
Value:
'Fn::Join': [ ':', [ 'Private', 'Zone A', !Ref 'AWS::StackName' ] ]
# Routing table for the private subnet in availability zone B
RouteTablePrivZoneB:
Type: AWS::EC2::RouteTable
Properties:
VpcId:
Ref: 'VPC'
Tags:
- Key: 'Name'
Value:
'Fn::Join': [ ':', [ 'Private', 'Zone B', !Ref 'AWS::StackName' ] ]
# Associate the private subnet with its routing table
RouteAssociationPrivSubnetZoneA:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: 'PrivSubnetZoneA'
RouteTableId:
Ref: 'RouteTablePrivZoneA'
# Associate the private subnet with its routing table
RouteAssociationPrivSubnetZoneB:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: 'PrivSubnetZoneB'
RouteTableId:
Ref: 'RouteTablePrivZoneB'
# EC2 Security Group Allowing Port 22 and 80 from anywhere
EC2SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: 'SSH and Port 80'
VpcId:
Ref: VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 80
ToPort: 80
SourceSecurityGroupId:
Ref: ELBSecurityGroup
# ELB Security Group allowing Port 80 from anywhere
ELBSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: 'SSH and Port 80'
VpcId:
Ref: VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
# Linux Instance with Apache running on Port 80
AmazonLinuxInstance:
Type: AWS::EC2::Instance
Properties:
ImageId:
Fn::FindInMap:
- RegionToAmazonAMI
- Ref: 'AWS::Region'
- HVM64
InstanceInitiatedShutdownBehavior: stop
InstanceType: t2.nano
KeyName:
Ref: SSHKeyName
Monitoring: 'true'
NetworkInterfaces:
- AssociatePublicIpAddress: 'true'
DeviceIndex: '0'
GroupSet:
- !Ref EC2SecurityGroup
SubnetId:
Ref: PubSubnetZoneA
Tenancy: default
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
cd /tmp
yum update -y
yum install -y httpd24
echo "Healthy" > /var/www/html/index.html
service httpd start
/opt/aws/bin/cfn-signal \
-e $? \
--stack ${AWS::StackName} \
--resource AmazonLinuxInstance \
--region ${AWS::Region}
# Target Group
EC2TargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckIntervalSeconds: 30
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 15
HealthyThresholdCount: 5
Matcher:
HttpCode: '200'
Name: EC2TargetGroup
Port: 80
Protocol: HTTP
TargetGroupAttributes:
- Key: deregistration_delay.timeout_seconds
Value: '20'
Targets:
- Id:
Ref: AmazonLinuxInstance
Port: 80
UnhealthyThresholdCount: 3
VpcId:
Ref: 'VPC'
Tags:
- Key: Name
Value: EC2TargetGroup
- Key: Port
Value: 80
#ELB (ALB)
ALBListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn:
Ref: EC2TargetGroup
LoadBalancerArn:
Ref: ApplicationLoadBalancer
Port: 80
Protocol: HTTP
ApplicationLoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Scheme: internet-facing # or internal
Subnets:
- Ref: PubSubnetZoneA
- Ref: PubSubnetZoneB
SecurityGroups:
- Ref: ELBSecurityGroup
Outputs:
VPC:
Description: 'Virtual Private Cloud'
Value:
Ref: 'VPC'
ALBHostName:
Description: 'Application Load Balancer Hostname'
Value:
!GetAtt ApplicationLoadBalancer.DNSName
EC2Instance:
Description: 'EC2 Instance'
Value:
Ref: AmazonLinuxInstance
EC2TargetGroup:
Description: 'EC2 Target Group'
Value:
Ref: EC2TargetGroup
ApplicationLoadBalancer:
Description: 'Application Load Balancer'
Value:
Ref: ApplicationLoadBalancer