-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVPC-NAT-Instance.yaml
771 lines (728 loc) · 23.4 KB
/
VPC-NAT-Instance.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
Description: Deploys a VPC with 2 AZs, and custom NAT instances.
Parameters:
Prefix:
Description: Something to prefix all the resources with
Type: String
Default: Dev
CostCenter:
Description: Tag for cost tracking. Leave blank to ignore
Type: String
Default: ""
MaxBandwidth:
Description: Maximum bandwidth each NAT instance will limit traffic to in Mbps. Defaults to 100Mbps
Type: Number
Default: 100
GatewayCount:
Description: Number of NAT Gateways to deploy
Type: Number
Default: 2
AllowedValues: [ 1, 2 ]
InstanceSize:
Description: How big to make the NAT Instance(s)
Type: String
Default: nano
AllowedValues:
- nano
- micro
- small
- medium
- large
- 2xlarge
EnableSSMEndpoint:
Description: Should the SSM Endpoint be enabled (costs about $20/mo in us-east-1).
Type: String
Default: "no"
AllowedValues:
- "yes"
- "no"
VpcCIDR:
Description: Please enter the IP range (CIDR notation) for this VPC
Type: String
Default: 10.0.0.0/16
CIDRPublic1:
Description: Please enter the IP range (CIDR notation) for the public subnet in the first Availability Zone
Type: String
Default: 10.0.0.0/24
CIDRPublic2:
Description: Please enter the IP range (CIDR notation) for the public subnet in the second Availability Zone
Type: String
Default: 10.0.1.0/24
CIDRPrivate1:
Description: Please enter the IP range (CIDR notation) for the private subnet in the first Availability Zone
Type: String
Default: 10.0.128.0/24
CIDRPrivate2:
Description: Please enter the IP range (CIDR notation) for the private subnet in the second Availability Zone
Type: String
Default: 10.0.129.0/24
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: General
Parameters:
- Prefix
- CostCenter
- MaxBandwidth
- GatewayCount
- InstanceSize
- EnableSSMEndpoint
- Label:
default: Networking
Parameters:
- VpcCIDR
- CIDRPublic1
- CIDRPublic2
- CIDRPrivate1
- CIDRPrivate2
Conditions:
HaveCostCenterTag: !Not
- !Equals
- !Ref CostCenter
- ""
HasTwoNAT: !Equals [ !Ref GatewayCount, 2 ]
HasSSMEndpoint: !Equals [ !Ref EnableSSMEndpoint, "yes" ]
Mappings:
AWSAMIRegionMap:
ap-northeast-1:
AMI: ami-0d874cf52d89e1e4b
type: t4g
ap-northeast-2:
AMI: ami-06a997e23f0199d45
type: t4g
ap-northeast-3:
AMI: ami-063f458e2f70f8951
type: t4g
ap-south-1:
AMI: ami-0494f7473bf694265
type: t4g
ap-southeast-1:
AMI: ami-0b404894f3a1e6da0
type: t4g
ap-southeast-2:
AMI: ami-0c1c73154cfdcc852
type: t4g
ca-central-1:
AMI: ami-045efbe545362bcfa
type: t4g
eu-central-1:
AMI: ami-08b91e717872d4caf
type: t4g
eu-north-1:
AMI: ami-06c417cf0d6b13d9c
type: t4g
eu-west-1:
AMI: ami-06055ab1b3dc70f03
type: t4g
eu-west-2:
AMI: ami-08a5f51a19810bf69
type: t4g
eu-west-3:
AMI: ami-0958b74cb185c1beb
type: t4g
sa-east-1:
AMI: ami-04e03458901339bf1
type: t4g
us-east-1:
AMI: ami-090230ed0c6b13c74
type: t4g
us-east-2:
AMI: ami-00b9738bf35b1d1de
type: t4g
us-west-1:
AMI: ami-0e48a188782d0412a
type: t4g
us-west-2:
AMI: ami-0d3127dab514c6a1a
type: t4g
Resources:
# /----------------------------------------------/
# / The VPC and Network
# /----------------------------------------------/
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCIDR
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: !Sub ${Prefix}-VPC
- !If
- HaveCostCenterTag
- {Key: CostCenter, Value: !Ref CostCenter}
- !Ref AWS::NoValue
Gateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: !Sub ${Prefix}-Gateway
- !If
- HaveCostCenterTag
- {Key: CostCenter, Value: !Ref CostCenter}
- !Ref AWS::NoValue
InternetGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref Gateway
VpcId: !Ref VPC
PublicSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
MapPublicIpOnLaunch: true
AvailabilityZone: !Select [ 0, !GetAZs '' ]
CidrBlock: !Ref CIDRPublic1
Tags:
- Key: Name
Value: !Sub ${Prefix} Public Subnet 1
PublicSubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
MapPublicIpOnLaunch: true
AvailabilityZone: !Select [ 1, !GetAZs '' ]
CidrBlock: !Ref CIDRPublic2
Tags:
- Key: Name
Value: !Sub ${Prefix} Public Subnet 2
PrivateSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [ 0, !GetAZs '' ]
CidrBlock: !Ref CIDRPrivate1
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Sub ${Prefix} Private Subnet 1
PrivateSubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [ 1, !GetAZs '' ]
CidrBlock: !Ref CIDRPrivate2
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Sub ${Prefix} Private Subnet 2
# /----------------------------------------------/
# / Endpoints
# /----------------------------------------------/
EndpointSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Sub ${Prefix}-Enpoint-SG
VpcId: !Ref VPC
GroupDescription: Enable HTTPS from VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: !Ref VpcCIDR
SecurityGroupEgress:
- IpProtocol: -1
CidrIp: !Ref VpcCIDR
S3Endpoint:
Type: "AWS::EC2::VPCEndpoint"
Properties:
# SecurityGroupIds:
# - !Ref EndpointSecurityGroup
RouteTableIds:
- !Ref PublicRouteTable
- !Ref PrivateRouteTable1
ServiceName: !Join
- ''
- - com.amazonaws.
- !Ref 'AWS::Region'
- .s3
VpcId: !Ref VPC
SSMEndpoint:
Type: "AWS::EC2::VPCEndpoint"
Condition: HasSSMEndpoint
Properties:
PrivateDnsEnabled: true
VpcEndpointType: Interface
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
SecurityGroupIds:
- !Ref EndpointSecurityGroup
ServiceName: !Join
- ''
- - com.amazonaws.
- !Ref 'AWS::Region'
- .ssm
VpcId: !Ref VPC
Ec2MessagesEndpoint:
Type: "AWS::EC2::VPCEndpoint"
Condition: HasSSMEndpoint
Properties:
PrivateDnsEnabled: true
VpcEndpointType: Interface
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
SecurityGroupIds:
- !Ref EndpointSecurityGroup
ServiceName: !Join
- ''
- - com.amazonaws.
- !Ref 'AWS::Region'
- .ec2messages
VpcId: !Ref VPC
SSMMessagesEndpoint:
Type: "AWS::EC2::VPCEndpoint"
Condition: HasSSMEndpoint
Properties:
PrivateDnsEnabled: true
VpcEndpointType: Interface
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
SecurityGroupIds:
- !Ref EndpointSecurityGroup
ServiceName: !Join
- ''
- - com.amazonaws.
- !Ref 'AWS::Region'
- .ssmmessages
VpcId: !Ref VPC
# /----------------------------------------------/
# / NACLs
# /----------------------------------------------/
NACL:
Type: AWS::EC2::NetworkAcl
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub ${Prefix}-NACL
# Outbound
AllowInternal:
Type: AWS::EC2::NetworkAclEntry
Properties:
NetworkAclId:
Ref: NACL
RuleNumber: 1
Protocol: -1
Egress: true
RuleAction: allow
CidrBlock: !Ref VpcCIDR
AllowHTTP:
Type: AWS::EC2::NetworkAclEntry
Properties:
NetworkAclId:
Ref: NACL
RuleNumber: 2
Protocol: 6
Egress: true
RuleAction: allow
CidrBlock: 0.0.0.0/0
PortRange:
From: 80
To: 80
AllowHTTPS:
Type: AWS::EC2::NetworkAclEntry
Properties:
NetworkAclId:
Ref: NACL
RuleNumber: 3
Protocol: 6
Egress: true
RuleAction: allow
CidrBlock: 0.0.0.0/0
PortRange:
From: 443
To: 443
AllowICMP:
Type: AWS::EC2::NetworkAclEntry
Properties:
NetworkAclId:
Ref: NACL
RuleNumber: 4
Protocol: 1
Icmp:
Code: -1
Type: -1
Egress: true
RuleAction: allow
CidrBlock: 0.0.0.0/0
AllowNTP:
Type: AWS::EC2::NetworkAclEntry
Properties:
NetworkAclId:
Ref: NACL
RuleNumber: 5
Protocol: 17
PortRange:
From: 123
To: 123
Egress: true
RuleAction: allow
CidrBlock: 0.0.0.0/0
DefaultOutbound:
Type: AWS::EC2::NetworkAclEntry
Properties:
NetworkAclId:
Ref: NACL
RuleNumber: 99
Protocol: -1
Egress: true
RuleAction: deny
CidrBlock: 0.0.0.0/0
DefaultInbound:
# Your SGs should be handling this, right?
Type: AWS::EC2::NetworkAclEntry
Properties:
NetworkAclId:
Ref: NACL
RuleNumber: 100
Protocol: -1
Egress: false
RuleAction: allow
CidrBlock: 0.0.0.0/0
NACLPublic1:
Type: AWS::EC2::SubnetNetworkAclAssociation
Properties:
NetworkAclId: !Ref NACL
SubnetId: !Ref PublicSubnet1
NACLPublic2:
Type: AWS::EC2::SubnetNetworkAclAssociation
Properties:
NetworkAclId: !Ref NACL
SubnetId: !Ref PublicSubnet2
# /----------------------------------------------/
# / The NAT Gateways
# /----------------------------------------------/
NatEip1:
Type: AWS::EC2::EIP
DependsOn: InternetGatewayAttachment
Properties:
Domain: vpc
InstanceId: !Ref Nat1
Tags:
- Key: Name
Value: !Sub ${Prefix}-NAT-EIP-1
- !If
- HaveCostCenterTag
- {Key: CostCenter, Value: !Ref CostCenter}
- !Ref AWS::NoValue
NatEip2:
Type: AWS::EC2::EIP
Condition: HasTwoNAT
DependsOn: InternetGatewayAttachment
Properties:
Domain: vpc
InstanceId: !Ref Nat2
Tags:
- Key: Name
Value: !Sub ${Prefix}-NAT-EIP-2
- !If
- HaveCostCenterTag
- {Key: CostCenter, Value: !Ref CostCenter}
- !Ref AWS::NoValue
NatRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM
NatProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles: [!Ref 'NatRole']
Nat1:
Type: AWS::EC2::Instance
Properties:
IamInstanceProfile: !Ref 'NatProfile'
ImageId: !FindInMap
- AWSAMIRegionMap
- !Ref 'AWS::Region'
- AMI
InstanceType: !Sub
- "${type}.${InstanceSize}"
- type: !FindInMap
- AWSAMIRegionMap
- !Ref 'AWS::Region'
- type
InstanceSize: !Ref InstanceSize
SecurityGroupIds: [!Ref 'NatSecurityGroup']
SubnetId: !Ref PublicSubnet1
SourceDestCheck: false
Tags:
- Key: Name
Value: !Sub ${Prefix}-Nat-1
- !If
- HaveCostCenterTag
- {Key: CostCenter, Value: !Ref CostCenter}
- !Ref AWS::NoValue
UserData:
Fn::Base64: !Sub |
#cloud-config
package_update: true
package_upgrade: true
write_files:
- owner: root:root
path: /usr/local/bin/trafficShape.sh
permissions: "0755"
content: |
#!/bin/bash
INTERFACE=eth0
LIMIT=$(printf "%.0fkbit" "$((10**3 * ${MaxBandwidth}))")
LIMIT20=$(printf "%.0fkbit" "$((10**6 * ${MaxBandwidth} * 2))e-4")
LIMIT40=$(printf "%.0fkbit" "$((10**6 * ${MaxBandwidth} * 4))e-4")
LIMIT80=$(printf "%.0fkbit" "$((10**6 * ${MaxBandwidth} * 8))e-4")
tc qdisc add dev $INTERFACE root handle 1: htb default 12
tc class add dev $INTERFACE parent 1: classid 1:1 htb rate $LIMIT ceil $LIMIT burst 10k
tc class add dev $INTERFACE parent 1:1 classid 1:10 htb rate $LIMIT20 ceil $LIMIT40 prio 1 burst 10k
tc class add dev $INTERFACE parent 1:1 classid 1:12 htb rate $LIMIT80 ceil $LIMIT prio 2
tc filter add dev $INTERFACE protocol ip parent 1:0 prio 1 u32 match ip protocol 0x11 0xff flowid 1:10
tc qdisc add dev $INTERFACE parent 1:10 handle 20: sfq perturb 10
tc qdisc add dev $INTERFACE parent 1:12 handle 30: sfq perturb 10
- owner: root:root
path: /etc/sysctl.d/85-ip-forward.conf
permissions: "0644"
content: |
net.ipv4.ip_forward = 1
- owner: root:root
path: /etc/systemd/system/traffic_shaping.service
permissions: "0644"
content: |
[Unit]
Description=Enable traffic shaping queueing
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/trafficShape.sh
packages:
- iptables-services
- tc
runcmd:
- |
sysctl -w net.ipv4.ip_forward=1
INTERFACE=eth0
iptables -t nat -A POSTROUTING -s ${VpcCIDR} -o $INTERFACE -j MASQUERADE
/usr/local/bin/trafficShape.sh
# Set high-priority class and relevant protocols which uses it
iptables -t mangle -A POSTROUTING -o $INTERFACE -p tcp -m tos --tos Minimize-Delay -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $INTERFACE -p icmp -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $INTERFACE -p tcp --sport 53 -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $INTERFACE -p tcp --dport 53 -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $INTERFACE -p tcp --sport 123 -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $INTERFACE -p tcp --dport 123 -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $INTERFACE -p tcp --sport 22 -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $INTERFACE -p tcp --dport 22 -j CLASSIFY --set-class 1:10
# a couple more tables to tag short, TCP signalling packets correctly
iptables -t mangle -N ack
iptables -t mangle -A ack -m tos ! --tos Normal-Service -j RETURN
iptables -t mangle -A ack -p tcp -m length --length 0:128 -j TOS --set-tos Minimize-Delay
iptables -t mangle -A ack -p tcp -m length --length 128: -j TOS --set-tos Maximize-Throughput
iptables -t mangle -A ack -j RETURN
iptables -t mangle -A POSTROUTING -p tcp -m tcp --tcp-flags SYN,RST,ACK ACK -j ack
iptables -t mangle -N tosfix
iptables -t mangle -A tosfix -p tcp -m length --length 0:512 -j RETURN
iptables -t mangle -A tosfix -j TOS --set-tos Maximize-Throughput
iptables -t mangle -A tosfix -j RETURN
service iptables save
systemctl daemon-reload
systemctl enable iptables.service
systemctl enable traffic_shaping.service
Nat2:
Type: AWS::EC2::Instance
Condition: HasTwoNAT
Properties:
IamInstanceProfile: !Ref 'NatProfile'
ImageId: !FindInMap
- AWSAMIRegionMap
- !Ref 'AWS::Region'
- AMI
InstanceType: !Sub
- "${type}.${InstanceSize}"
- type: !FindInMap
- AWSAMIRegionMap
- !Ref 'AWS::Region'
- type
InstanceSize: !Ref InstanceSize
SecurityGroupIds: [!Ref 'NatSecurityGroup']
SubnetId: !Ref PublicSubnet2
SourceDestCheck: false
Tags:
- Key: Name
Value: !Sub ${Prefix}-Nat-2
- !If
- HaveCostCenterTag
- {Key: CostCenter, Value: !Ref CostCenter}
- !Ref AWS::NoValue
UserData:
Fn::Base64: !Sub |
#cloud-config
package_update: true
package_upgrade: true
write_files:
- owner: root:root
path: /usr/local/bin/trafficShape.sh
permissions: "0755"
content: |
#!/bin/bash
INTERFACE=eth0
LIMIT=$(printf "%.0fkbit" "$((10**3 * ${MaxBandwidth}))")
LIMIT20=$(printf "%.0fkbit" "$((10**6 * ${MaxBandwidth} * 2))e-4")
LIMIT40=$(printf "%.0fkbit" "$((10**6 * ${MaxBandwidth} * 4))e-4")
LIMIT80=$(printf "%.0fkbit" "$((10**6 * ${MaxBandwidth} * 8))e-4")
tc qdisc add dev $INTERFACE root handle 1: htb default 12
tc class add dev $INTERFACE parent 1: classid 1:1 htb rate $LIMIT ceil $LIMIT burst 10k
tc class add dev $INTERFACE parent 1:1 classid 1:10 htb rate $LIMIT20 ceil $LIMIT40 prio 1 burst 10k
tc class add dev $INTERFACE parent 1:1 classid 1:12 htb rate $LIMIT80 ceil $LIMIT prio 2
tc filter add dev $INTERFACE protocol ip parent 1:0 prio 1 u32 match ip protocol 0x11 0xff flowid 1:10
tc qdisc add dev $INTERFACE parent 1:10 handle 20: sfq perturb 10
tc qdisc add dev $INTERFACE parent 1:12 handle 30: sfq perturb 10
- owner: root:root
path: /etc/sysctl.d/85-ip-forward.conf
permissions: "0644"
content: |
net.ipv4.ip_forward = 1
- owner: root:root
path: /etc/systemd/system/traffic_shaping.service
permissions: "0644"
content: |
[Unit]
Description=Enable traffic shaping queueing
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/trafficShape.sh
packages:
- iptables-services
- tc
runcmd:
- |
sysctl -w net.ipv4.ip_forward=1
INTERFACE=eth0
iptables -t nat -A POSTROUTING -s ${VpcCIDR} -o $INTERFACE -j MASQUERADE
/usr/local/bin/trafficShape.sh
# Set high-priority class and relevant protocols which uses it
iptables -t mangle -A POSTROUTING -o $INTERFACE -p tcp -m tos --tos Minimize-Delay -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $INTERFACE -p icmp -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $INTERFACE -p tcp --sport 53 -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $INTERFACE -p tcp --dport 53 -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $INTERFACE -p tcp --sport 123 -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $INTERFACE -p tcp --dport 123 -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $INTERFACE -p tcp --sport 22 -j CLASSIFY --set-class 1:10
iptables -t mangle -A POSTROUTING -o $INTERFACE -p tcp --dport 22 -j CLASSIFY --set-class 1:10
# a couple more tables to tag short, TCP signalling packets correctly
iptables -t mangle -N ack
iptables -t mangle -A ack -m tos ! --tos Normal-Service -j RETURN
iptables -t mangle -A ack -p tcp -m length --length 0:128 -j TOS --set-tos Minimize-Delay
iptables -t mangle -A ack -p tcp -m length --length 128: -j TOS --set-tos Maximize-Throughput
iptables -t mangle -A ack -j RETURN
iptables -t mangle -A POSTROUTING -p tcp -m tcp --tcp-flags SYN,RST,ACK ACK -j ack
iptables -t mangle -N tosfix
iptables -t mangle -A tosfix -p tcp -m length --length 0:512 -j RETURN
iptables -t mangle -A tosfix -j TOS --set-tos Maximize-Throughput
iptables -t mangle -A tosfix -j RETURN
service iptables save
systemctl daemon-reload
systemctl enable iptables.service
systemctl enable traffic_shaping.service
NatSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: SG for Nat host
VpcId: !Ref 'VPC'
SecurityGroupEgress:
- Description: Allow ALL
CidrIp: 0.0.0.0/0
IpProtocol: -1
SecurityGroupIngress:
- Description: Allow all from local
IpProtocol: -1
CidrIp: !Ref VpcCIDR
PublicRouteTable:
Type: AWS::EC2::RouteTable
DependsOn: InternetGatewayAttachment
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub ${Prefix} Default Route Table (Public)
PublicRouteTableDefaultRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref Gateway
PublicSubnet1RouteAssoc:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet1
PublicSubnet2RouteAssoc:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet2
PrivateRouteTable1:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub ${Prefix} Private Routes (AZ1)
PrivateRouteTable1Default:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateRouteTable1
DestinationCidrBlock: 0.0.0.0/0
InstanceId: !Ref Nat1
PrivateRouteTable2:
Type: AWS::EC2::RouteTable
Condition: HasTwoNAT
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub ${Prefix} Private Routes 2
PrivateRouteTable2Default:
Type: AWS::EC2::Route
Condition: HasTwoNAT
Properties:
RouteTableId: !Ref PrivateRouteTable2
DestinationCidrBlock: 0.0.0.0/0
InstanceId: !Ref Nat2
PrivateSubnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateRouteTable1
SubnetId: !Ref PrivateSubnet1
PrivateSubnet2RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !If [HasTwoNAT, !Ref PrivateRouteTable2, !Ref PrivateRouteTable1]
SubnetId: !Ref PrivateSubnet2
Outputs:
VPC:
Description: A reference to the created VPC
Value: !Ref VPC
Nat1:
Description: ID of AZ1's Nat Instance
Value: !Ref Nat1
Nat1PublicIp:
Description: AZ1's Nat Public IP
Value: !Ref NatEip1
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 ]]