generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsample-developer-environment.yml
More file actions
2443 lines (2399 loc) · 89.3 KB
/
sample-developer-environment.yml
File metadata and controls
2443 lines (2399 loc) · 89.3 KB
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
AWSTemplateFormatVersion: "2010-09-09"
Description: Sample browser-based development environment with VS Code, S3-backed git storage, and secure CloudFront access
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Networking
Parameters:
- VpcCidr
- Label:
default: Deployment Settings
Parameters:
- CodeServerVersion
- GitHubRepo
- GitHubBranch
- S3AssetBucket
- S3AssetPrefix
- DeployPipeline
- RotateSecret
- AutoSetDeveloperProfile
- InstallDotNet
- EnableKiroIDE
- Label:
default: Compute settings
Parameters:
- InstanceArchitecture
- InstanceType
- AMIx86CodeServer
- AMIArmCodeServer
- Label:
default: Tagging and Naming
Parameters:
- PrefixCode
- SolutionTag
- EnvironmentTag
Parameters:
VpcCidr:
Type: String
Default: "10.180"
Description: Network addressing prefix (first two octets) for VPC and subnet CIDR blocks, e.g., '10.180'
GitHubRepo:
Type: String
Default: https://github.com/aws-samples/sample-developer-environment.git
Description: GitHub repository URL to clone as initial workspace content for code-server
GitHubBranch:
Type: String
Default: main
Description: GitHub branch to use for devbox-setup.sh script
S3AssetBucket:
Type: String
Default: ""
Description: (Optional) S3 path for initial workspace content in format 'my-bucket-name'. If provided, this OVERWRITES GitHubRepo parameter.
S3AssetPrefix:
Type: String
Default: ""
Description: (Optional) Asset prefix path. Only required when S3AssetBucket is specified. Must end in '/' e.g. 'assets/' or 'assets/solution/'
DeployPipeline:
Type: String
Default: false
AllowedValues:
- true
- false
Description: Deploy AWS CodePipeline with CodeBuild to automatically build/destroy infrastructure using Terraform
RotateSecret:
Type: String
Default: false
AllowedValues:
- true
- false
Description: Rotate code-server secret every 30 days.
AutoSetDeveloperProfile:
Type: String
Default: true
AllowedValues:
- true
- false
Description: Automatically set Developer profile as default in code-server terminal sessions without requiring elevation
InstallDotNet:
Type: String
Default: false
Description: "Install .NET SDK"
AllowedValues:
- true
- false
EnableKiroIDE:
Type: String
Default: false
Description: "Enable Kiro IDE with NICE DCV remote desktop (requires amd64 architecture)"
AllowedValues:
- true
- false
CodeServerVersion:
Type: String
Default: "4.105.1"
Description: Version of code-server to install. See available versions at github.com/coder/code-server/releases
InstanceArchitecture:
Type: String
Default: amd64
AllowedValues:
- amd64
- arm64
Description: Choose amd64 for AMD/Intel instances or arm64 for Graviton instances
InstanceType:
Type: String
Default: t3a.large
Description: EC2 instance type MUST match architecture. amd64= t3.small, t3a.large, c6i.xlarge, m6a.2xlarge, etc. arm64= t4g.large, c7g.xlarge, m6g.2xlarge, etc. Template will fail if mismatched. For Kiro IDE, t3a.large or larger recommended (minimum 2.4GB memory for GNOME).
AMIx86CodeServer:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64
Description: SSM parameter path for x86_64 AMI (used for AMD/Intel instances)
AMIArmCodeServer:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-arm64
Description: SSM parameter path for arm64 AMI (used for Graviton instances)
PrefixCode:
Type: String
Default: devbox
Description: Resource naming prefix for uniqueness and organization. Six characters or less. Must be lowercase alphanumeric and cannot start with 'aws'
EnvironmentTag:
Type: String
Default: production
Description: Environment identifier for resource tagging e.g. dev, prod
SolutionTag:
Type: String
Default: SampleDeveloperEnvironment
Description: Solution identifier for resource tagging and grouping. Use alphanumeric characters only
### Region to CloudFront Prefix List mapping - delete regions not required for deployment
### To get prefix list for your region: aws ec2 describe-managed-prefix-lists --filters Name=prefix-list-name,Values=com.amazonaws.global.cloudfront.origin-facing
Mappings:
RegionMap:
ap-northeast-1:
CloudFrontPrefixList: pl-58a04531
ALBAccount: 582318560864
ap-northeast-2:
CloudFrontPrefixList: pl-22a6434b
ALBAccount: 600734575887
ap-northeast-3:
CloudFrontPrefixList: pl-31a14458
ALBAccount: 383597477331
ap-south-1:
CloudFrontPrefixList: pl-9aa247f3
ALBAccount: 718504428378
ap-southeast-1:
CloudFrontPrefixList: pl-31a34658
ALBAccount: 114774131450
ap-southeast-2:
CloudFrontPrefixList: pl-b8a742d1
ALBAccount: 783225319266
ca-central-1:
CloudFrontPrefixList: pl-38a64351
ALBAccount: 985666609251
eu-central-1:
CloudFrontPrefixList: pl-a3a144ca
ALBAccount: 054676820928
eu-north-1:
CloudFrontPrefixList: pl-fab65393
ALBAccount: 897822967062
eu-west-1:
CloudFrontPrefixList: pl-4fa04526
ALBAccount: 156460612806
eu-west-2:
CloudFrontPrefixList: pl-93a247fa
ALBAccount: 652711504416
eu-west-3:
CloudFrontPrefixList: pl-75b1541c
ALBAccount: 009996457667
sa-east-1:
CloudFrontPrefixList: pl-5da64334
ALBAccount: 507241528517
us-east-1:
CloudFrontPrefixList: pl-3b927c52
ALBAccount: 127311923021
us-east-2:
CloudFrontPrefixList: pl-b6a144df
ALBAccount: 033677994240
us-west-1:
CloudFrontPrefixList: pl-4ea04527
ALBAccount: 027434742980
us-west-2:
CloudFrontPrefixList: pl-82a045eb
ALBAccount: 797873946194
Rules:
KiroIDEArchitectureCheck:
RuleCondition: !Equals [!Ref EnableKiroIDE, "true"]
Assertions:
- Assert: !Equals [!Ref InstanceArchitecture, "amd64"]
AssertDescription: "Kiro IDE requires amd64 architecture. Please select an amd64 instance type or disable Kiro IDE."
Conditions:
CreatePipeline: !Equals [!Ref DeployPipeline, "true"]
EnableRotation: !Equals [!Ref RotateSecret, "true"]
IsArm64: !Equals [!Ref InstanceArchitecture, 'arm64']
IsAmd64: !Equals [!Ref InstanceArchitecture, "amd64"]
HasS3Assets: !Not [!Equals [!Ref S3AssetBucket, ""]]
EnableKiroIDE: !Equals [!Ref EnableKiroIDE, "true"]
InstallKiroIDE: !And
- !Condition EnableKiroIDE
- !Condition IsAmd64
Resources:
### Resource Group - Groups all AWS resources for easy management and tracking
resourcegroup:
Type: AWS::ResourceGroups::Group
Properties:
Description: Sample Developer Environment Resources
Name: !Sub ${PrefixCode}-resources
ResourceQuery:
Type: TAG_FILTERS_1_0
Query:
ResourceTypeFilters:
- AWS::AllSupported
TagFilters:
- Key: solution
Values:
- !Sub ${SolutionTag}
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-resources
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: environment
Value: !Sub ${EnvironmentTag}
- Key: resourcetype
Value: management
### KMS - Central encryption key for CloudWatch Logs, Secrets Manager, and other AWS services
kmskey:
Type: AWS::KMS::Key
Properties:
Description: Shared encryption key for AWS services
PendingWindowInDays: 7
EnableKeyRotation: true
KeyPolicy:
Version: 2012-10-17
Id: key-default-1
Statement:
- Sid: Enable IAM User Permissions
# https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-overview.html
Effect: Allow
Principal:
AWS: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:root
Action:
- kms:*
Resource: "*"
- Sid: Enable Cloudwatch access to KMS Key for VPC log group
# https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/encrypt-log-data-kms.html
Effect: Allow
Principal:
Service: !Sub logs.${AWS::Region}.amazonaws.com
Action:
- kms:Encrypt*
- kms:Decrypt*
- kms:ReEncrypt*
- kms:GenerateDataKey*
- kms:Describe*
Resource: "*"
Condition:
ArnLike:
"kms:EncryptionContext:aws:logs:arn": !Sub "arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:*"
Tags:
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: environment
Value: !Sub ${EnvironmentTag}
- Key: resourcetype
Value: security
kmskeyalias:
Type: AWS::KMS::Alias
Properties:
AliasName: !Sub alias/${PrefixCode}-kms-cmk
TargetKeyId: !Ref kmskey
### Network Infrastructure - VPC with public/private subnets and flow logs for developer environment
vpc01:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Sub ${VpcCidr}.0.0/16
EnableDnsHostnames: true
EnableDnsSupport: true
InstanceTenancy: default
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-vpc01
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: environment
Value: !Sub ${EnvironmentTag}
- Key: resourcetype
Value: network
iamrolevpcflowlogs:
Type: AWS::IAM::Role
# https://docs.aws.amazon.com/vpc/latest/userguide/flow-logs-cwl.html
Properties:
RoleName: !Sub ${PrefixCode}-iamrole-vpcflowlogs
Description: Publish flow logs to CloudWatch Logs
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: vpc-flow-logs.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: !Sub ${PrefixCode}-iampolicy-VpcFlowLogsPermissions
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- logs:DescribeLogGroups
- logs:DescribeLogStreams
Resource:
- !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/events/${PrefixCode}-vpcflowlog:*
- !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/events/${PrefixCode}-vpcflowlog:*:*
Condition:
StringEquals:
aws:SourceAccount: !Sub "${AWS::AccountId}"
ArnLike:
"aws:SourceArn": !Sub "arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:*"
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-iamrole-VpcFlowLogs
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: security
- Key: environment
Value: !Sub ${EnvironmentTag}
Metadata:
cdk_nag:
rules_to_suppress:
- id: AwsSolutions-IAM5
reason: "XXXX https://docs.aws.amazon.com/vpc/latest/userguide/flow-logs-iam-role.html"
vpcloggroup:
Type: AWS::Logs::LogGroup
Properties:
KmsKeyId: !GetAtt kmskey.Arn
LogGroupName: !Sub /aws/vpc/${PrefixCode}-flowlogs
RetentionInDays: 30
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-vpcflowloggroup
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: monitoring
- Key: environment
Value: !Sub ${EnvironmentTag}
vpcflowlog:
Type: AWS::EC2::FlowLog
Properties:
DeliverLogsPermissionArn: !GetAtt iamrolevpcflowlogs.Arn
LogGroupName: !Ref vpcloggroup
ResourceId: !Ref vpc01
ResourceType: VPC
TrafficType: ALL
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-vpcflowlog
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: monitoring
- Key: environment
Value: !Sub ${EnvironmentTag}
internetgateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-internetgateway
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: network
- Key: environment
Value: !Sub ${EnvironmentTag}
internetgatewayattach:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref vpc01
InternetGatewayId: !Ref internetgateway
subnetpublic01:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref vpc01
AvailabilityZone: !Select [0, !GetAZs ""]
CidrBlock: !Sub ${VpcCidr}.1.0/24
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-subnet-public01-AvailabilityZone01
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: network
- Key: environment
Value: !Sub ${EnvironmentTag}
# Second public subnet required for ALB
subnetpublic02:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref vpc01
AvailabilityZone: !Select [1, !GetAZs ""]
CidrBlock: !Sub ${VpcCidr}.2.0/24
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-subnet-public02-AvailabilityZone02
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: network
- Key: environment
Value: !Sub ${EnvironmentTag}
subnetprivate01:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref vpc01
AvailabilityZone: !Select [0, !GetAZs ""]
CidrBlock: !Sub ${VpcCidr}.3.0/24
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-subnet-private01-AvailabilityZone01
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: network
- Key: environment
Value: !Sub ${EnvironmentTag}
elasticip01:
Type: AWS::EC2::EIP
Properties:
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-eip-AvailabilityZone01
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: network
- Key: environment
Value: !Sub ${EnvironmentTag}
natgateway01:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt elasticip01.AllocationId
SubnetId: !Ref subnetpublic01
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-nat-public01-AvailabilityZone01
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: network
- Key: environment
Value: !Sub ${EnvironmentTag}
DependsOn: internetgateway
routetablepublic:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref vpc01
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-routetable-public
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: network
- Key: environment
Value: !Sub ${EnvironmentTag}
routepublic:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref routetablepublic
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !GetAtt internetgateway.InternetGatewayId
routeassociationpublic01:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref subnetpublic01
RouteTableId: !Ref routetablepublic
routeassociationpublic02:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref subnetpublic02
RouteTableId: !Ref routetablepublic
routetableprivate:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref vpc01
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-routetable-private1-AvailabilityZone1
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: network
- Key: environment
Value: !Sub ${EnvironmentTag}
routeprivate:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref routetableprivate
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !GetAtt natgateway01.NatGatewayId
routeassociationprivate:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref subnetprivate01
RouteTableId: !Ref routetableprivate
### Centralized logging bucket for CloudFront and S3 access logs
s3bucketlogs:
Type: AWS::S3::Bucket
DeletionPolicy: Delete
Properties:
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
VersioningConfiguration:
Status: Enabled
OwnershipControls:
Rules:
- ObjectOwnership: ObjectWriter
LifecycleConfiguration:
Rules:
- Id: DeleteOldLogs
Status: Enabled
ExpirationInDays: 30
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-logs
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: storage
- Key: environment
Value: !Sub ${EnvironmentTag}
Metadata:
cfn_nag:
rules_to_suppress:
- id: W35
reason: "This is an access logs bucket and does not need its own access logging."
checkov:
skip:
- id: CKV_AWS_18
comment: "This is an access logs bucket and does not need its own access logging."
s3bucketpolicylogs:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref s3bucketlogs
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: EnforceSecureTransport
Effect: Deny
Principal: "*"
Action: s3:*
Resource:
- !Sub arn:${AWS::Partition}:s3:::${s3bucketlogs}
- !Sub arn:${AWS::Partition}:s3:::${s3bucketlogs}/*
Condition:
Bool:
aws:SecureTransport: false
- Sid: Allow TLS 1.2 and above
Effect: Deny
Principal: "*"
Action:
- s3:*
Resource:
- !Sub arn:${AWS::Partition}:s3:::${s3bucketlogs}
- !Sub arn:${AWS::Partition}:s3:::${s3bucketlogs}/*
Condition:
NumericLessThan:
s3:TlsVersion: 1.2
# https://docs.aws.amazon.com/elasticloadbalancing/latest/application/enable-access-logging.html
- Sid: Allow ALB logging access regions available as of August 2022 or later
Effect: Allow
Principal:
Service: logdelivery.elasticloadbalancing.amazonaws.com
Action:
- s3:PutObject
Resource:
- !Sub arn:${AWS::Partition}:s3:::${s3bucketlogs}
- !Sub arn:${AWS::Partition}:s3:::${s3bucketlogs}/*
- Sid: Allow ALB logging access regions available before August 2022
Effect: Allow
Principal:
AWS:
!Join
- ""
- - "arn:"
- !Ref AWS::Partition
- ":iam::"
- !FindInMap [RegionMap, !Ref "AWS::Region", ALBAccount]
- ":root"
Action:
- s3:PutObject
Resource:
- !Sub arn:${AWS::Partition}:s3:::${s3bucketlogs}
- !Sub arn:${AWS::Partition}:s3:::${s3bucketlogs}/*
- Sid: Allow S3 Logging Service for git and artifact buckets
Effect: Allow
Principal:
Service: logging.s3.amazonaws.com
Action: s3:PutObject
Resource:
- !Sub arn:${AWS::Partition}:s3:::${s3bucketlogs}/s3-logs/git/*
- !Sub arn:${AWS::Partition}:s3:::${s3bucketlogs}/s3-logs/pipeline/*
Condition:
StringEquals:
aws:SourceAccount: !Ref AWS::AccountId
ArnLike:
aws:SourceArn:
- !Sub arn:${AWS::Partition}:s3:::${s3bucketgit}
- !If
# BEWARE: Inline condition for artifact bucket logging - can't be grouped due to policy structure.
- CreatePipeline
- Sid: Allow S3 Logging Service for artifact bucket
Effect: Allow
Principal:
Service: logging.s3.amazonaws.com
Action: s3:PutObject
Resource:
- !Sub arn:${AWS::Partition}:s3:::${s3bucketlogs}/s3-logs/pipeline/*
Condition:
StringEquals:
aws:SourceAccount: !Ref AWS::AccountId
ArnLike:
aws:SourceArn:
- !Sub arn:${AWS::Partition}:s3:::${s3bucketartifact}
- !Ref AWS::NoValue
- Sid: Allow ALB Logging
Effect: Allow
Principal:
AWS: !Join
- ""
- - "arn:"
- !Ref AWS::Partition
- ":iam::"
- !FindInMap [RegionMap, !Ref "AWS::Region", ALBAccount]
- ":root"
Action: s3:PutObject
Resource: !Sub arn:${AWS::Partition}:s3:::${s3bucketlogs}/alb-logs/*
### Access Layer - ALB and CloudFront distribution for secure access to code-server
albcodeserver:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name: !Sub ${PrefixCode}-alb-codeserver
Scheme: internet-facing
Type: application
Subnets:
- !Ref subnetpublic01
- !Ref subnetpublic02
SecurityGroups:
- !Ref securitygroupalb
LoadBalancerAttributes:
- Key: access_logs.s3.enabled
Value: true
- Key: access_logs.s3.bucket
Value: !Ref s3bucketlogs
- Key: access_logs.s3.prefix
Value: alb-logs
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-alb-codeserver
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: network
- Key: environment
Value: !Sub ${EnvironmentTag}
Metadata:
cfn_nag:
rules_to_suppress:
- id: CKV_AWS_131
reason: "ALB uses HTTP for internal AWS traffic. Security handled by CloudFront HTTPS and origin verification. Consider end-to-end HTTPS if required."
checkov:
skip:
- id: CKV_AWS_131
comment: "ALB uses HTTP for internal AWS traffic. Security handled by CloudFront HTTPS and origin verification. Consider end-to-end HTTPS if required."
albtargetgroupcodeserver:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckPath: /login
HealthCheckPort: 8080
Name: !Sub ${PrefixCode}-targetgroup-codeserver
Port: 8080
Protocol: HTTP
TargetType: instance
Targets:
- Id: !Ref ec2codeserver
VpcId: !Ref vpc01
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-targetgroup-codeserver
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: network
- Key: environment
Value: !Sub ${EnvironmentTag}
# DCV target group for NICE DCV desktop access
albtargetgroupdcv:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Condition: InstallKiroIDE
Properties:
HealthCheckPath: /
HealthCheckPort: 8443
HealthCheckProtocol: HTTPS
HealthCheckTimeoutSeconds: 10
HealthyThresholdCount: 2
Matcher:
HttpCode: 200,404 # DCV returns 404 on root path when no session is active, but service is healthy
Name: !Sub ${PrefixCode}-targetgroup-dcv
Port: 8443
Protocol: HTTPS
TargetType: instance
Targets:
- Id: !Ref ec2codeserver
VpcId: !Ref vpc01
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-targetgroup-dcv
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: network
- Key: environment
Value: !Sub ${EnvironmentTag}
# ALB listener default rule blocks all traffic unless coming from CloudFront with correct secret header
alblistenercodeserver:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: fixed-response
FixedResponseConfig:
ContentType: text/plain
MessageBody: "Access Denied"
StatusCode: 403
LoadBalancerArn: !Ref albcodeserver
Port: 80
Protocol: HTTP
Metadata:
cfn_nag:
rules_to_suppress:
- id: W56
reason: "ALB uses HTTP for internal AWS traffic. Security handled by CloudFront HTTPS and origin verification. Consider end-to-end HTTPS if required."
- id: CKV_AWS_103
reason: "TLS check not applicable - ALB uses HTTP internally. External TLS 1.2 enforced at CloudFront. Consider end-to-end HTTPS if required."
checkov:
skip:
- id: CKV_AWS_2
comment: "ALB uses HTTP for internal AWS traffic. Security handled by CloudFront HTTPS and origin verification. Consider end-to-end HTTPS if required."
- id: CKV_AWS_103
comment: "TLS check not applicable - ALB uses HTTP internally. External TLS 1.2 enforced at CloudFront. Consider end-to-end HTTPS if required."
# Allow traffic only if secret header from CloudFront is present
alblistenerrulecodeserver:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
Properties:
Actions:
- Type: forward
TargetGroupArn: !Ref albtargetgroupcodeserver
Conditions:
- Field: http-header
HttpHeaderConfig:
HttpHeaderName: !Sub "{{resolve:secretsmanager:${cloudfrontsecretheadercodeserver}:SecretString:HeaderName}}"
Values:
- !Sub "{{resolve:secretsmanager:${cloudfrontsecretheadercodeserver}:SecretString:HeaderValue}}"
ListenerArn: !Ref alblistenercodeserver
Priority: 10
alblistenerruledcv:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
Condition: InstallKiroIDE
Properties:
Actions:
- Type: forward
TargetGroupArn: !Ref albtargetgroupdcv
Conditions:
- Field: http-header
HttpHeaderConfig:
HttpHeaderName: !Sub "{{resolve:secretsmanager:${cloudfrontsecretheadercodeserver}:SecretString:HeaderName}}"
Values:
- !Sub "{{resolve:secretsmanager:${cloudfrontsecretheadercodeserver}:SecretString:HeaderValue}}"
- Field: path-pattern
PathPatternConfig:
Values:
- "/dcv*"
ListenerArn: !Ref alblistenercodeserver
Priority: 5
# Security group allows traffic only from CloudFront IP ranges
securitygroupalb:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Sub ${PrefixCode}-securitygroup-alb
GroupDescription: ALB security group
VpcId: !Ref vpc01
SecurityGroupIngress:
- Description: HTTP inbound from CloudFront
IpProtocol: tcp
FromPort: 80
ToPort: 80
SourcePrefixListId: !FindInMap [RegionMap, !Ref "AWS::Region", CloudFrontPrefixList]
SecurityGroupEgress:
- Description: Allow to EC2
IpProtocol: tcp
FromPort: 8080
ToPort: 8080
DestinationSecurityGroupId: !Ref securitygroupcodeserver
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-securitygroup-alb
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: security
- Key: environment
Value: !Sub ${EnvironmentTag}
cloudfrontcachepolcodeserver:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
Comment: Cache policy for VS code-server allows caching with cookie/header/query string forwarding
DefaultTTL: 86400
MaxTTL: 31536000
MinTTL: 1
Name: !Sub ${PrefixCode}-cloudfront-policy
ParametersInCacheKeyAndForwardedToOrigin:
CookiesConfig:
CookieBehavior: all
EnableAcceptEncodingGzip: true
HeadersConfig:
HeaderBehavior: whitelist
Headers:
- Host
- Origin
- Authorization
QueryStringsConfig:
QueryStringBehavior: all
# CloudFront distribution adds secret header to all requests to ALB
cloudfrontdistributioncodeserver:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: true
Origins:
- DomainName: !GetAtt albcodeserver.DNSName
Id: CodeServerOrigin
CustomOriginConfig:
OriginProtocolPolicy: http-only
HTTPPort: 80
# Add secret header to all requests to prevent direct ALB access
OriginCustomHeaders:
- HeaderName: !Sub "{{resolve:secretsmanager:${cloudfrontsecretheadercodeserver}:SecretString:HeaderName}}"
HeaderValue: !Sub "{{resolve:secretsmanager:${cloudfrontsecretheadercodeserver}:SecretString:HeaderValue}}"
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
- OPTIONS
- PUT
- PATCH
- POST
- DELETE
CachePolicyId: !Ref cloudfrontcachepolcodeserver
OriginRequestPolicyId: 216adef6-5c7f-47e4-b989-5492eafa07d3 # Managed AllViewer Policy
TargetOriginId: CodeServerOrigin
ViewerProtocolPolicy: redirect-to-https
CacheBehaviors:
# Cache behavior for NICE DCV (Kiro IDE). Only used when EnableKiroIDE is true.
- PathPattern: /dcv*
AllowedMethods:
- GET
- HEAD
- OPTIONS
- PUT
- PATCH
- POST
- DELETE
CachePolicyId: !Ref cloudfrontcachepolcodeserver
OriginRequestPolicyId: 216adef6-5c7f-47e4-b989-5492eafa07d3
TargetOriginId: CodeServerOrigin
ViewerProtocolPolicy: redirect-to-https
Logging:
Bucket: !GetAtt s3bucketlogs.DomainName
IncludeCookies: false
Prefix: cloudfront-logs/
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-cloudfrontdistribution-alb
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: network
- Key: environment
Value: !Sub ${EnvironmentTag}
Metadata:
cdk_nag:
rules_to_suppress:
- id: AwsSolutions-CFR4
reason: "CloudFront uses HTTP internally with ALB. Security handled by CloudFront HTTPS and origin verification. Consider end-to-end HTTPS if required."
- id: AwsSolutions-CFR5
reason: "Origin uses HTTP within AWS network. Consider end-to-end HTTPS if certificate management overhead is acceptable."
- id: CKV_AWS_174
reason: "Using CloudFront default certificate with TLS 1.2. Consider custom certificate if custom domain required."
- id: AwsSolutions-CFR2
reason: "Security handled by CloudFront origin header, IP restrictions and ALB rules. WAF needs us-east-1 deployment. Consider using StackSets for WAF if required"
- id: CKV_AWS_68
reason: "Security handled by CloudFront origin header, IP restrictions and ALB rules. WAF needs us-east-1 deployment. Consider using StackSets for WAF if required"
- id: AwsSolutions-CFR1
reason: "Geo-restrictions not implemented for flexibility. Consider enabling geo-restrictions if regional compliance or security requirements dictate."
checkov:
skip:
- id: CKV_AWS_174
comment: "Using CloudFront default certificate with TLS 1.2. Consider custom certificate if custom domain required."
- id: CKV_AWS_68
comment: "WAF not implemented for development environment. Security handled through CloudFront origin verification and code-server authentication. Consider WAF for production."
cloudfrontsecretheadercodeserver:
Type: AWS::SecretsManager::Secret
Properties:
Name: !Sub ${PrefixCode}-secret-cloudfrontheader
Description: Origin verification header for code-server CloudFront distribution
KmsKeyId: !GetAtt kmskey.Arn
GenerateSecretString:
SecretStringTemplate: '{"HeaderName": "X-Origin-Verify"}'
GenerateStringKey: "HeaderValue"
PasswordLength: 32
ExcludeCharacters: '\[]{}>|*&!%#`@,."$:+=-~^()'''
Tags:
- Key: Name
Value: !Sub ${PrefixCode}-secret-cloudfrontheader
- Key: provisioner
Value: CFN
- Key: solution
Value: !Sub ${SolutionTag}
- Key: resourcetype
Value: security
- Key: environment
Value: !Sub ${EnvironmentTag}
Metadata:
cfn_nag:
rules_to_suppress:
- id: W77
reason: "Secret is a static verification token between CloudFront and ALB. Rotation would require coordinated updates to both services causing downtime. Consider automated rotation if required."
- id: AwsSolutions-SMG4
reason: "Secret is a static verification token between CloudFront and ALB. Rotation would require coordinated updates to both services causing downtime. Consider automated rotation if required."
cdk_nag: