-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpc_template_ready.yaml
197 lines (179 loc) · 5.72 KB
/
vpc_template_ready.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
AWSTemplateFormatVersion: 2010-09-09
Description: This Template Creates a VPC with 2 Public and 2 Private Subnets.
Parameters:
# VPC Parameters
CidrBlock: # CIDR block for my VPC
AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}'
Default: 10.0.0.0/16
Description: VPC CIDR Block (eg 10.0.0.0/16)
Type: String
AvailabilityZone1:
Description: The AvailabilityZone to use for the first subnet
Type: AWS::EC2::AvailabilityZone::Name
AvailabilityZone2:
Description: The AvailabilityZone to use for the second subnet
Type: AWS::EC2::AvailabilityZone::Name
PublicSubnetCIDR1:
AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}'
Default: 10.0.1.0/24
Description: VPC CIDR Block for the Public Subnet (eg 10.0.0.0/24)
Type: String
PublicSubnetCIDR2:
AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}'
Default: 10.0.2.0/24
Description: VPC CIDR Block for the Public Subnet (eg 10.0.0.0/24)
Type: String
PrivateSubnetCIDR1:
AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}'
Default: 10.0.11.0/24
Description: VPC CIDR Block for the Public Subnet (eg 10.0.0.0/24)
Type: String
PrivateSubnetCIDR2:
AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}'
Default: 10.0.12.0/24
Description: VPC CIDR Block for the Public Subnet (eg 10.0.0.0/24)
Type: String
Resources:
PubPrivateVPC: # My VPC with Private and Public Subnets
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock: !Ref CidrBlock
EnableDnsSupport: 'true'
EnableDnsHostnames: 'true'
InstanceTenancy: 'default'
Tags:
- Key: Name
Value:
Ref: AWS::StackName
PublicSubnet1: # Public Subnet 1
Type: 'AWS::EC2::Subnet'
Properties:
VpcId: !Ref PubPrivateVPC
AvailabilityZone: !Select [ '0', !GetAZs ]
CidrBlock: !Ref PublicSubnetCIDR1
MapPublicIpOnLaunch: 'true'
Tags:
- Key: Name
Value: PublicSubnet1
PublicSubnet2: # Public Subnet 2
Type: 'AWS::EC2::Subnet'
Properties:
VpcId: !Ref PubPrivateVPC
AvailabilityZone: !Select [ '1', !GetAZs ]
CidrBlock: !Ref PublicSubnetCIDR2
MapPublicIpOnLaunch: 'true'
Tags:
- Key: Name
Value: PublicSubnet2
PrivateSubnet1: # Private Subnet 1
Type: 'AWS::EC2::Subnet'
Properties:
VpcId: !Ref PubPrivateVPC
AvailabilityZone: !Select [ '0', !GetAZs ]
CidrBlock: !Ref PrivateSubnetCIDR1
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: PrivateSubnet1
PrivateSubnet2: # Private Subnet 2
Type: 'AWS::EC2::Subnet'
Properties:
VpcId: !Ref PubPrivateVPC
AvailabilityZone: !Select [ '1', !GetAZs ]
CidrBlock: !Ref PrivateSubnetCIDR2
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: PrivateSubnet2
InternetGateway: # Internet Gateway to get the Public Internet
Type: 'AWS::EC2::InternetGateway'
Properties:
Tags:
- Key: Name
Value: !Join [_, [!Ref 'AWS::StackName']]
- Key: Network
Value: PublicInternet
GatewayToInternet:
Type: 'AWS::EC2::VPCGatewayAttachment'
Properties:
VpcId: !Ref PubPrivateVPC
InternetGatewayId: !Ref InternetGateway
PublicRouteTable: # Public Route Table is created for a Public Subnets
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref PubPrivateVPC
Tags:
- Key: Network
Value: PublicRT
PublicRoute:
Type: 'AWS::EC2::Route'
DependsOn: GatewayToInternet
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
PublicSubnet1RouteTableAssociation: # Public Subnets needs to be assosiated with a Public RT
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref PublicSubnet1
RouteTableId: !Ref PublicRouteTable
PublicSubnet2RouteTableAssociation:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref PublicSubnet2
RouteTableId: !Ref PublicRouteTable
NatGateway: # NAT Gateway is for bringing Internet for Private Subnets
Type: "AWS::EC2::NatGateway"
DependsOn: NatPublicIP
Properties:
AllocationId: !GetAtt NatPublicIP.AllocationId
SubnetId: !Ref PublicSubnet1
NatPublicIP: # Elastic IP is a Static IP adress for NAT Gateway
Type: "AWS::EC2::EIP"
DependsOn: PubPrivateVPC
Properties:
Domain: vpc
PrivateRouteTable: # Private Route Table is created for Private Subnets
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref PubPrivateVPC
Tags:
- Key: Network
Value: PrivateRT
PrivateRoute:
Type: 'AWS::EC2::Route'
Properties:
RouteTableId: !Ref PrivateRouteTable
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGateway
PrivateSubnet1RouteTableAssociation: # Private Subnets needs to be assosiated with a Private RT
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref PrivateSubnet1
RouteTableId: !Ref PrivateRouteTable
PrivateSubnet2RouteTableAssociation:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref PrivateSubnet2
RouteTableId: !Ref PrivateRouteTable
Outputs:
MyVPC:
Description: " MyVPC with 2 Private and 2 public Subnets"
Value:
!Ref PubPrivateVPC
PrivateSubnet1:
Description: " Private Subnet AZ1"
Value:
!Ref PrivateSubnet1
PrivateSubnet2:
Description: " Private Subnet AZ2"
Value:
!Ref PrivateSubnet2
PubicSubnet1:
Description: " Public Subnet AZ1"
Value:
!Ref PublicSubnet1
PubicSubnet2:
Description: " Public Subnet AZ2"
Value:
!Ref PublicSubnet2