-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpc_cloudformation_template.yaml
419 lines (410 loc) · 15.1 KB
/
vpc_cloudformation_template.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
Description: Create a VPC, Internet Gateway, Subnets, Route Tables, Security Groups and EC2 Instances running the Cockroach Database
AWSTemplateFormatVersion: '2010-09-09'
#
# ToDo:
# Cluster Name
#
# MyIP 72.132.195.192
# Creating a security group that references itself
# https://gist.github.com/alanwill/9254414
#
# Using parameters
# https://curiousorbit.com/blog/cloudformation-using-parameters/
#
# AWS Parameters
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html
#
# AWS Subnet Configuration
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet.html
#
# AWS Route Table Configuration
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-routetable.html
#
# AWS Routes Configuration
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-route.html
#
# AWS Referencing resource attributes in cloud formation to build a user data script
# https://stackoverflow.com/questions/48625737/userdata-script-with-resource-attribute-cloudformation
Mappings:
amazonLinuxAmi:
us-east-1:
AMI: ami-0ed9277fb7eb570c9
us-east-2:
AMI: ami-002068ed284fb165b
us-west-1:
AMI: ami-03af6a70ccd8cb578
us-west-2:
AMI: ami-00f7e5c52c0f43726
Parameters:
VpcCidrParameter:
Description: "CIDR Block For the VPC"
Type: String
Default: "10.173.12.0/24"
VpcNamePrefix:
Description: "VPC Name Prefix. The name will be the prefix plus the region name. Ex: vpc01-us-west-2"
Type: String
Default: "vpc01"
# See the following function which splits the /24 CIDR range into 6 CIDRs with a subnet mask "/27"
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-cidr.html
VpcAzs:
Type: List<AWS::EC2::AvailabilityZone::Name>
Description: "Comma-delimited list of three AZs"
Default: "us-west-2a,us-west-2b,us-west-2c"
MyIP:
Type: String
Description: "Your IP address. This will be used in the security group configuration to provide SSH/RDP access to EC2 instances. A CIDR range of /32 will be appended."
Default: "0.0.0.0"
AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})
ConstraintDescription: must be a valid IP address of the form x.x.x.x.
KeyPairName:
Description: "Key Pair Name (specific to a region) to be used for the EC2 instances. This keypair must already exist prior to runing this CloudFormation script."
Type: "AWS::EC2::KeyPair::KeyName"
Resources:
CreateVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref "VpcCidrParameter"
EnableDnsSupport: true
EnableDnsHostnames: true
InstanceTenancy: default
Tags:
- Key: Name
Value: !Join ['-', [ !Ref 'VpcNamePrefix', !Ref 'AWS::Region']]
InternetGateway01:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: !Join ['-', [ "ig01", !Ref 'VpcNamePrefix', !Ref 'AWS::Region']]
VPCGatewayAttachment01:
DependsOn:
- CreateVPC
- InternetGateway01
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref InternetGateway01
VpcId: !Ref CreateVPC
subnet2Public:
DependsOn: CreateVPC
Type: AWS::EC2::Subnet
Properties:
# To be clear, the first line sets up the AZ, the second line substitues the AZ (a, b, c, etc) with the reference parameter value
# AvailabilityZone: !Sub
# - "${AWS::Region}${AZ}"
# - AZ: !Select [0, !Ref VpcAzs]
AvailabilityZone: !Select [0, !Ref VpcAzs]
# AvailabilityZone: "us-west-2a"
VpcId: !Ref CreateVPC
# CidrBlock: !Select [0, !Ref SubnetIpBlocks]
CidrBlock: !Select [ 0, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]]
Tags:
- Key: Name
Value: aza-pub-subnet2
MapPublicIpOnLaunch: true
subnet1Private:
DependsOn: CreateVPC
Type: AWS::EC2::Subnet
Properties:
# To be clear, the first line sets up the AZ, the second line substitues the AZ (a, b, c, etc) with the reference parameter value
# AvailabilityZone: !Sub
# - "${AWS::Region}${AZ}"
# - AZ: !Select [0, !Ref VpcAzs]
AvailabilityZone: !Select [0, !Ref VpcAzs]
# AvailabilityZone: "us-west-2a"
VpcId: !Ref CreateVPC
# CidrBlock: !Select [0, !Ref SubnetIpBlocks]
CidrBlock: !Select [ 1, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]]
Tags:
- Key: Name
Value: aza-pri-subnet1
MapPublicIpOnLaunch: false
subnet4Public:
DependsOn: CreateVPC
Type: AWS::EC2::Subnet
Properties:
# To be clear, the first line sets up the AZ, the second line substitues the AZ (a, b, c, etc) with the reference parameter value
# AvailabilityZone: !Sub
# - "${AWS::Region}${AZ}"
# - AZ: !Select [0, !Ref VpcAzs]
AvailabilityZone: !Select [1, !Ref VpcAzs]
# AvailabilityZone: "us-west-2a"
VpcId: !Ref CreateVPC
# CidrBlock: !Select [0, !Ref SubnetIpBlocks]
CidrBlock: !Select [ 2, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]]
Tags:
- Key: Name
Value: azb-pub-subnet4
MapPublicIpOnLaunch: True
subnet3Private:
DependsOn: CreateVPC
Type: AWS::EC2::Subnet
Properties:
# To be clear, the first line sets up the AZ, the second line substitues the AZ (a, b, c, etc) with the reference parameter value
# AvailabilityZone: !Sub
# - "${AWS::Region}${AZ}"
# - AZ: !Select [0, !Ref VpcAzs]
AvailabilityZone: !Select [1, !Ref VpcAzs]
# AvailabilityZone: "us-west-2a"
VpcId: !Ref CreateVPC
# CidrBlock: !Select [0, !Ref SubnetIpBlocks]
CidrBlock: !Select [ 3, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]]
Tags:
- Key: Name
Value: azb-pri-subnet3
MapPublicIpOnLaunch: false
subnet6Public:
DependsOn: CreateVPC
Type: AWS::EC2::Subnet
Properties:
# To be clear, the first line sets up the AZ, the second line substitues the AZ (a, b, c, etc) with the reference parameter value
# AvailabilityZone: !Sub
# - "${AWS::Region}${AZ}"
# - AZ: !Select [0, !Ref VpcAzs]
AvailabilityZone: !Select [2, !Ref VpcAzs]
# AvailabilityZone: "us-west-2a"
VpcId: !Ref CreateVPC
# CidrBlock: !Select [0, !Ref SubnetIpBlocks]
CidrBlock: !Select [ 4, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]]
Tags:
- Key: Name
Value: azc-pub-subnet6
MapPublicIpOnLaunch: True
subnet5Private:
DependsOn: CreateVPC
Type: AWS::EC2::Subnet
Properties:
# To be clear, the first line sets up the AZ, the second line substitues the AZ (a, b, c, etc) with the reference parameter value
# AvailabilityZone: !Sub
# - "${AWS::Region}${AZ}"
# - AZ: !Select [0, !Ref VpcAzs]
AvailabilityZone: !Select [2, !Ref VpcAzs]
# AvailabilityZone: "us-west-2a"
VpcId: !Ref CreateVPC
# CidrBlock: !Select [0, !Ref SubnetIpBlocks]
CidrBlock: !Select [ 5, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]]
Tags:
- Key: Name
Value: azc-pri-subnet5
MapPublicIpOnLaunch: false
routeTable01Private:
DependsOn: CreateVPC
Type: AWS::EC2::RouteTable
Properties:
Tags:
- Key: Name
Value: rt01-private
VpcId: !Ref CreateVPC
routeTable02Public:
DependsOn: CreateVPC
Type: AWS::EC2::RouteTable
Properties:
Tags:
- Key: Name
Value: rt02-public
VpcId: !Ref CreateVPC
privateSubnetRouteAssociations1:
DependsOn:
- subnet1Private
- routeTable01Private
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: subnet1Private
RouteTableId:
Ref: routeTable01Private
privateSubnetRouteAssociations2:
DependsOn:
- routeTable01Private
- subnet3Private
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: subnet3Private
RouteTableId:
Ref: routeTable01Private
privateSubnetRouteAssociations3:
DependsOn:
- routeTable01Private
- subnet5Private
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: subnet5Private
RouteTableId:
Ref: routeTable01Private
publicSubnetRouteAssociations1:
DependsOn:
- routeTable02Public
- subnet2Public
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: subnet2Public
RouteTableId:
Ref: routeTable02Public
publicSubnetRouteAssociations2:
DependsOn:
- routeTable02Public
- subnet4Public
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: subnet4Public
RouteTableId:
Ref: routeTable02Public
publicSubnetRouteAssociations3:
DependsOn:
- routeTable02Public
- subnet6Public
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: subnet6Public
RouteTableId:
Ref: routeTable02Public
InternetRoute:
Type: AWS::EC2::Route
DependsOn:
- VPCGatewayAttachment01
- routeTable02Public
Properties:
RouteTableId:
Ref: routeTable02Public
DestinationCidrBlock: 0.0.0.0/0
GatewayId:
Ref: InternetGateway01
InstanceSecurityGroup01:
Type: AWS::EC2::SecurityGroup
DependsOn: CreateVPC
Properties:
Tags:
- Key: Name
Value: sg01
GroupDescription: Allow desktop access (SSH, RDP, Database, HTTP) to EC2 instances
VpcId:
Ref: CreateVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Join ['/', [ !Ref 'MyIP', '32']]
Description: "SSH"
- IpProtocol: tcp
FromPort: 3389
ToPort: 3389
CidrIp: !Join ['/', [ !Ref 'MyIP', '32']]
Description: "RDP"
- IpProtocol: tcp
FromPort: 26257
ToPort: 26257
CidrIp: !Join ['/', [ !Ref 'MyIP', '32']]
Description: "CockroachDB"
- IpProtocol: tcp
FromPort: 8080
ToPort: 8080
CidrIp: !Join ['/', [ !Ref 'MyIP', '32']]
Description: "CockroachDB UI"
InstanceSecurityGroup02:
Type: AWS::EC2::SecurityGroup
DependsOn: CreateVPC
Properties:
Tags:
- Key: Name
Value: sg02
GroupDescription: Allow http to client host
VpcId:
Ref: CreateVPC
InstanceSecurityGroup02Ingress:
Type: AWS::EC2::SecurityGroupIngress
DependsOn: InstanceSecurityGroup02
Properties:
GroupId: !Ref InstanceSecurityGroup02
IpProtocol: tcp
FromPort: 0
ToPort: 65535
SourceSecurityGroupId: !Ref InstanceSecurityGroup02
Description: "Intra Node Communication"
Outputs:
StackVPC:
Description: The ID of the VPC
Value: !Ref CreateVPC
Export:
Name: !Sub "${AWS::StackName}-VPCID"
# SubnetPublic1:
# Description: The ID of the a public subnet
# Value: !Ref subnet2Public
# Export:
# Name: !Sub "${AWS::StackName}-PublicSubnet1"
# SubnetPublic2:
# Description: The ID of the a public subnet
# Value: !Ref subnet4Public
# Export:
# Name: !Sub "${AWS::StackName}-PublicSubnet2"
# SubnetPublic3:
# Description: The ID of the a public subnet
# Value: !Ref subnet6Public
# Export:
# Name: !Sub "${AWS::StackName}-PublicSubnet3"
SecurityGroup1:
Description: The ID of the security group 1 (local access to the resources)
Value: !Ref InstanceSecurityGroup01
Export:
Name: !Sub "${AWS::StackName}-SecurityGroup1"
SecurityGroup2:
Description: The ID of the security group 2 (intranode communication)
Value: !Ref InstanceSecurityGroup02
Export:
Name: !Sub "${AWS::StackName}-SecurityGroup2"
PubSubnet01:
Description: The ID of the 1st Public Subnet created by this CF Template
Value: !Ref subnet2Public
Export:
Name: !Sub "${AWS::StackName}-PubSubnet01"
PubSubnet02:
Description: The ID of the 2nd Public Subnet created by this CF Template
Value: !Ref subnet4Public
Export:
Name: !Sub "${AWS::StackName}-PubSubnet02"
PubSubnet03:
Description: The ID of the 3rd Public Subnet created by this CF Template
Value: !Ref subnet6Public
Export:
Name: !Sub "${AWS::StackName}-PubSubnet03"
# PublicSubnet01WouldBe:
# Description: This is what the Private IP Would be
# # Get the CIDR range of the 1st public subnet, then generate 16 CIDRs (for the whole range) with a /32 and select the 4th IP address
# Value: !Select [ 0, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]]
# PrivateIP01WouldBe:
# Description: This is what the Private IP Would be
# # Get the CIDR range of the 1st public subnet, then generate 16 CIDRs (for the whole range) with a /32 and select the 4th IP address
# Value: !Select [0, !Split ['.', !Select [0, !Cidr [ !Select [ 0, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]], 16, 1] ] ] ]
# PrivateIP02WouldBe:
# Description: This is what the Private IP Would be
# # Get the CIDR range of the 1st public subnet, then generate 16 CIDRs (for the whole range) with a /32 and select the 4th IP address
# Value: !Select [1, !Split ['.', !Select [0, !Cidr [ !Select [ 0, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]], 16, 1] ] ] ]
# PrivateIP03WouldBe:
# Description: This is what the Private IP Would be
# # Get the CIDR range of the 1st public subnet, then generate 16 CIDRs (for the whole range) with a /32 and select the 4th IP address
# Value: !Select [2, !Split ['.', !Select [0, !Cidr [ !Select [ 0, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]], 16, 1] ] ] ]
# PrivateIPCombined:
# Value: !Join
# - '.'
# - - !Select [0, !Split ['.', !Select [0, !Cidr [ !Select [ 0, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]], 16, 1] ] ] ]
# - !Select [1, !Split ['.', !Select [0, !Cidr [ !Select [ 0, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]], 16, 1] ] ] ]
# - !Select [2, !Split ['.', !Select [0, !Cidr [ !Select [ 0, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]], 16, 1] ] ] ]
# PrivateIP02WouldBe:
# Description: This is what the Private IP Would be
# # Get the CIDR range of the 1st public subnet, then generate 16 CIDRs (for the whole range) with a /32 and select the 4th IP address
# Value: !Select [1, !Cidr [ !Select [ 0, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]], 16, 0] ]
# PrivateIP03WouldBe:
# Description: This is what the Private IP Would be
# # Get the CIDR range of the 1st public subnet, then generate 16 CIDRs (for the whole range) with a /32 and select the 4th IP address
# Value: !Select [2, !Cidr [ !Select [ 0, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]], 16, 0] ]
# PrivateIP04WouldBe:
# Description: This is what the Private IP Would be
# # Get the CIDR range of the 1st public subnet, then generate 16 CIDRs (for the whole range) with a /32 and select the 4th IP address
# Value: !Select [3, !Cidr [ !Select [ 0, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]], 16, 0] ]
# PrivateIP16WouldBe:
# Description: This is what the Private IP Would be
# # Get the CIDR range of the 1st public subnet, then generate 16 CIDRs (for the whole range) with a /32 and select the 4th IP address
# Value: !Select [15, !Cidr [ !Select [ 0, !Cidr [ !Ref VpcCidrParameter, 6, 5 ]], 16, 0] ]