-
Notifications
You must be signed in to change notification settings - Fork 3
/
guidance-stack.yml
884 lines (827 loc) · 28.7 KB
/
guidance-stack.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
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
AWSTemplateFormatVersion: 2010-09-09
Description: (SO9403) Guidance - External Connectivity to Amazon VPC Lattice (Pipeline)
Parameters:
DeploymentMode:
Type: String
Default: BOTH
AllowedValues:
- PUBLIC
- PRIVATE
- BOTH
Description: Deployment mode for the proxy.
VPCLatticeServiceNetwork:
Type: String
Description: VPC Lattice Service Network ID (to create VPC association)
Default: "EMPTY"
VpcName:
Default: vpc-lattice-external-connectivity
Description: Logical name for the proxy VPC
Type: String
VpcCidr:
AllowedPattern: "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/(1[6-9]|2[0-8]))$"
Default: 192.168.1.0/24
Description: CIDR block for the VPC
Type: String
ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/y
PublicSubnet1Cidr:
AllowedPattern: "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/(1[6-9]|2[0-8]))$"
Default: 192.168.1.0/27
Description: CIDR block for the Public Subnet 1 located in AZ 1
Type: String
ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28
PublicSubnet2Cidr:
AllowedPattern: "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/(1[6-9]|2[0-8]))$"
Default: 192.168.1.32/27
Description: CIDR block for the Public Subnet 2 located in AZ 2
Type: String
ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28
PublicSubnet3Cidr:
AllowedPattern: "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/(1[6-9]|2[0-8]))$"
Default: 192.168.1.64/27
Description: CIDR block for the Public Subnet 3 located in AZ 3
Type: String
ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28
PrivateSubnet1Cidr:
AllowedPattern: "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/(1[6-9]|2[0-8]))$"
Default: 192.168.1.96/27
Description: CIDR block for the Private Subnet 1 located in AZ 1
Type: String
ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28
PrivateSubnet2Cidr:
AllowedPattern: "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/(1[6-9]|2[0-8]))$"
Default: 192.168.1.128/27
Description: CIDR block for the Private Subnet 2 located in AZ 2
Type: String
ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28
PrivateSubnet3Cidr:
AllowedPattern: "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/(1[6-9]|2[0-8]))$"
Default: 192.168.1.160/27
Description: CIDR block for the Private Subnet 3 located in AZ 3
Type: String
ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28
Conditions:
PublicDeploymentMode: !Or
- !Equals
- !Ref DeploymentMode
- "PUBLIC"
- !Equals
- !Ref DeploymentMode
- "BOTH"
PrivateDeploymentMode: !Or
- !Equals
- !Ref DeploymentMode
- "PRIVATE"
- !Equals
- !Ref DeploymentMode
- "BOTH"
CreateVPCLatticeAssociation: !Not
- !Equals
- !Ref VPCLatticeServiceNetwork
- "EMPTY"
Resources:
# ---------- PIPELINE ----------
# Amazon ECR repository
LatticeIngressECRRepo:
Type: AWS::ECR::Repository
DeletionPolicy: Retain
# Amazon S3 bucket
LatticeIngressBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Properties:
BucketName: !Sub
- lattice-ingress-codepipeline-${AWS::Region}-${AWS::AccountId}-${RandomizedValue}
- RandomizedValue:
Fn::Select: [0, Fn::Split: [-, Fn::Select: [2, Fn::Split: [/, !Ref AWS::StackId ]]]] # Takes the first part of the random GUID in the cloudformation stacks arn.
AccessControl: Private
# Custom Resource - Moving repository code to AWS CodeCommit
GitRepoToCodeCommitCustomResource:
Type: Custom::CopyGitRepoToS3
Properties:
ServiceToken: !GetAtt GitRepoToCodeCommitLambda.Arn
GitRepoToCodeCommitLambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyDocument:
Statement:
- Effect: Allow
Action:
- s3:PutObject
Resource: !Sub ${LatticeIngressBucket.Arn}/cloneFromGithub/*
PolicyName: PutS3Object
GitRepoToCodeCommitLambda:
Type: AWS::Lambda::Function
Properties:
Description: Lambda to be triggered by Cloudformation Custom resource to copy GitHub repo to CodeCommit repo
Code:
ZipFile: |
import boto3
import logging
import os
import shutil
import cfnresponse
s3 = boto3.client('s3')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
repo = os.environ['GitRepo']
s3Bucket = os.environ['s3Bucket']
path = '/tmp/repo'
s3ObjectName = 'latticecingress'
s3ObjectExtension = 'zip'
s3ObjectFullName = s3ObjectName + '.' + s3ObjectExtension
def lambda_handler(event, context):
response_data = {}
try:
logger.info('Create path and change working directory to: %s' % (path))
os.mkdir(path)
os.chdir(path)
logger.info('Clone the repository: %s to: %s' % (repo, path))
os.system('git clone ' + repo + ' cloned-repo')
shutil.rmtree('cloned-repo/.git')
logger.info('Clone complete. Files in working directory:')
logger.info(os.listdir(os.getcwd()))
logger.info('Create Zip from repo')
shutil.make_archive(s3ObjectName, s3ObjectExtension,'cloned-repo','.')
logger.info('Created zip from repo. Files in working directory:')
logger.info(os.listdir(os.getcwd()))
logger.info('Uploading %s to S3://%s/%s' % (s3ObjectFullName, s3Bucket, 'cloneFromGithub/'+s3ObjectFullName))
s3.upload_file(os.getcwd() + '/' + s3ObjectFullName, s3Bucket, 'cloneFromGithub/'+s3ObjectFullName)
logger.info('Upload Complete. Cleaning directory')
shutil.rmtree(path)
cfnresponse.send(event, context, cfnresponse.SUCCESS, response_data)
except Exception as e:
logger.error('Execution failed...')
logger.error(str(e))
response_data['Data'] = str(e)
cfnresponse.send(event, context, cfnresponse.FAILED, response_data)
Environment:
Variables:
GitRepo: https://github.com/aws-solutions-library-samples/guidance-for-external-connectivity-amazon-vpc-lattice.git
s3Bucket: !Ref LatticeIngressBucket
Handler: index.lambda_handler
Runtime: python3.8
Layers:
- !Sub arn:aws:lambda:${AWS::Region}:553035198032:layer:git-lambda2:8 # https://github.com/lambci/git-lambda-layer
MemorySize: 1024
Role: !GetAtt GitRepoToCodeCommitLambdaRole.Arn
CCRepo:
DependsOn:
- GitRepoToCodeCommitCustomResource
Type: AWS::CodeCommit::Repository
Properties:
Code:
S3:
Bucket: !Ref LatticeIngressBucket
Key: cloneFromGithub/latticecingress.zip
RepositoryDescription: Repository for holding IPS solution Automation Code
RepositoryName: !Sub ${AWS::StackName}-repository
# AWS CodePipeline pipeline
CPipelineCloudFormationRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Principal:
Service:
- cloudformation.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: cfnRolePolicy
PolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Action: '*'
Resource: '*'
CPipelineServiceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- codepipeline.amazonaws.com
Action:
- 'sts:AssumeRole'
Policies:
- PolicyName: CodePipelineRolePolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- codecommit:CancelUploadArchive
- codecommit:GetBranch
- codecommit:GetCommit
- codecommit:GetUploadArchiveStatus
- codecommit:UploadArchive
Resource: !Sub arn:aws:codecommit:${AWS::Region}:${AWS::AccountId}:${CCRepo.Name}
- Effect: Allow
Action:
- codebuild:StartBuild
- codebuild:BatchGetBuilds
Resource:
- !GetAtt LatticeIngressBuildProject.Arn
- Effect: Allow
Action:
- cloudformation:CreateStack
- cloudformation:DeleteStack
- cloudformation:DescribeStacks
- cloudformation:UpdateStack
- cloudformation:CreateChangeSet
- cloudformation:DeleteChangeSet
- cloudformation:DescribeChangeSet
- cloudformation:ExecuteChangeSet
- cloudformation:SetStackPolicy
- cloudformation:ValidateTemplate
Resource: "*"
- Effect: Allow
Action:
- iam:PassRole
Resource:
- !GetAtt CPipelineCloudFormationRole.Arn
- Effect: Allow
Action:
- s3:*
Resource:
- !Sub ${LatticeIngressBucket.Arn}
- !Sub ${LatticeIngressBucket.Arn}/*
CPipelineCodeBuildRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- codebuild.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: CodebuildRolePolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: '*'
- Effect: Allow
Action:
- ecr:PutImage
- ecr:PutImageTagMutability
- ecr:InitiateLayerUpload
- ecr:UploadLayerPart
- ecr:CompleteLayerUpload
- ecr:BatchCheckLayerAvailability
- ecr:GetAuthorizationToken
- ecr:BatchGetImage
- ecr:GetDownloadUrlForLayer
Resource:
- !Sub ${LatticeIngressECRRepo.Arn}
- Effect: Allow
Action:
- ecr:GetAuthorizationToken
Resource: '*'
- Effect: Allow
Action:
- ssm:GetParameter*
- ssm:PutParameter
Resource: !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${AWS::StackName}/codebuild/*
- Effect: Allow
Action:
- s3:PutObject
- s3:GetObject
- s3:GetObjectVersion
- s3:GetBucketAcl
- s3:GetBucketLocation
Resource:
- !Sub ${LatticeIngressBucket.Arn}
- !Sub ${LatticeIngressBucket.Arn}/*
LatticeIngressBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Type: CODEPIPELINE
Environment:
Type: ARM_CONTAINER
Image: aws/codebuild/amazonlinux2-aarch64-standard:2.0
ComputeType: BUILD_GENERAL1_SMALL
PrivilegedMode: true
ImagePullCredentialsType: CODEBUILD
EnvironmentVariables:
- Name: AWS_REGION
Type: PLAINTEXT
Value: !Ref AWS::Region
- Name: AWS_ACCOUNT_ID
Type: PLAINTEXT
Value: !Ref AWS::AccountId
- Name: IMAGE_REPO_NAME
Type: PLAINTEXT
Value: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${LatticeIngressECRRepo}
LogsConfig:
CloudWatchLogs:
Status: ENABLED
GroupName: codebuild
ServiceRole: !GetAtt CPipelineCodeBuildRole.Arn
Source:
Type: CODEPIPELINE
BuildSpec: Dockerfiles/nginx/buildspec.yml
CPipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
ArtifactStore:
Type: S3
Location: !Ref LatticeIngressBucket
DisableInboundStageTransitions:
- Reason: "Disabling to permit code changes to internal stack before initial deployment"
StageName: BuildStage
Name: !Sub ${AWS::StackName}-pipeline
RoleArn: !GetAtt CPipelineServiceRole.Arn
Stages:
- Name: SourceStage
Actions:
- Name: Source
ActionTypeId:
Category: Source
Owner: AWS
Provider: CodeCommit
Version: '1'
Configuration:
RepositoryName: !GetAtt CCRepo.Name
BranchName: main
PollForSourceChanges: 'false'
OutputArtifacts:
- Name: SourceArtifact
RunOrder: 1
- Name: BuildStage
Actions:
- Name: BuildNginxContainer
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: '1'
Configuration:
ProjectName: !Ref LatticeIngressBuildProject
InputArtifacts:
- Name: SourceArtifact
Namespace: NginxBuild
RunOrder: 2
- Name: CopyToS3
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: S3
Version: '1'
InputArtifacts:
- Name: SourceArtifact
Configuration:
BucketName: !Ref LatticeIngressBucket
Extract: true
ObjectKey: deployment
RunOrder: 1
- Name: DeployApplicationStage
Actions:
- Name: CreateChangeSet
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: '1'
InputArtifacts:
- Name: SourceArtifact
Configuration:
ActionMode: CHANGE_SET_REPLACE
Capabilities: CAPABILITY_IAM
RoleArn: !GetAtt CPipelineCloudFormationRole.Arn
StackName: !Sub ${AWS::StackName}-${AWS::AccountId}-ecs
ChangeSetName: !Sub ${AWS::StackName}-${AWS::AccountId}-ecs-cset
TemplatePath: SourceArtifact::cloudformation/ecs/cluster.yaml
ParameterOverrides: !Sub |
{
"VpcId": "${VPC}",
"VpcCidr": "${VpcCidr}",
"PrivateSubnet1": "${PrivateSubnet1}",
"PrivateSubnet2": "${PrivateSubnet2}",
"PrivateSubnet3": "${PrivateSubnet3}",
"PublicSubnet1": "${PublicSubnet1}",
"PublicSubnet2": "${PublicSubnet2}",
"PublicSubnet3": "${PublicSubnet3}",
"NginxImage": "#{NginxBuild.image}"
}
RunOrder: 1
- Name: ExecuteChangeSet
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: '1'
Configuration:
ActionMode: CHANGE_SET_EXECUTE
ChangeSetName: !Sub ${AWS::StackName}-${AWS::AccountId}-ecs-cset
RoleArn: !GetAtt CPipelineCloudFormationRole.Arn
StackName: !Sub ${AWS::StackName}-${AWS::AccountId}-ecs
RunOrder: 2
CPipelineCloudWatchEventRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Principal:
Service:
- events.amazonaws.com
Action: sts:AssumeRole
Path: /
Policies:
- PolicyName: CWEventRolePolicy
PolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Action: codepipeline:StartPipelineExecution
Resource: !Sub arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${AWS::StackName}-pipeline
CPipelineCloudWatchEventRule:
Type: AWS::Events::Rule
Properties:
EventPattern:
source:
- aws.codecommit
detail-type:
- 'CodeCommit Repository State Change'
resources:
- !Sub arn:aws:codecommit:${AWS::Region}:${AWS::AccountId}:${AWS::StackName}-repository
detail:
event:
- referenceCreated
- referenceUpdated
referenceType:
- branch
referenceName:
- main
Targets:
- Arn: !Sub arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${AWS::StackName}-pipeline
RoleArn: !GetAtt CPipelineCloudWatchEventRole.Arn
Id: codepipeline-AppPipeline
# ---------- VPC ----------
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCidr
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: "Name"
Value: !Ref VpcName
IPv6CidrBlock:
Type: AWS::EC2::VPCCidrBlock
Properties:
AmazonProvidedIpv6CidrBlock: true
VpcId: !Ref VPC
InternetGateway:
Condition: PublicDeploymentMode
Type: AWS::EC2::InternetGateway
InternetGatewayAttachment:
Condition: PublicDeploymentMode
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
# ---------- PUBLIC SUBNETS ----------
PublicSubnet1:
Condition: PublicDeploymentMode
Type: AWS::EC2::Subnet
DependsOn: IPv6CidrBlock
Properties:
AssignIpv6AddressOnCreation: true
CidrBlock: !Ref PublicSubnet1Cidr
Ipv6CidrBlock: !Select [ 0, !Cidr [ !Select [ 0, !GetAtt VPC.Ipv6CidrBlocks], 6, 64 ]]
AvailabilityZone: !Select [0, Fn::GetAZs: !Ref 'AWS::Region']
VpcId: !Ref VPC
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: IngressPubSub1
PublicSubnet2:
Condition: PublicDeploymentMode
Type: AWS::EC2::Subnet
DependsOn: IPv6CidrBlock
Properties:
AssignIpv6AddressOnCreation: true
CidrBlock: !Ref PublicSubnet2Cidr
Ipv6CidrBlock: !Select [ 1, !Cidr [ !Select [ 0, !GetAtt VPC.Ipv6CidrBlocks], 6, 64 ]]
AvailabilityZone: !Select [1, Fn::GetAZs: !Ref 'AWS::Region']
VpcId: !Ref VPC
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: IngressPubSub2
PublicSubnet3:
Condition: PublicDeploymentMode
Type: AWS::EC2::Subnet
DependsOn: IPv6CidrBlock
Properties:
AssignIpv6AddressOnCreation: true
CidrBlock: !Ref PublicSubnet3Cidr
Ipv6CidrBlock: !Select [ 2, !Cidr [ !Select [ 0, !GetAtt VPC.Ipv6CidrBlocks], 6, 64 ]]
AvailabilityZone: !Select [2, Fn::GetAZs: !Ref 'AWS::Region']
VpcId: !Ref VPC
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: IngressPubSub3
PublicSubnetRouteTable:
Condition: PublicDeploymentMode
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: IngressPubSubRt
PublicSubnetDefaultIPv4Route:
Condition: PublicDeploymentMode
DependsOn:
- InternetGatewayAttachment
Type: AWS::EC2::Route
Properties:
DestinationCidrBlock: 0.0.0.0/0
RouteTableId: !Ref PublicSubnetRouteTable
GatewayId: !Ref InternetGateway
PublicSubnetDefaultIPv6Route:
Condition: PublicDeploymentMode
DependsOn:
- InternetGatewayAttachment
Type: AWS::EC2::Route
Properties:
DestinationIpv6CidrBlock: ::/0
RouteTableId: !Ref PublicSubnetRouteTable
GatewayId: !Ref InternetGateway
PublicSubnet1RouteTableAssociation:
Condition: PublicDeploymentMode
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PublicSubnetRouteTable
SubnetId: !Ref PublicSubnet1
PublicSubnet2RouteTableAssociation:
Condition: PublicDeploymentMode
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PublicSubnetRouteTable
SubnetId: !Ref PublicSubnet2
PublicSubnet3RouteTableAssociation:
Condition: PublicDeploymentMode
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PublicSubnetRouteTable
SubnetId: !Ref PublicSubnet3
# ---------- PRIVATE SUBNETS ----------
PrivateSubnet1:
Condition: PrivateDeploymentMode
Type: AWS::EC2::Subnet
DependsOn: IPv6CidrBlock
Properties:
AssignIpv6AddressOnCreation: true
CidrBlock: !Ref PrivateSubnet1Cidr
Ipv6CidrBlock: !Select [ 3, !Cidr [ !Select [ 0, !GetAtt VPC.Ipv6CidrBlocks], 6, 64 ]]
AvailabilityZone: !Select [0, Fn::GetAZs: !Ref 'AWS::Region']
VpcId: !Ref VPC
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: IngressPriSub1
PrivateSubnet1RouteTable:
Condition: PrivateDeploymentMode
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: IngressPriSub1Rt
PrivateSubnet1RouteTableAssociation:
Condition: PrivateDeploymentMode
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateSubnet1RouteTable
SubnetId: !Ref PrivateSubnet1
PrivateSubnet2:
Condition: PrivateDeploymentMode
Type: AWS::EC2::Subnet
DependsOn: IPv6CidrBlock
Properties:
AssignIpv6AddressOnCreation: true
CidrBlock: !Ref PrivateSubnet2Cidr
Ipv6CidrBlock: !Select [ 4, !Cidr [ !Select [ 0, !GetAtt VPC.Ipv6CidrBlocks], 6, 64 ]]
AvailabilityZone: !Select [1, Fn::GetAZs: !Ref 'AWS::Region']
VpcId: !Ref VPC
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: IngressPriSub2
PrivateSubnet2RouteTable:
Condition: PrivateDeploymentMode
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: IngressPriSub2Rt
PrivateSubnet2RouteTableAssociation:
Condition: PrivateDeploymentMode
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateSubnet2RouteTable
SubnetId: !Ref PrivateSubnet2
PrivateSubnet3:
Condition: PrivateDeploymentMode
Type: AWS::EC2::Subnet
DependsOn: IPv6CidrBlock
Properties:
AssignIpv6AddressOnCreation: true
CidrBlock: !Ref PrivateSubnet3Cidr
Ipv6CidrBlock: !Select [ 5, !Cidr [ !Select [ 0, !GetAtt VPC.Ipv6CidrBlocks], 6, 64 ]]
AvailabilityZone: !Select [2, Fn::GetAZs: !Ref 'AWS::Region']
VpcId: !Ref VPC
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: IngressPriSub3
PrivateSubnet3RouteTable:
Condition: PrivateDeploymentMode
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: IngressPriSub3Rt
PrivateSubnet3RouteTableAssociation:
Condition: PrivateDeploymentMode
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateSubnet3RouteTable
SubnetId: !Ref PrivateSubnet3
# ---------- VPC ENDPOINTS ----------
EcrDkrEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
PrivateDnsEnabled: true
SecurityGroupIds:
- !Ref EndpointSecurityGroup
ServiceName: !Sub com.amazonaws.${AWS::Region}.ecr.dkr
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
- !Ref PrivateSubnet3
VpcEndpointType: Interface
VpcId: !Ref VPC
EcrApiEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
PrivateDnsEnabled: true
SecurityGroupIds:
- !Ref EndpointSecurityGroup
ServiceName: !Sub com.amazonaws.${AWS::Region}.ecr.api
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
- !Ref PrivateSubnet3
VpcEndpointType: Interface
VpcId: !Ref VPC
CwatchLogsEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
PrivateDnsEnabled: true
SecurityGroupIds:
- !Ref EndpointSecurityGroup
ServiceName: !Sub com.amazonaws.${AWS::Region}.logs
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
- !Ref PrivateSubnet3
VpcEndpointType: Interface
VpcId: !Ref VPC
S3GatewayEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
RouteTableIds:
- !Ref PrivateSubnet1RouteTable
- !Ref PrivateSubnet2RouteTable
- !Ref PrivateSubnet3RouteTable
ServiceName: !Sub com.amazonaws.${AWS::Region}.s3
VpcEndpointType: Gateway
VpcId: !Ref VPC
SsmEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
PrivateDnsEnabled: true
SecurityGroupIds:
- !Ref EndpointSecurityGroup
ServiceName: !Sub com.amazonaws.${AWS::Region}.ssm
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
- !Ref PrivateSubnet3
VpcEndpointType: Interface
VpcId: !Ref VPC
SsmMessagesEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
PrivateDnsEnabled: true
SecurityGroupIds:
- !Ref EndpointSecurityGroup
ServiceName: !Sub com.amazonaws.${AWS::Region}.ssmmessages
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
- !Ref PrivateSubnet3
VpcEndpointType: Interface
VpcId: !Ref VPC
Ec2MessagesEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
PrivateDnsEnabled: true
SecurityGroupIds:
- !Ref EndpointSecurityGroup
ServiceName: !Sub com.amazonaws.${AWS::Region}.ec2messages
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
- !Ref PrivateSubnet3
VpcEndpointType: Interface
VpcId: !Ref VPC
# ---------- SECURITY GROUPS ----------
EndpointSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
VpcId: !Ref VPC
GroupDescription: VPC Endpoint Security Group
SecurityGroupIngress:
- CidrIp: !Ref VpcCidr
IpProtocol: '-1'
FromPort: 443
ToPort: 443
Description: Ingress rule for tcp 443
SecurityGroupEgress:
- CidrIp: 0.0.0.0/0
IpProtocol: '-1'
FromPort: -1
ToPort: -1
Tags:
- Key: Name
Value: Endpoint SG
VPCLatticeAssociationSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
VpcId: !Ref VPC
GroupDescription: VPC Lattice VPC association Security Group
SecurityGroupIngress:
- CidrIp: !Ref VpcCidr
IpProtocol: '-1'
FromPort: 443
ToPort: 443
Description: Ingress rule for tcp 443
- CidrIp: !Ref VpcCidr
IpProtocol: '-1'
FromPort: 80
ToPort: 80
Description: Ingress rule for tcp 80
SecurityGroupEgress:
- CidrIp: 0.0.0.0/0
IpProtocol: '-1'
FromPort: -1
ToPort: -1
Tags:
- Key: Name
Value: VPCLattice SG
# ---------- VPC LATTICE VPC ASSOCIATION ----------
VPCLatticeVPCAssociation:
Condition: CreateVPCLatticeAssociation
Type: AWS::VpcLattice::ServiceNetworkVpcAssociation
Properties:
ServiceNetworkIdentifier: !Ref VPCLatticeServiceNetwork
VpcIdentifier: !Ref VPC
SecurityGroupIds:
- !Ref VPCLatticeAssociationSecurityGroup
Tags:
- Key: Name
Value: vpc-association-external-connectivity