-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpublic-private.yaml
265 lines (247 loc) · 6.16 KB
/
public-private.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
---
AWSTemplateFormatVersion: '2010-09-09'
Description: Create one public and one private subnet. Can create a VPC or use an existing one.
### Stack metadata
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: VPC
Parameters:
- AvailabilityZone
- VpcCIDR
- VpcId
- InternetGatewayId
- Label:
default: Subnets
Parameters:
- PublicCIDR
- PrivateCIDR
Parameters:
AvailabilityZone:
Description: "(Optional) Availability Zone in which you want to create your subnet(s). Required if you opt to create a VPC."
Type: String
InternetGatewayId:
Description: "(Optional) The id of the Internet Gateway. Required if using an existing VPC."
Type: String
Default: ''
PrivateCIDR:
AllowedPattern: "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/(1[6-9]|2[0-9]|3[0-2])$"
Description: CIDR block for the private subnet
Default: 10.0.16.0/20
Type: String
PublicCIDR:
AllowedPattern: "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/(1[6-9]|2[0-9]|3[0-2])$"
Description: CIDR block for the public subnet
Default: 10.0.0.0/24
Type: String
VpcId:
Description: "(Optional) The VPC to create subnets in. Leave blank to create a new VPC."
Default: ''
Type: String
VpcCIDR:
Description: "CIDR block for the VPC if it will be created. Required if you opt to create a VPC."
Default: 10.0.0.0/16
Type: String
Conditions:
CreateInternetGateway:
Fn::Equals:
- Ref: InternetGatewayId
- ''
CreateVpc:
Fn::Equals:
- Ref: VpcId
- ''
ExistingInternetGateway:
Fn::Not:
- Fn::Equals:
- Ref: InternetGatewayId
- ''
Resources:
Vpc:
Condition: CreateVpc
Type: AWS::EC2::VPC
Properties:
CidrBlock:
Ref: VpcCIDR
EnableDnsHostnames: true
EnableDnsSupport: true
Tags:
- Key: "Name"
Value: !Sub '${AWS::StackName}:Basic-HPC'
DefaultRouteDependsOnPublic:
Condition: CreateInternetGateway
DependsOn: VPCGatewayAttachment
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId:
Fn::If:
- CreateInternetGateway
- Ref: InternetGateway
- Ref: InternetGatewayId
RouteTableId:
Ref: RouteTablePublic
Type: AWS::EC2::Route
DefaultRouteNoDependsOnPublic:
Condition: ExistingInternetGateway
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId:
Fn::If:
- CreateInternetGateway
- Ref: InternetGateway
- Ref: InternetGatewayId
RouteTableId:
Ref: RouteTablePublic
Type: AWS::EC2::Route
InternetGateway:
Condition: CreateInternetGateway
Properties:
Tags:
- Key: Name
Value: !Sub '${AWS::StackName}:InternetGateway'
- Key: Stack
Value:
Ref: AWS::StackId
Type: AWS::EC2::InternetGateway
NatEIPPublic:
Properties:
Domain: vpc
Type: AWS::EC2::EIP
NatGatewayPublic:
Properties:
AllocationId:
Fn::GetAtt:
- NatEIPPublic
- AllocationId
SubnetId:
Ref: Public
Type: AWS::EC2::NatGateway
NatRoutePrivate:
Properties:
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId:
Ref: NatGatewayPublic
RouteTableId:
Ref: RouteTablePrivate
Type: AWS::EC2::Route
Private:
Properties:
AvailabilityZone:
Ref: AvailabilityZone
CidrBlock:
Ref: PrivateCIDR
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Sub '${AWS::StackName}:PrivateSubnetA-${AvailabilityZone}'
- Key: Stack
Value:
Ref: AWS::StackId
VpcId:
Fn::If:
- CreateVpc
- Ref: Vpc
- Ref: VpcId
Type: AWS::EC2::Subnet
Public:
Properties:
AvailabilityZone:
Ref: AvailabilityZone
CidrBlock:
Ref: PublicCIDR
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Sub '${AWS::StackName}:PublicSubnetA-${AvailabilityZone}'
- Key: Stack
Value:
Ref: AWS::StackId
VpcId:
Fn::If:
- CreateVpc
- Ref: Vpc
- Ref: VpcId
Type: AWS::EC2::Subnet
RouteAssociationPrivate:
Properties:
RouteTableId:
Ref: RouteTablePrivate
SubnetId:
Ref: Private
Type: AWS::EC2::SubnetRouteTableAssociation
RouteAssociationPublic:
Properties:
RouteTableId:
Ref: RouteTablePublic
SubnetId:
Ref: Public
Type: AWS::EC2::SubnetRouteTableAssociation
RouteTablePrivate:
Properties:
Tags:
- Key: Name
Value: !Sub '${AWS::StackName}:PrivateRoute'
- Key: Stack
Value:
Ref: AWS::StackId
VpcId:
Fn::If:
- CreateVpc
- Ref: Vpc
- Ref: VpcId
Type: AWS::EC2::RouteTable
RouteTablePublic:
Properties:
Tags:
- Key: Name
Value: !Sub '${AWS::StackName}:PublicRoute'
- Key: Stack
Value:
Ref: AWS::StackId
VpcId:
Fn::If:
- CreateVpc
- Ref: Vpc
- Ref: VpcId
Type: AWS::EC2::RouteTable
VPCGatewayAttachment:
Condition: CreateInternetGateway
Properties:
InternetGatewayId:
Ref: InternetGateway
VpcId:
Fn::If:
- CreateVpc
- Ref: Vpc
- Ref: VpcId
Type: AWS::EC2::VPCGatewayAttachment
Outputs:
VPC:
Value:
Fn::If:
- CreateVpc
- Ref: Vpc
- Ref: VpcId
Description: ID of the VPC
Export:
Name: !Sub ${AWS::StackName}-VPC
DefaultPrivateSubnet:
Description: The ID of a default private subnet
Value: !Ref Private
Export:
Name: !Sub "${AWS::StackName}-DefaultPrivateSubnet"
DefaultPublicSubnet:
Description: The ID of a default public subnet
Value: !Ref Public
Export:
Name: !Sub "${AWS::StackName}-DefaultPublicSubnet"
InternetGatewayId:
Description: The ID of the Internet Gateway
Value:
Fn::If:
- CreateInternetGateway
- Ref: InternetGateway
- Ref: InternetGatewayId
Export:
Name: !Sub "${AWS::StackName}-InternetGateway"