-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVPCforPrivateNAT.yml
280 lines (260 loc) · 6.83 KB
/
VPCforPrivateNAT.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
AWSTemplateFormatVersion: "2010-09-09"
Description: Default Configuration for Private NAT Gateway (VPC, EC2, TGW)
Mappings:
RegionMap:
us-east-1:
Linux2: ami-0be2609ba883822ec
us-east-2:
Linux2: ami-0a0ad6b70e61be944
us-west-1:
Linux2: ami-03130878b60947df3
us-west-2:
Linux2: ami-0a36eb8fadc976275
ap-northeast-1:
Linux2: ami-01748a72bed07727c
ap-northeast-2:
Linux2: ami-0094965d55b3bb1ff
Parameters:
KeyName:
Description: Name of KeyPair
Type: AWS::EC2::KeyPair::KeyName
Resources:
#####
# VPC1 (10.0.0.0/16) (Source)
VPC1:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Sub '10.0.0.0/16'
Tags:
- Key: Name
Value: VPC 1
IGW:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: VPC1 IGW
Attachigw:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref IGW
VpcId: !Ref VPC1
## Source Subnet (10.0.10.0/24) - IGW & Public
SourceSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC1
CidrBlock: !Sub '10.0.10.0/24'
AvailabilityZone: !Select [ 0, !GetAZs ]
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: VPC1 - Source(10.0.10)
SourceRT:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC1
Tags:
- Key: Name
Value: VPC1 - SourceRT
SourceRoutetoIGW:
Type: AWS::EC2::Route
DependsOn: Attachigw
Properties:
RouteTableId: !Ref SourceRT
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref IGW
SourceSubnetRTAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref SourceRT
SubnetId: !Ref SourceSubnet
### Source SG & EC2
SGforSourceEC2:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: allow 22 from any
GroupName: SourceEC2SG
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
Description: SSH for any
VpcId: !Ref VPC1
SourceEC2:
Type: AWS::EC2::Instance
Properties:
ImageId: !FindInMap [ RegionMap, !Ref "AWS::Region", Linux2 ]
KeyName: !Ref KeyName
AvailabilityZone: !GetAtt SourceSubnet.AvailabilityZone
InstanceType: t3.micro
SubnetId: !Ref SourceSubnet
SecurityGroupIds:
- !Ref SGforSourceEC2
Tags:
- Key: Name
Value: SourceEC2(10.0.10)
## NAT Subnet (10.0.20.0/24) - Nothing
NATSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC1
CidrBlock: !Sub '10.0.20.0/24'
AvailabilityZone: !Select [ 2, !GetAZs ]
Tags:
- Key: Name
Value: VPC1 - NAT(10.0.20)
NATRT:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC1
Tags:
- Key: Name
Value: VPC1 - NATRT
NATRoutetoVPC2:
Type: AWS::EC2::Route
DependsOn: TGWAttachVPC1
Properties:
RouteTableId: !Ref NATRT
DestinationCidrBlock: 10.50.0.0/16
TransitGatewayId: !Ref TGW
NATSubnetRTAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref NATRT
SubnetId: !Ref NATSubnet
######
# VPC2 (10.50.0.0/16) (Target)
VPC2:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Sub '10.50.0.0/16'
Tags:
- Key: Name
Value: VPC 2
## Target Subnet (10.50.10.0/24) - Nothing
TargetSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC2
CidrBlock: !Sub '10.50.10.0/24'
AvailabilityZone: !Select [ 0, !GetAZs ]
Tags:
- Key: Name
Value: VPC2 - Target(10.50.10)
TargetRT:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC2
Tags:
- Key: Name
Value: VPC2 - TargetRT
TargetRoutetoVPC1:
Type: AWS::EC2::Route
DependsOn: TGWAttachVPC2
Properties:
RouteTableId: !Ref TargetRT
DestinationCidrBlock: 10.0.0.0/16
TransitGatewayId: !Ref TGW
TargetSubnetRTAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref TargetRT
SubnetId: !Ref TargetSubnet
### Target SG & EC2
SGforTargetEC2:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: allow icmp from NAT Subnet
GroupName: TargetEC2SG
SecurityGroupIngress:
- IpProtocol: icmp
FromPort: -1
ToPort: -1
CidrIp: 10.0.20.0/24
Description: NAT Subnet
VpcId: !Ref VPC2
TargetEC2:
Type: AWS::EC2::Instance
Properties:
ImageId: !FindInMap [ RegionMap, !Ref "AWS::Region", Linux2 ]
KeyName: !Ref KeyName
AvailabilityZone: !GetAtt TargetSubnet.AvailabilityZone
InstanceType: t3.micro
SubnetId: !Ref TargetSubnet
SecurityGroupIds:
- !Ref SGforTargetEC2
Tags:
- Key: Name
Value: TargetEC2(10.50.10)
#####
# TGW
TGW:
Type: AWS::EC2::TransitGateway
Properties:
Tags:
- Key: Name
Value: TGW
TGWAttachVPC1:
Type: AWS::EC2::TransitGatewayAttachment
Properties:
TransitGatewayId: !Ref TGW
VpcId: !Ref VPC1
SubnetIds:
- !Ref NATSubnet
Tags:
- Key: Name
Value: VPC1 (10.0.10.0/24)
TGWAttachVPC2:
Type: AWS::EC2::TransitGatewayAttachment
Properties:
TransitGatewayId: !Ref TGW
VpcId: !Ref VPC2
SubnetIds:
- !Ref TargetSubnet
Tags:
- Key: Name
Value: VPC2 - Target Subnet (10.50.10.0/24)
#####
# For CloudWatch Logs Insight Query
LogsGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: VPC2
RetentionInDays: 1
FlowLogRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: FlowLogRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- vpc-flow-logs.amazonaws.com
Action:
- 'sts:AssumeRole'
Policies:
- PolicyName: FlowLogPolicy
PolicyDocument:
Statement:
- Effect: Allow
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:DescribeLogGroups'
- 'logs:DescribeLogStreams'
- 'logs:PutLogEvents'
Resource: '*'
FlowLogsInVPC2:
Type: AWS::EC2::FlowLog
Properties:
ResourceType: VPC
ResourceId: !Ref VPC2
DeliverLogsPermissionArn: !GetAtt FlowLogRole.Arn
LogGroupName: !Ref LogsGroup
TrafficType: ALL
MaxAggregationInterval: 60
LogFormat: '${version} ${account-id} ${interface-id} ${srcaddr} ${dstaddr} ${srcport} ${dstport} ${protocol} ${packets} ${bytes} ${start} ${end} ${action} ${log-status}'