-
Notifications
You must be signed in to change notification settings - Fork 4
/
centralised_egress_proxy.yaml
1307 lines (1216 loc) · 36.4 KB
/
centralised_egress_proxy.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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
AWSTemplateFormatVersion: 2010-09-09
Parameters:
BranchName:
Description: CodeCommit branch name
Type: String
Default: main
RepositoryName:
Description: CodeCommit repository name
Type: String
TotalAZsInRegion:
Type: Number
Default: 2
AllowedValues:
- 2
- 3
- 4
- 5
- 6
Description: Select Number of AZs in the Region.
AllowedPrincipalsList:
Type: CommaDelimitedList
Default: ''
Conditions:
SixAz: !Equals
- !Ref TotalAZsInRegion
- 6
FiveAz: !Or
- !Equals
- !Ref TotalAZsInRegion
- 5
- !Condition SixAz
FourAz: !Or
- !Equals
- !Ref TotalAZsInRegion
- 4
- !Condition FiveAz
ThreeAz: !Or
- !Equals
- !Ref TotalAZsInRegion
- 3
- !Condition FourAz
TwoAz: !Or
- !Equals
- !Ref TotalAZsInRegion
- 2
- !Condition ThreeAz
IsPrincipalProvided:
!Not [!Equals [!Join ["",!Ref AllowedPrincipalsList],'']]
Resources:
#Infra Networking
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/21
Tags:
- Key: Name
Value: !Sub 'VPC - ${AWS::StackName}'
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: !Sub 'IGW - ${AWS::StackName}'
AttachGateway:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId:
Ref: VPC
Tags:
- Key: Name
Value: !Sub 'Public Route Table- ${AWS::StackName}'
PublicRouteTableRoute:
DependsOn:
- AttachGateway
Type: AWS::EC2::Route
Properties:
RouteTableId:
Ref: PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId:
Ref: InternetGateway
#AZ 1
PrivateSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.1.0/24
MapPublicIpOnLaunch: False
AvailabilityZone: !Select
- 0
- !GetAZs
Ref: 'AWS::Region'
Tags:
- Key: Name
Value: !Sub 'Private Subnet 1 - ${AWS::StackName}'
PublicSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.0.0/27
MapPublicIpOnLaunch: False
AvailabilityZone: !Select
- 0
- !GetAZs
Ref: 'AWS::Region'
Tags:
- Key: Name
Value: !Sub 'Public Subnet 1 - ${AWS::StackName}'
PublicRouteTableSubnetAssociation1:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet1
EIP1:
Type: AWS::EC2::EIP
Properties:
Tags:
- Key: Name
Value: !Sub 'EIP 1 - ${AWS::StackName}'
PublicNATGateway1:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt EIP1.AllocationId
SubnetId:
Ref: PublicSubnet1
Tags:
- Key: Name
Value: !Sub 'Public NATGateway 1 - ${AWS::StackName}'
PrivateRouteTable1:
Type: AWS::EC2::RouteTable
Properties:
VpcId:
Ref: VPC
Tags:
- Key: Name
Value: !Sub 'Private RouteTable 1- ${AWS::StackName}'
PrivateRouteTable1Route:
Type: AWS::EC2::Route
Properties:
RouteTableId:
Ref: PrivateRouteTable1
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId:
Ref: PublicNATGateway1
PrivateRouteTableSubnetAssociation1:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateRouteTable1
SubnetId: !Ref PrivateSubnet1
#AZ 2
PrivateSubnet2:
Type: AWS::EC2::Subnet
Condition: TwoAz
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.2.0/24
MapPublicIpOnLaunch: False
AvailabilityZone: !Select
- 1
- !GetAZs
Ref: 'AWS::Region'
Tags:
- Key: Name
Value: !Sub 'Private Subnet 2 - ${AWS::StackName}'
PublicSubnet2:
Type: AWS::EC2::Subnet
Condition: TwoAz
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.0.32/27
MapPublicIpOnLaunch: False
AvailabilityZone: !Select
- 1
- !GetAZs
Ref: 'AWS::Region'
Tags:
- Key: Name
Value: !Sub 'Public Subnet 2 - ${AWS::StackName}'
PublicRouteTableSubnetAssociation2:
Type: AWS::EC2::SubnetRouteTableAssociation
Condition: TwoAz
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet2
EIP2:
Type: AWS::EC2::EIP
Condition: TwoAz
Properties:
Tags:
- Key: Name
Value: !Sub 'EIP 2 - ${AWS::StackName}'
PublicNATGateway2:
Type: AWS::EC2::NatGateway
Condition: TwoAz
Properties:
AllocationId: !GetAtt EIP2.AllocationId
SubnetId:
Ref: PublicSubnet2
Tags:
- Key: Name
Value: !Sub 'Public NATGateway 2 - ${AWS::StackName}'
PrivateRouteTable2:
Type: AWS::EC2::RouteTable
Condition: TwoAz
Properties:
VpcId:
Ref: VPC
Tags:
- Key: Name
Value: !Sub 'Private RouteTable 2- ${AWS::StackName}'
PrivateRouteTable2Route:
Type: AWS::EC2::Route
Condition: TwoAz
Properties:
RouteTableId:
Ref: PrivateRouteTable2
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId:
Ref: PublicNATGateway2
PrivateRouteTableSubnetAssociation2:
Type: AWS::EC2::SubnetRouteTableAssociation
Condition: TwoAz
Properties:
RouteTableId: !Ref PrivateRouteTable2
SubnetId: !Ref PrivateSubnet2
#AZ 3
PrivateSubnet3:
Type: AWS::EC2::Subnet
Condition: ThreeAz
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.3.0/24
MapPublicIpOnLaunch: False
AvailabilityZone: !Select
- 2
- !GetAZs
Ref: 'AWS::Region'
Tags:
- Key: Name
Value: !Sub 'Private Subnet 3 - ${AWS::StackName}'
PublicSubnet3:
Type: AWS::EC2::Subnet
Condition: ThreeAz
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.0.64/27
MapPublicIpOnLaunch: False
AvailabilityZone: !Select
- 2
- !GetAZs
Ref: 'AWS::Region'
Tags:
- Key: Name
Value: !Sub 'Public Subnet 3 - ${AWS::StackName}'
PublicRouteTableSubnetAssociation3:
Type: AWS::EC2::SubnetRouteTableAssociation
Condition: ThreeAz
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet3
EIP3:
Type: AWS::EC2::EIP
Condition: ThreeAz
Properties:
Tags:
- Key: Name
Value: !Sub 'EIP 3 - ${AWS::StackName}'
PublicNATGateway3:
Type: AWS::EC2::NatGateway
Condition: ThreeAz
Properties:
AllocationId: !GetAtt EIP3.AllocationId
SubnetId:
Ref: PublicSubnet3
Tags:
- Key: Name
Value: !Sub 'Public NATGateway 3 - ${AWS::StackName}'
PrivateRouteTable3:
Type: AWS::EC2::RouteTable
Condition: ThreeAz
Properties:
VpcId:
Ref: VPC
Tags:
- Key: Name
Value: !Sub 'Private RouteTable 3- ${AWS::StackName}'
PrivateRouteTable3Route:
Type: AWS::EC2::Route
Condition: ThreeAz
Properties:
RouteTableId:
Ref: PrivateRouteTable3
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId:
Ref: PublicNATGateway3
PrivateRouteTableSubnetAssociation3:
Type: AWS::EC2::SubnetRouteTableAssociation
Condition: ThreeAz
Properties:
RouteTableId: !Ref PrivateRouteTable3
SubnetId: !Ref PrivateSubnet3
#AZ 4
PrivateSubnet4:
Type: AWS::EC2::Subnet
Condition: FourAz
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.4.0/24
MapPublicIpOnLaunch: False
AvailabilityZone: !Select
- 3
- !GetAZs
Ref: 'AWS::Region'
Tags:
- Key: Name
Value: !Sub 'Private Subnet 4 - ${AWS::StackName}'
PublicSubnet4:
Type: AWS::EC2::Subnet
Condition: FourAz
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.0.96/27
MapPublicIpOnLaunch: False
AvailabilityZone: !Select
- 3
- !GetAZs
Ref: 'AWS::Region'
Tags:
- Key: Name
Value: !Sub 'Public Subnet 4 - ${AWS::StackName}'
PublicRouteTableSubnetAssociation4:
Type: AWS::EC2::SubnetRouteTableAssociation
Condition: FourAz
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet4
EIP4:
Type: AWS::EC2::EIP
Condition: FourAz
Properties:
Tags:
- Key: Name
Value: !Sub 'EIP 4 - ${AWS::StackName}'
PublicNATGateway4:
Type: AWS::EC2::NatGateway
Condition: FourAz
Properties:
AllocationId: !GetAtt EIP4.AllocationId
SubnetId:
Ref: PublicSubnet4
Tags:
- Key: Name
Value: !Sub 'Public NATGateway 4 - ${AWS::StackName}'
PrivateRouteTable4:
Type: AWS::EC2::RouteTable
Condition: FourAz
Properties:
VpcId:
Ref: VPC
Tags:
- Key: Name
Value: !Sub 'Private RouteTable 4- ${AWS::StackName}'
PrivateRouteTable4Route:
Type: AWS::EC2::Route
Condition: FourAz
Properties:
RouteTableId:
Ref: PrivateRouteTable4
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId:
Ref: PublicNATGateway4
PrivateRouteTableSubnetAssociation4:
Type: AWS::EC2::SubnetRouteTableAssociation
Condition: FourAz
Properties:
RouteTableId: !Ref PrivateRouteTable4
SubnetId: !Ref PrivateSubnet4
#AZ 5
PrivateSubnet5:
Type: AWS::EC2::Subnet
Condition: FiveAz
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.5.0/24
MapPublicIpOnLaunch: False
AvailabilityZone: !Select
- 4
- !GetAZs
Ref: 'AWS::Region'
Tags:
- Key: Name
Value: !Sub 'Private Subnet 5 - ${AWS::StackName}'
PublicSubnet5:
Type: AWS::EC2::Subnet
Condition: FiveAz
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.0.128/27
MapPublicIpOnLaunch: False
AvailabilityZone: !Select
- 4
- !GetAZs
Ref: 'AWS::Region'
Tags:
- Key: Name
Value: !Sub 'Public Subnet 5 - ${AWS::StackName}'
PublicRouteTableSubnetAssociation5:
Type: AWS::EC2::SubnetRouteTableAssociation
Condition: FiveAz
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet5
EIP5:
Type: AWS::EC2::EIP
Condition: FiveAz
Properties:
Tags:
- Key: Name
Value: !Sub 'EIP 5 - ${AWS::StackName}'
PublicNATGateway5:
Type: AWS::EC2::NatGateway
Condition: FiveAz
Properties:
AllocationId: !GetAtt EIP5.AllocationId
SubnetId:
Ref: PublicSubnet5
Tags:
- Key: Name
Value: !Sub 'Public NATGateway 5 - ${AWS::StackName}'
PrivateRouteTable5:
Type: AWS::EC2::RouteTable
Condition: FiveAz
Properties:
VpcId:
Ref: VPC
Tags:
- Key: Name
Value: !Sub 'Private RouteTable 5- ${AWS::StackName}'
PrivateRouteTable5Route:
Type: AWS::EC2::Route
Condition: FiveAz
Properties:
RouteTableId:
Ref: PrivateRouteTable5
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId:
Ref: PublicNATGateway5
PrivateRouteTableSubnetAssociation5:
Type: AWS::EC2::SubnetRouteTableAssociation
Condition: FiveAz
Properties:
RouteTableId: !Ref PrivateRouteTable5
SubnetId: !Ref PrivateSubnet5
#AZ 6
PrivateSubnet6:
Type: AWS::EC2::Subnet
Condition: SixAz
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.6.0/24
MapPublicIpOnLaunch: False
AvailabilityZone: !Select
- 5
- !GetAZs
Ref: 'AWS::Region'
Tags:
- Key: Name
Value: !Sub 'Private Subnet 6 - ${AWS::StackName}'
PublicSubnet6:
Type: AWS::EC2::Subnet
Condition: SixAz
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.0.160/27
MapPublicIpOnLaunch: False
AvailabilityZone: !Select
- 5
- !GetAZs
Ref: 'AWS::Region'
Tags:
- Key: Name
Value: !Sub 'Public Subnet 6 - ${AWS::StackName}'
PublicRouteTableSubnetAssociation6:
Type: AWS::EC2::SubnetRouteTableAssociation
Condition: SixAz
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet6
EIP6:
Type: AWS::EC2::EIP
Condition: SixAz
Properties:
Tags:
- Key: Name
Value: !Sub 'EIP 6 - ${AWS::StackName}'
PublicNATGateway6:
Type: AWS::EC2::NatGateway
Condition: SixAz
Properties:
AllocationId: !GetAtt EIP6.AllocationId
SubnetId:
Ref: PublicSubnet6
Tags:
- Key: Name
Value: !Sub 'Public NATGateway 6 - ${AWS::StackName}'
PrivateRouteTable6:
Type: AWS::EC2::RouteTable
Condition: SixAz
Properties:
VpcId:
Ref: VPC
Tags:
- Key: Name
Value: !Sub 'Private RouteTable 6- ${AWS::StackName}'
PrivateRouteTable6Route:
Type: AWS::EC2::Route
Condition: SixAz
Properties:
RouteTableId:
Ref: PrivateRouteTable6
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId:
Ref: PublicNATGateway6
PrivateRouteTableSubnetAssociation6:
Type: AWS::EC2::SubnetRouteTableAssociation
Condition: SixAz
Properties:
RouteTableId: !Ref PrivateRouteTable6
SubnetId: !Ref PrivateSubnet6
#Flow Logs
VPCFlowLogGroup:
Type: 'AWS::Logs::LogGroup'
Properties:
RetentionInDays: 365
VPCFlowLogRoleRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: vpc-flow-logs.amazonaws.com
Action: 'sts:AssumeRole'
Policies:
- PolicyName: CW
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: !GetAtt VPCFlowLogGroup.Arn
VPCFlowLog:
Type: AWS::EC2::FlowLog
Properties:
DeliverLogsPermissionArn: !GetAtt VPCFlowLogRoleRole.Arn
LogDestination: !GetAtt VPCFlowLogGroup.Arn
ResourceId: !Ref VPC
ResourceType: VPC
TrafficType: ALL
#SQUID Components
CodeBuildServiceRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: codebuild.amazonaws.com
Action: 'sts:AssumeRole'
Policies:
- PolicyName: CW
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: !Sub 'arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/${AWS::StackName}-*'
- PolicyName: CC
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- codecommit:GitPull
Resource:
- !Sub 'arn:aws:codecommit:${AWS::Region}:${AWS::AccountId}:${RepositoryName}'
- PolicyName: CB
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- codebuild:CreateReportGroup
- codebuild:CreateReport
- Codebuild:UpdateReport
- codebuild:BatchPutTestCases
- codebuild:BatchPutCodeCoverages
Resource:
- !Sub 'arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:report-group/${AWS::StackName}-SquidProxyBuild-*'
- PolicyName: ECRAuth
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- ecr:GetAuthorizationToken
Resource: '*'
- PolicyName: ECR
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- ecr:PutImage
- ecr:InitiateLayerUpload
- ecr:UploadLayerPart
- ecr:CompleteLayerUpload
- ecr:BatchCheckLayerAvailability
Resource:
- !GetAtt ECRRepository.Arn
- PolicyName: PipelineS3
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- s3:PutObject
- s3:GetObject
- s3:GetObjectVersion
- s3:GetBucketAcl
- s3:GetBucketLocation
Resource:
- !Sub '${CodePipelineArtifactStoreBucket.Arn}*'
ECRRepository:
Type: 'AWS::ECR::Repository'
Properties:
EncryptionConfiguration:
EncryptionType: AES256
ImageScanningConfiguration:
ScanOnPush: true
CodeBuildProject:
Type: 'AWS::CodeBuild::Project'
Properties:
Name: !Sub '${AWS::StackName}-SquidProxyBuild'
Artifacts:
Type: NO_ARTIFACTS
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: 'aws/codebuild/standard:5.0'
PrivilegedMode: true
Type: LINUX_CONTAINER
EnvironmentVariables:
- Name: IMAGE_REPO_NAME
Type: PLAINTEXT
Value: !GetAtt ECRRepository.RepositoryUri
- Name: IMAGE_URI
Type: PLAINTEXT
Value: !Sub '${ECRRepository.RepositoryUri}:latest'
- Name: ECR_REPOSITORY
Type: PLAINTEXT
Value: !Sub "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com"
- Name: IMAGE_TAG
Type: PLAINTEXT
Value: "latest"
- Name: REGION
Type: PLAINTEXT
Value: !Sub "${AWS::Region}"
- Name: CONTAINER_NAME
Type: PLAINTEXT
Value: !Sub '${AWS::StackName}-SquidProxyContainer'
ServiceRole: !GetAtt CodeBuildServiceRole.Arn
Source:
BuildSpec: |
version: 0.2
phases:
pre_build:
commands:
- aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $ECR_REPOSITORY
build:
commands:
- docker build -t $IMAGE_REPO_NAME:$CODEBUILD_BUILD_NUMBER .
- docker tag $IMAGE_REPO_NAME:$CODEBUILD_BUILD_NUMBER $IMAGE_REPO_NAME:$IMAGE_TAG
- printf '{"ImageURI":$IMAGE_REPO_NAME:$IMAGE_TAG}' > imageDetail.json
post_build:
commands:
- docker push $IMAGE_REPO_NAME:$CODEBUILD_BUILD_NUMBER
- docker push $IMAGE_REPO_NAME:$IMAGE_TAG
- printf '[{"name":"'$CONTAINER_NAME'","imageUri":"'$IMAGE_URI'"}]' > imageDetail.json
artifacts:
files:
- imageDetail.json
Location: !Sub 'https://git-codecommit.${AWS::Region}.amazonaws.com/v1/repos/${RepositoryName}'
Type: CODECOMMIT
ECSTaskExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: ecs-tasks.amazonaws.com
Action: 'sts:AssumeRole'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
ECSCluster:
DependsOn:
- NLB
Type: 'AWS::ECS::Cluster'
Properties:
CapacityProviders:
- FARGATE
- FARGATE_SPOT
ClusterName: !Sub '${AWS::StackName}-ECSCluster'
ClusterSettings:
- Name: containerInsights
Value: enabled
ECSTaskLogGroup:
Type: 'AWS::Logs::LogGroup'
Properties:
RetentionInDays: 365
ECSTaskDefinition:
Type: 'AWS::ECS::TaskDefinition'
Properties:
ContainerDefinitions:
-
Cpu: 0
Memory: 256
HealthCheck:
Command:
- "CMD-SHELL"
- "exit 0"
Interval: 5
Retries: 2
Timeout: 3
Name: !Sub '${AWS::StackName}-SquidProxyContainer'
Image: 'public.ecr.aws/ubuntu/squid:latest'
PortMappings:
- ContainerPort: 3128
HostPort: 3128
Protocol: tcp
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Ref ECSTaskLogGroup
awslogs-region: !Sub '${AWS::Region}'
awslogs-stream-prefix: sqProxy
Memory: 512
Cpu: 256
RequiresCompatibilities:
- FARGATE
NetworkMode: awsvpc
ExecutionRoleArn: !GetAtt ECSTaskExecutionRole.Arn
TaskRoleArn: !GetAtt ECSTaskExecutionRole.Arn
ECSTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
Name: !Sub '${AWS::StackName}-ECSTargetGroup'
VpcId: !Ref VPC
Protocol: TCP
Port: 3128
TargetType: ip
AccessLogsS3:
Type: 'AWS::S3::Bucket'
Properties:
VersioningConfiguration:
Status: Enabled
PublicAccessBlockConfiguration:
BlockPublicAcls: true
IgnorePublicAcls: true
BlockPublicPolicy: true
RestrictPublicBuckets: true
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: 'AES256'
# NLB and access logs
NLBAccessLogsBucketPolicy:
Type: 'AWS::S3::BucketPolicy'
Properties:
Bucket: !Ref AccessLogsS3
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: Denyhttp
Effect: Deny
Action: 's3:*'
Principal: '*'
Resource:
- !Sub '${AccessLogsS3.Arn}/*'
- !Sub '${AccessLogsS3.Arn}'
Condition:
Bool:
'aws:SecureTransport': false
- Sid: AWSLogDeliveryWrite
Effect: Allow
Principal:
Service: "delivery.logs.amazonaws.com"
Action: 's3:PutObject'
Resource: !Sub '${AccessLogsS3.Arn}/nlb/*'
Condition:
StringEquals:
s3:x-amz-acl: 'bucket-owner-full-control'
- Sid: AWSLogDeliveryAclCheck
Effect: Allow
Principal:
Service: "delivery.logs.amazonaws.com"
Action: 's3:GetBucketAcl'
Resource: !Sub '${AccessLogsS3.Arn}'
- Sid: S3ServerAccessLogsPolicy
Effect: Allow
Principal:
Service: "logging.s3.amazonaws.com"
Action: 's3:PutObject'
Resource: !Sub '${AccessLogsS3.Arn}/codeartifactstores3*'
Condition:
ArnLike:
aws:SourceArn: !Sub '${CodePipelineArtifactStoreBucket.Arn}'
StringEquals:
aws:SourceAccount: !Sub '${AWS::AccountId}'
NLB:
DependsOn: NLBAccessLogsBucketPolicy
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Scheme: internal
Subnets:
- !Ref PrivateSubnet1
- !If
- TwoAz
- !Ref PrivateSubnet2
- !Ref 'AWS::NoValue'
- !If
- ThreeAz
- !Ref PrivateSubnet3
- !Ref 'AWS::NoValue'
- !If
- FourAz
- !Ref PrivateSubnet4
- !Ref 'AWS::NoValue'
- !If
- FiveAz
- !Ref PrivateSubnet5
- !Ref 'AWS::NoValue'
- !If
- SixAz
- !Ref PrivateSubnet6
- !Ref 'AWS::NoValue'
Type: network
LoadBalancerAttributes:
- Key: access_logs.s3.enabled
Value: "true"
- Key: access_logs.s3.bucket
Value: !Ref AccessLogsS3
- Key: access_logs.s3.prefix
Value: "nlb"
- Key: deletion_protection.enabled
Value: "true"
NLBListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
LoadBalancerArn: !Ref NLB
Port: 3128
Protocol: TCP
DefaultActions:
- Type: forward
TargetGroupArn: !Ref ECSTargetGroup
ECSTaskSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: ECS Service Security Group
VpcId:
Ref: VPC
SecurityGroupIngress:
- IpProtocol: tcp
CidrIp: 10.0.0.0/21
FromPort: 3128
ToPort: 3128
Description: Allow Proxy traffic from the VPC
SecurityGroupEgress:
- IpProtocol: -1
CidrIp: 0.0.0.0/0
Description: Allow outboud to internet.
ECSTaskSecurityGroupSelfIngress:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref ECSTaskSecurityGroup
SourceSecurityGroupId: !GetAtt ECSTaskSecurityGroup.GroupId
IpProtocol: -1
Description: Ingress from the self.
ECSService:
Type: 'AWS::ECS::Service'
DependsOn: NLBListener
Properties:
Cluster : !GetAtt ECSCluster.Arn
DeploymentConfiguration:
MaximumPercent: 200