/
egress-vpc.yaml
766 lines (732 loc) · 23.6 KB
/
egress-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
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
AWSTemplateFormatVersion: '2010-09-09'
Description: The template creates TGW & an Egress VPC that hosts squid servers for internet. Author shkahma@
Parameters:
CidrBlock:
Default: '100.64.0.0/16'
Description: The CIDR Block for the VPC
Type: String
AZCount:
Type: Number
Description: "# of AZs. Max - 2"
MinValue: 1
MaxValue: 2
Default: 2
AmazonLinuxAMI:
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
Description: DO NOT CHANGE - AMI Used for Squid Proxy.
Conditions:
TwoAZs: !Equals [ !Ref AZCount, 2 ]
Resources:
TransitGateway:
Type: "AWS::EC2::TransitGateway"
Properties:
AmazonSideAsn: 64513
AutoAcceptSharedAttachments: enable
DefaultRouteTableAssociation: "disable"
DefaultRouteTablePropagation: "disable"
Description: A transit gateway to support a centralized FW server
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref CidrBlock
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
- Key: Name
Value: !Sub EgressVpc-${AWS::StackName}
PublicEgressVpcSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref 'VPC'
CidrBlock: !Sub
- ${a}.${b}.0.0/24
- a: !Select [0, !Split ['.', !Select [0, !Split [ "/" , !Ref CidrBlock ]]]]
b: !Select [1, !Split ['.', !Select [0, !Split [ "/" , !Ref CidrBlock ]]]]
AvailabilityZone:
Fn::Select:
- 0
- Fn::GetAZs: ""
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
- Key: Name
Value: !Sub Public-EgressVpc-${AWS::StackName}
PublicEgressVpcSubnet2:
Type: AWS::EC2::Subnet
Condition: TwoAZs
Properties:
VpcId: !Ref 'VPC'
CidrBlock: !Sub
- ${a}.${b}.1.0/24
- a: !Select [0, !Split ['.', !Select [0, !Split [ "/" , !Ref CidrBlock ]]]]
b: !Select [1, !Split ['.', !Select [0, !Split [ "/" , !Ref CidrBlock ]]]]
AvailabilityZone:
Fn::Select:
- 1
- Fn::GetAZs: ""
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
- Key: Name
Value: !Sub Public-2-EgressVpc-${AWS::StackName}
PrivateEgressVpcSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref 'VPC'
CidrBlock: !Sub
- ${a}.${b}.2.0/24
- a: !Select [0, !Split ['.', !Select [0, !Split [ "/" , !Ref CidrBlock ]]]]
b: !Select [1, !Split ['.', !Select [0, !Split [ "/" , !Ref CidrBlock ]]]]
AvailabilityZone:
Fn::Select:
- 0
- Fn::GetAZs: ""
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
- Key: Name
Value: !Sub Private-EgressVpc-${AWS::StackName}
PrivateEgressVpcSubnet2:
Type: AWS::EC2::Subnet
Condition: TwoAZs
Properties:
VpcId: !Ref 'VPC'
CidrBlock: !Sub
- ${a}.${b}.3.0/24
- a: !Select [0, !Split ['.', !Select [0, !Split [ "/" , !Ref CidrBlock ]]]]
b: !Select [1, !Split ['.', !Select [0, !Split [ "/" , !Ref CidrBlock ]]]]
AvailabilityZone:
Fn::Select:
- 1
- Fn::GetAZs: ""
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
- Key: Name
Value: !Sub Private-2-EgressVpc-${AWS::StackName}
TGWAttchSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref 'VPC'
CidrBlock: !Sub
- ${a}.${b}.4.0/24
- a: !Select [0, !Split ['.', !Select [0, !Split [ "/" , !Ref CidrBlock ]]]]
b: !Select [1, !Split ['.', !Select [0, !Split [ "/" , !Ref CidrBlock ]]]]
AvailabilityZone:
Fn::Select:
- 0
- Fn::GetAZs: ""
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
- Key: Name
Value: !Sub TGWAttch-EgressVpc1-${AWS::StackName}
TGWAttchSubnet2:
Type: AWS::EC2::Subnet
Condition: TwoAZs
Properties:
VpcId: !Ref 'VPC'
CidrBlock: !Sub
- ${a}.${b}.5.0/24
- a: !Select [0, !Split ['.', !Select [0, !Split [ "/" , !Ref CidrBlock ]]]]
b: !Select [1, !Split ['.', !Select [0, !Split [ "/" , !Ref CidrBlock ]]]]
AvailabilityZone:
Fn::Select:
- 1
- Fn::GetAZs: ""
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
- Key: Name
Value: !Sub TGWAttch-EgressVpc2-${AWS::StackName}
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
AttachGateway:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref 'VPC'
InternetGatewayId: !Ref 'InternetGateway'
RouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref 'VPC'
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
- Key: Name
Value: !Sub Public-EgressVpc-${AWS::StackName}
PrivateRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref 'VPC'
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
- Key: Name
Value: !Sub Private-EgressVpc-${AWS::StackName}
PrivateRouteTable2:
Type: AWS::EC2::RouteTable
Condition: TwoAZs
Properties:
VpcId: !Ref 'VPC'
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
- Key: Name
Value: !Sub Private-2-EgressVpc-${AWS::StackName}
TGWAttchRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref 'VPC'
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
- Key: Name
Value: !Sub TGWAttch-EgressVpc-${AWS::StackName}
TGWAttchRouteTable2:
Type: AWS::EC2::RouteTable
Condition: TwoAZs
Properties:
VpcId: !Ref 'VPC'
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
- Key: Name
Value: !Sub TGWAttch2-EgressVpc-${AWS::StackName}
FWENI:
Type: AWS::EC2::NetworkInterface
Properties:
Description: String
GroupSet:
- !Ref 'FWSecurityGroup'
SourceDestCheck: false
SubnetId: !Ref TGWAttchSubnet
FWENI2:
Type: AWS::EC2::NetworkInterface
Condition: TwoAZs
Properties:
Description: String
GroupSet:
- !Ref 'FWSecurityGroup'
SourceDestCheck: false
SubnetId: !Ref TGWAttchSubnet2
FWSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VPC
GroupDescription: Enable SSH access via port 22
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: '0.0.0.0/0'
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '443'
ToPort: '443'
CidrIp: 0.0.0.0/0
- IpProtocol: icmp
FromPort: -1
ToPort: -1
CidrIp: '0.0.0.0/0'
- IpProtocol: -1
FromPort: -1
ToPort: -1
CidrIp: 100.64.0.0/16
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
- Key: Name
Value: !Sub FWSecurityGroup-${AWS::StackName}
Route:
Type: AWS::EC2::Route
DependsOn: AttachGateway
Properties:
RouteTableId: !Ref 'RouteTable'
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref 'InternetGateway'
SubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref 'PublicEgressVpcSubnet'
RouteTableId: !Ref 'RouteTable'
SubnetRouteTableAssociation2:
Type: AWS::EC2::SubnetRouteTableAssociation
Condition: TwoAZs
Properties:
SubnetId: !Ref 'PublicEgressVpcSubnet2'
RouteTableId: !Ref 'RouteTable'
PrivateRoute:
Type: AWS::EC2::Route
DependsOn: AttachGateway
Properties:
RouteTableId: !Ref 'PrivateRouteTable'
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref 'NATGateway'
PrivateRoute2:
Type: AWS::EC2::Route
Condition: TwoAZs
DependsOn: AttachGateway
Properties:
RouteTableId: !Ref 'PrivateRouteTable2'
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref 'NATGateway2'
TGWAttchRoute:
Type: AWS::EC2::Route
DependsOn: AttachGateway
Properties:
RouteTableId: !Ref 'TGWAttchRouteTable'
DestinationCidrBlock: 0.0.0.0/0
NetworkInterfaceId: !Ref 'FWENI'
TGWAttchRoute2:
Type: AWS::EC2::Route
Condition: TwoAZs
DependsOn: AttachGateway
Properties:
RouteTableId: !Ref 'TGWAttchRouteTable2'
DestinationCidrBlock: 0.0.0.0/0
NetworkInterfaceId: !Ref 'FWENI2'
PrivateSubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref 'PrivateEgressVpcSubnet'
RouteTableId: !Ref 'PrivateRouteTable'
PrivateSubnet2RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Condition: TwoAZs
Properties:
SubnetId: !Ref 'PrivateEgressVpcSubnet2'
RouteTableId: !Ref 'PrivateRouteTable2'
TGWAttchRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref 'TGWAttchSubnet'
RouteTableId: !Ref 'TGWAttchRouteTable'
TGWAttchRouteTable2Association:
Type: AWS::EC2::SubnetRouteTableAssociation
Condition: TwoAZs
Properties:
SubnetId: !Ref 'TGWAttchSubnet2'
RouteTableId: !Ref 'TGWAttchRouteTable2'
NetworkAcl:
Type: AWS::EC2::NetworkAcl
Properties:
VpcId: !Ref 'VPC'
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
InboundHTTPNetworkAclEntry:
Type: AWS::EC2::NetworkAclEntry
Properties:
NetworkAclId: !Ref 'NetworkAcl'
RuleNumber: '100'
Protocol: '-1'
RuleAction: allow
Egress: 'false'
CidrBlock: 0.0.0.0/0
OutboundHTTPNetworkAclEntry:
Type: AWS::EC2::NetworkAclEntry
Properties:
NetworkAclId: !Ref 'NetworkAcl'
RuleNumber: '100'
Protocol: '-1'
RuleAction: allow
Egress: 'true'
CidrBlock: 0.0.0.0/0
PublicEgressVpcSubnetNetworkAclAssociation:
Type: AWS::EC2::SubnetNetworkAclAssociation
Properties:
SubnetId: !Ref 'PublicEgressVpcSubnet'
NetworkAclId: !Ref 'NetworkAcl'
PublicEgressVpcSubnet2NetworkAclAssociation:
Type: AWS::EC2::SubnetNetworkAclAssociation
Condition: TwoAZs
Properties:
SubnetId: !Ref 'PublicEgressVpcSubnet2'
NetworkAclId: !Ref 'NetworkAcl'
PrivateEgressVpcSubnetNetworkAclAssociation:
Type: AWS::EC2::SubnetNetworkAclAssociation
Properties:
SubnetId: !Ref 'PrivateEgressVpcSubnet'
NetworkAclId: !Ref 'NetworkAcl'
PrivateEgressVpcSubnet2NetworkAclAssociation:
Type: AWS::EC2::SubnetNetworkAclAssociation
Condition: TwoAZs
Properties:
SubnetId: !Ref 'PrivateEgressVpcSubnet2'
NetworkAclId: !Ref 'NetworkAcl'
TGWAttchSubnetNetworkAclAssociation:
Type: AWS::EC2::SubnetNetworkAclAssociation
Properties:
SubnetId: !Ref 'TGWAttchSubnet'
NetworkAclId: !Ref 'NetworkAcl'
TGWAttchSubnet2NetworkAclAssociation:
Type: AWS::EC2::SubnetNetworkAclAssociation
Condition: TwoAZs
Properties:
SubnetId: !Ref 'TGWAttchSubnet2'
NetworkAclId: !Ref 'NetworkAcl'
IPAddress:
Type: AWS::EC2::EIP
DependsOn: AttachGateway
Properties:
Domain: vpc
IPAddress2:
Type: AWS::EC2::EIP
Condition: TwoAZs
DependsOn: AttachGateway
Properties:
Domain: vpc
NATGateway:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt IPAddress.AllocationId
SubnetId: !Ref PublicEgressVpcSubnet
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
NATGateway2:
Type: AWS::EC2::NatGateway
Condition: TwoAZs
Properties:
AllocationId: !GetAtt IPAddress2.AllocationId
SubnetId: !Ref PublicEgressVpcSubnet2
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
EgressVpcAttachment:
Type: "AWS::EC2::TransitGatewayAttachment"
Properties:
SubnetIds:
!If
- TwoAZs
-
- !Ref 'TGWAttchSubnet'
- !Ref 'TGWAttchSubnet2'
-
- !Ref 'TGWAttchSubnet'
Tags:
- Key: Name
Value: !Sub EgressVpc-${AWS::StackName}
TransitGatewayId: !Ref TransitGateway
VpcId: !Ref VPC
SecurityTransitGatewayRouteTable:
Type: "AWS::EC2::TransitGatewayRouteTable"
Properties:
Tags:
- Key: Name
Value: !Sub EgressVpc-${AWS::StackName}
TransitGatewayId: !Ref TransitGateway
SpokeTransitGatewayRouteTable:
Type: "AWS::EC2::TransitGatewayRouteTable"
Properties:
Tags:
- Key: Name
Value: !Sub SpokeVpc-${AWS::StackName}
TransitGatewayId: !Ref TransitGateway
SpokeDefaultTGWRoute:
Type: "AWS::EC2::TransitGatewayRoute"
Properties:
DestinationCidrBlock: 0.0.0.0/0
TransitGatewayAttachmentId: !Ref EgressVpcAttachment
TransitGatewayRouteTableId: !Ref SpokeTransitGatewayRouteTable
SecurityTransitGatewayRoutePropagation:
Type: "AWS::EC2::TransitGatewayRouteTablePropagation"
Properties:
TransitGatewayAttachmentId: !Ref EgressVpcAttachment
TransitGatewayRouteTableId: !Ref SecurityTransitGatewayRouteTable
SpokeTransitGatewayRoutePropagation:
Type: "AWS::EC2::TransitGatewayRouteTablePropagation"
Properties:
TransitGatewayAttachmentId: !Ref EgressVpcAttachment
TransitGatewayRouteTableId: !Ref SpokeTransitGatewayRouteTable
EgressVpcTgwAssociation:
Type: "AWS::EC2::TransitGatewayRouteTableAssociation"
Properties:
TransitGatewayAttachmentId: !Ref EgressVpcAttachment
TransitGatewayRouteTableId: !Ref SecurityTransitGatewayRouteTable
#------------------ EC2 Stuff ------
FWRole:
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
FWProfile:
Type: "AWS::IAM::InstanceProfile"
Properties:
Roles:
- !Ref FWRole
FWRolePolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ec2:DetachNetworkInterface
- ec2:DescribeNetworkInterfaces
- ec2:AttachNetworkInterface
Resource:
- '*'
Description: Role for the FW instance
Roles:
- !Ref 'FWRole'
FWASG:
Type: AWS::AutoScaling::AutoScalingGroup
DependsOn:
- PrivateSubnetRouteTableAssociation
Properties:
AutoScalingGroupName: !Sub proxy-ASG1-${AWS::StackName}
HealthCheckType: EC2
LaunchConfigurationName: !Ref FWLC
MaxSize: 1
DesiredCapacity: 1
MinSize: 1
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
PropagateAtLaunch: true
- Key: Name
Value: !Sub proxy-az1-${AWS::StackName}
PropagateAtLaunch: true
VPCZoneIdentifier:
- !Ref PrivateEgressVpcSubnet
FWASG2:
Type: AWS::AutoScaling::AutoScalingGroup
Condition: TwoAZs
DependsOn:
- PrivateSubnetRouteTableAssociation
Properties:
AutoScalingGroupName: !Sub proxy-ASG2-${AWS::StackName}
HealthCheckType: EC2
LaunchConfigurationName: !Ref FWLC
MaxSize: 1
DesiredCapacity: 1
MinSize: 1
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
PropagateAtLaunch: true
- Key: Name
Value: !Sub proxy-az2-${AWS::StackName}
PropagateAtLaunch: true
VPCZoneIdentifier:
- !Ref PrivateEgressVpcSubnet2
FWLC:
Type: AWS::AutoScaling::LaunchConfiguration
Metadata:
Comment: Install a transparent squid proxy
AWS::CloudFormation::Init:
configSets:
Install:
- Install
Install:
files:
/etc/squid/attach-eni.sh:
content: !Sub
- |
#!/bin/bash
NEXT_WAIT_TIME=0
CURR_AZ=$(curl http://169.254.169.254/latest/meta-data/placement/availability-zone)
AZ1="${AZ1}"
AZ2="${AZ2}"
echo "[$AZ1]"
echo "[$AZ2]"
echo "[$CURR_AZ]"
if [ "$CURR_AZ" == "$AZ1" ]; then
FW_ENI="${FWENI}"
echo "In AZ1...."
else
FW_ENI="${FWENI2}"
echo "In AZ2...."
fi
echo "[$CURR_AZ]"
FW_ENI_STATUS=$(aws ec2 describe-network-interfaces --filter Name=network-interface-id,Values=$FW_ENI --region ${AWS::Region} --query NetworkInterfaces[0].Status --output text)
if [ "$FW_ENI_STATUS" == "in-use" ]; then
echo "ENI is in use"
FW_ATTACHMENT_ID=$(aws ec2 describe-network-interfaces --filter Name=network-interface-id,Values=$FW_ENI --region ${AWS::Region} --query NetworkInterfaces[0].Attachment.AttachmentId --output text)
aws ec2 detach-network-interface --attachment-id $FW_ATTACHMENT_ID --region ${AWS::Region}
fi
until [ "$FW_ENI_STATUS" == "available" ] || [ $NEXT_WAIT_TIME -eq 8 ]; do
sleep $(( NEXT_WAIT_TIME++ ))
FW_ENI_STATUS=$(aws ec2 describe-network-interfaces --filter Name=network-interface-id,Values=$FW_ENI --region ${AWS::Region} --query NetworkInterfaces[0].Status --output text)
done
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
aws ec2 attach-network-interface --network-interface-id $FW_ENI --instance-id $INSTANCE_ID --device-index 1 --region ${AWS::Region}
sleep 20
route add -net 10.0.0.0/8 dev eth1
route add -net 172.16.0.0/12 dev eth1
route add -net 192.168.0.0/16 dev eth1
- FWENI: !Ref FWENI
FWENI2: !If
- TwoAZs
- !Ref FWENI2
- !Ref FWENI
AZ1:
Fn::Select:
- 0
- Fn::GetAZs: ""
AZ2:
Fn::Select:
- 1
- Fn::GetAZs: ""
mode: '000755'
owner: root
group: root
/etc/squid/squid.proxy.conf:
content: !Sub |
visible_hostname FW
#Handling HTTP requests
http_port 3129 intercept
#acl allowed_http_sites dstdomain DOMAIN
#Handling HTTPS requests
https_port 3130 cert=/etc/squid/ssl/squid.pem ssl-bump intercept
acl SSL_port port 443
http_access allow SSL_port
#acl allowed_https_sites ssl::server_name DOMAIN
acl step1 at_step SslBump1
acl step2 at_step SslBump2
acl step3 at_step SslBump3
ssl_bump peek step1 all
ssl_bump peek step2 all
ssl_bump splice step3 all
ssl_bump terminate step2 all
http_access allow all
#http_access deny all
mode: '000644'
owner: root
group: root
/etc/cfn/cfn-hup.conf:
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
mode: '000400'
owner: root
group: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.FWLC.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource FWASG --region ${AWS::Region} runas=root
mode: '000400'
owner: root
group: root
services:
sysvinit:
cfn-hup:
enabled: 'true'
ensureRunning: 'true'
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
Properties:
ImageId: !Ref AmazonLinuxAMI
InstanceType: 't2.micro'
IamInstanceProfile: !Ref 'FWProfile'
SecurityGroups:
- !Ref 'FWSecurityGroup'
UserData:
Fn::Base64: !Sub |
#!/bin/bash
yum update -y
yum install -y squid
yum install -y aws-cfn-bootstrap
echo "Yum Install Complete"
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --configsets Install --resource FWLC --region ${AWS::Region}
/etc/squid/attach-eni.sh
mv /etc/squid/squid.conf /etc/squid/squid.orig.conf
cat /etc/squid/squid.proxy.conf
cp /etc/squid/squid.proxy.conf /etc/squid/squid.conf
mkdir /etc/squid/ssl
cd /etc/squid/ssl
openssl genrsa -out squid.key 2048
openssl req -new -key squid.key -out squid.csr -subj "/C=XX/ST=XX/L=squid/O=squid/CN=squid"
openssl x509 -req -days 3650 -in squid.csr -signkey squid.key -out squid.crt
cat squid.key squid.crt | tee squid.pem
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3129
iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 3130
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
ifconfig -a
sleep 5
ifconfig -a
route add -net 10.0.0.0/8 dev eth1
route add -net 172.16.0.0/12 dev eth1
route add -net 192.168.0.0/16 dev eth1
service iptables save
service squid start
# -e $?
/opt/aws/bin/cfn-signal --stack ${AWS::StackName} --resource FWLC --region ${AWS::Region}
CreationPolicy:
ResourceSignal:
Count: 1
Timeout: PT15M
Outputs:
Name:
Value: !Ref AWS::StackName
TransitGateway:
Value: !Ref TransitGateway
Export:
Name: !Sub ${AWS::StackName}-TransitGateway
ImageId:
Value: !Ref AmazonLinuxAMI
Export:
Name: !Sub ${AWS::StackName}-ImageId
TgwRouteTable:
Value: !Ref SpokeTransitGatewayRouteTable
Export:
Name: !Sub ${AWS::StackName}-TgwRouteTable
TgwEgressVpcRouteTable:
Value: !Ref SecurityTransitGatewayRouteTable
Export:
Name: !Sub ${AWS::StackName}-TgwEgressVpcRouteTable
EgressVpc:
Value: !Ref VPC
Description: The VPC ID for the security VPC.
Export:
Name: !Sub ${AWS::StackName}-EgressVpc
PrivateRouteTable:
Value: !Ref PrivateRouteTable
Export:
Name: !Sub ${AWS::StackName}-PrivateRouteTable
TGWAttchRouteTable:
Value: !Ref TGWAttchRouteTable
Export:
Name: !Sub ${AWS::StackName}-TGWAttchRouteTable
PrivateRouteTable2:
Value: !If
- TwoAZs
- !Ref PrivateRouteTable2
- SingleAZ
Export:
Name: !Sub ${AWS::StackName}-PrivateRouteTable2
TGWAttchRouteTable2:
Value: !If
- TwoAZs
- !Ref TGWAttchRouteTable2
- SingleAZ
Export:
Name: !Sub ${AWS::StackName}-TGWAttchRouteTable2