forked from thinegan/cloudformation-project1
-
Notifications
You must be signed in to change notification settings - Fork 4
/
webapp-vpc.yaml
332 lines (280 loc) · 9.47 KB
/
webapp-vpc.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
# Note, this code use NatGateway instead of NatInstance.
# Its much simpler and reliable structure, but its not free tier
# http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/vpc-nat-comparison.html
#
# NatGateway start charges immediately when its turn on. ~ ($0.045 per hour)
# For more detail refer, https://aws.amazon.com/vpc/pricing/
#
---
AWSTemplateFormatVersion: "2010-09-09"
Description: >
This template deploys a VPC, with a pair of public and private subnets spread
across two (Zone A & Zone B) Availabilty Zones. It deploys an Internet Gateway,
with a default route on the public subnets. It deploy NAT Gateway (in one public AZ),
and default routes for them in the private subnets.
Parameters:
PMServerEnv:
Description: "Server Environment name."
ConstraintDescription: "Choose an Environment from the drop down"
Type: "String"
AllowedValues:
- "dev"
- "staging"
- "prod"
PMVpcCIDR:
Description: "Please enter the IP range (CIDR notation) for this VPC"
Type: "String"
PMPublicSubnet1CIDR:
Description: "Please enter the IP range (CIDR notation) for the public subnet in the first Availability Zone"
Type: "String"
PMPublicSubnet2CIDR:
Description: "Please enter the IP range (CIDR notation) for the public subnet in the second Availability Zone"
Type: "String"
PMPrivateSubnet1CIDR:
Description: "Please enter the IP range (CIDR notation) for the private subnet in the first Availability Zone"
Type: "String"
PMPrivateSubnet2CIDR:
Description: "Please enter the IP range (CIDR notation) for the private subnet in the second Availability Zone"
Type: "String"
PMFlowLogRole:
Description: "VPC Flow Log Role Description"
Type: "String"
Resources:
####### Create custom VPC and send log to S3 bucket #######
VPC:
Type: "AWS::EC2::VPC"
Properties:
CidrBlock: !Ref "PMVpcCIDR"
Tags:
- Key: "Name"
Value: !Sub "${PMServerEnv}-MyVPC"
VPCFlowLog:
Type: "AWS::EC2::FlowLog"
Properties:
DeliverLogsPermissionArn: !Ref "PMFlowLogRole"
LogGroupName: !Sub "${PMServerEnv}-VPCFlowLogsGroup"
ResourceId: !Ref "VPC"
ResourceType: "VPC"
TrafficType: "ALL"
####### Create Public Subnet #######
PublicSubnet1:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref "VPC"
CidrBlock: !Ref "PMPublicSubnet1CIDR"
AvailabilityZone: !Select [ '0', !GetAZs ]
MapPublicIpOnLaunch: "True"
Tags:
- Key: "Name"
Value: !Sub "${PMServerEnv}-${PMPublicSubnet1CIDR}-PublicSubnet1"
PublicSubnet2:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref "VPC"
CidrBlock: !Ref "PMPublicSubnet2CIDR"
AvailabilityZone: !Select [ '1', !GetAZs ]
MapPublicIpOnLaunch: "True"
Tags:
- Key: "Name"
Value: !Sub "${PMServerEnv}-${PMPublicSubnet2CIDR}-PublicSubnet2"
######## Create Private Subnet #######
PrivateSubnet1:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref "VPC"
CidrBlock: !Ref "PMPrivateSubnet1CIDR"
AvailabilityZone: !Select [ '0', !GetAZs ]
Tags:
- Key: "Name"
Value: !Sub "${PMServerEnv}-${PMPrivateSubnet1CIDR}-PrivateSubnet1"
PrivateSubnet2:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref "VPC"
CidrBlock: !Ref "PMPrivateSubnet2CIDR"
AvailabilityZone: !Select [ '1', !GetAZs ]
Tags:
- Key: "Name"
Value: !Sub "${PMServerEnv}-${PMPrivateSubnet2CIDR}-PrivateSubnet2"
######## Create Internet Gateway #######
MyInternetGateway:
Type: "AWS::EC2::InternetGateway"
Properties:
Tags:
- Key: "Name"
Value: !Sub "${PMServerEnv}-MyIGW"
######## Attach Internet Gateway to VPC #######
GatewayToInternet:
Type: "AWS::EC2::VPCGatewayAttachment"
Properties:
VpcId: !Ref "VPC"
InternetGatewayId: !Ref "MyInternetGateway"
######## Create Public Route Table #######
MyPublicRouteTable1:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref "VPC"
Tags:
- Key: "Name"
Value: !Sub "${PMServerEnv}-MyPublicRoute1"
MyPublicRouteTable2:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref "VPC"
Tags:
- Key: "Name"
Value: !Sub "${PMServerEnv}-MyPublicRoute2"
######## Create Private Route Table #######
MyPrivateRouteTable1:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref "VPC"
Tags:
- Key: "Name"
Value: !Sub "${PMServerEnv}-MyPrivateRoute1"
MyPrivateRouteTable2:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref "VPC"
Tags:
- Key: "Name"
Value: !Sub "${PMServerEnv}-MyPrivateRoute2"
######## Route-out Public Route Table to Internet Gateway (Internet connection) #######
MyPublicRouteIGW1:
Type: "AWS::EC2::Route"
DependsOn: "GatewayToInternet"
Properties:
RouteTableId: !Ref "MyPublicRouteTable1"
DestinationCidrBlock: "0.0.0.0/0"
GatewayId: !Ref "MyInternetGateway"
MyPublicRouteIGW2:
Type: "AWS::EC2::Route"
DependsOn: "GatewayToInternet"
Properties:
RouteTableId: !Ref "MyPublicRouteTable2"
DestinationCidrBlock: "0.0.0.0/0"
GatewayId: !Ref "MyInternetGateway"
######## Associate Public Route Table with Public Subnet1 & Subnet2 #######
MyPublicSubnet1RouteTableAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref "PublicSubnet1"
RouteTableId: !Ref "MyPublicRouteTable1"
MyPublicSubnet2RouteTableAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref "PublicSubnet2"
RouteTableId: !Ref "MyPublicRouteTable2"
######## Associate Private Route Table with Private Subnet1 & Subnet2 #######
MyPrivateSubnet1RouteTableAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref "PrivateSubnet1"
RouteTableId: !Ref "MyPrivateRouteTable1"
MyPrivateSubnet2RouteTableAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref "PrivateSubnet2"
RouteTableId: !Ref "MyPrivateRouteTable2"
######## Create Nat Gateway in Public Subnet #######
######## Allocate Elastic IP to Nat Gateway #######
######## Target Private Route Table with Nat Gateway #######
MyNATGateway1EIP:
Type: "AWS::EC2::EIP"
DependsOn: "GatewayToInternet"
Properties:
Domain: "VPC"
MyNATGateway2EIP:
Type: "AWS::EC2::EIP"
DependsOn: "GatewayToInternet"
Properties:
Domain: "VPC"
MyNATGateway1:
Type: "AWS::EC2::NatGateway"
DependsOn: "GatewayToInternet"
Properties:
AllocationId:
Fn::GetAtt:
- "MyNATGateway1EIP"
- "AllocationId"
SubnetId: !Ref "PublicSubnet1"
MyNATGateway2:
Type: "AWS::EC2::NatGateway"
DependsOn: "GatewayToInternet"
Properties:
AllocationId:
Fn::GetAtt:
- "MyNATGateway2EIP"
- "AllocationId"
SubnetId: !Ref "PublicSubnet2"
MyNatPrivateRoute1:
Type: "AWS::EC2::Route"
Properties:
RouteTableId: !Ref "MyPrivateRouteTable1"
DestinationCidrBlock: "0.0.0.0/0"
NatGatewayId: !Ref "MyNATGateway1"
MyNatPrivateRoute2:
Type: "AWS::EC2::Route"
Properties:
RouteTableId: !Ref "MyPrivateRouteTable2"
DestinationCidrBlock: "0.0.0.0/0"
NatGatewayId: !Ref "MyNATGateway2"
######## Create Custom Network ACL #######
MyNetworkACL:
Type: "AWS::EC2::NetworkAcl"
Properties:
VpcId: !Ref "VPC"
Tags:
- Key: "Name"
Value: !Sub "${PMServerEnv}-MyNetworkACL"
######## Associate Public Subnet to Network ACL #######
MyPublicSubnet1NetworkAclAssociation:
Type: "AWS::EC2::SubnetNetworkAclAssociation"
Properties:
SubnetId: !Ref "PublicSubnet1"
NetworkAclId: !Ref "MyNetworkACL"
MyPublicSubnet2NetworkAclAssociation:
Type: "AWS::EC2::SubnetNetworkAclAssociation"
Properties:
SubnetId: !Ref "PublicSubnet2"
NetworkAclId: !Ref "MyNetworkACL"
######## Associate Private Subnet to Network ACL #######
MyPrivateSubnet1NetworkAclAssociation:
Type: "AWS::EC2::SubnetNetworkAclAssociation"
Properties:
SubnetId: !Ref "PrivateSubnet1"
NetworkAclId: !Ref "MyNetworkACL"
MyPrivateSubnet2NetworkAclAssociation:
Type: "AWS::EC2::SubnetNetworkAclAssociation"
Properties:
SubnetId: !Ref "PrivateSubnet2"
NetworkAclId: !Ref "MyNetworkACL"
Outputs:
VPC:
Description: "A reference to the created VPC"
Value: !Ref "VPC"
Export:
Name: !Sub "${PMServerEnv}-VPC"
MyNetworkACL:
Description: "A reference to the created VPC"
Value: !Ref "MyNetworkACL"
PublicSubnets:
Description: "A list of the public subnets"
Value: !Join [ ",", [ !Ref "PublicSubnet1", !Ref "PublicSubnet2" ]]
PrivateSubnets:
Description: "A list of the private subnets"
Value: !Join [ ",", [ !Ref "PrivateSubnet1", !Ref "PrivateSubnet2" ]]
Export:
Name: !Sub "${PMServerEnv}-PrivateSubnets"
PublicSubnet1:
Description: "A reference to the public subnet in the 1st Availability Zone"
Value: !Ref "PublicSubnet1"
PublicSubnet2:
Description: "A reference to the public subnet in the 2nd Availability Zone"
Value: !Ref "PublicSubnet2"
PrivateSubnet1:
Description: "A reference to the private subnet in the 1st Availability Zone"
Value: !Ref "PrivateSubnet1"
PrivateSubnet2:
Description: "A reference to the private subnet in the 2nd Availability Zone"
Value: !Ref "PrivateSubnet2"