-
Notifications
You must be signed in to change notification settings - Fork 282
/
Copy pathaws-stack.yml
1368 lines (1232 loc) · 48 KB
/
aws-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
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: "Buildkite stack %v"
# The Buildkite Elastic CI Stack for AWS gives you a private,
# autoscaling Buildkite Agent cluster. Use it to parallelize
# large test suites across thousands of nodes, run tests and
# deployments for Linux or Windows based services and apps,
# or run AWS ops tasks.
#
# To gain a better understanding of how Elastic CI Stack works
# and how to use it most effectively and securely, check out
# the following resources:
#
# * Elastic CI Stack for AWS Overview: https://buildkite.com/docs/agent/v3/elastic_ci_aws
# * Elastic CI Stack for AWS Tutorial: https://buildkite.com/docs/tutorials/elastic-ci-stack-aws
# * Running Buildkite Agent on AWS: https://buildkite.com/docs/agent/v3/aws
# * GitHub Repo for Elastic CI Stack: https://github.com/buildkite/elastic-ci-stack-for-aws
# * Template Parameters for Elastic CI Stack for AWS: https://buildkite.com/docs/agent/v3/elastic-ci-aws/parameters
# * Using AWS Secrets Manager: https://buildkite.com/docs/agent/v3/aws/secrets-manager
# * VPC Design: https://buildkite.com/docs/agent/v3/aws/vpc
# * CloudFormation Service Role: https://buildkite.com/docs/agent/v3/elastic-ci-aws/cloudformation-service-role
Transform: AWS::Serverless-2016-10-31
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Buildkite Configuration
Parameters:
- BuildkiteAgentTokenParameterStorePath
- BuildkiteAgentTokenParameterStoreKMSKey
- BuildkiteAgentToken
- BuildkiteQueue
- Label:
default: Advanced Buildkite Configuration
Parameters:
- BuildkiteAgentRelease
- BuildkiteAgentTags
- BuildkiteAgentTimestampLines
- BuildkiteAgentExperiments
- EnableAgentGitMirrorsExperiment
- BuildkiteAgentTracingBackend
- BuildkiteTerminateInstanceAfterJob
- BuildkiteAdditionalSudoPermissions
- BuildkiteWindowsAdministrator
- BuildkiteAgentScalerServerlessARN
- BuildkiteAgentScalerVersion
- Label:
default: Network Configuration
Parameters:
- VpcId
- Subnets
- AvailabilityZones
- SecurityGroupId
- AssociatePublicIpAddress
- Label:
default: Instance Configuration
Parameters:
- ImageId
- ImageIdParameter
- InstanceOperatingSystem
- InstanceType
- EnableInstanceStorage
- AgentsPerInstance
- KeyName
- SpotPrice
- SecretsBucket
- SecretsBucketRegion
- SecretsBucketEncryption
- ArtifactsBucket
- AuthorizedUsersUrl
- BootstrapScriptUrl
- AgentEnvFileUrl
- RootVolumeSize
- RootVolumeName
- RootVolumeType
- RootVolumeEncrypted
- ManagedPolicyARN
- InstanceRoleName
- InstanceRolePermissionsBoundaryARN
- IMDSv2Tokens
- EnableDetailedMonitoring
- InstanceName
- Label:
default: Auto-scaling Configuration
Parameters:
- MinSize
- MaxSize
- OnDemandPercentage
- ScaleOutFactor
- ScaleInIdlePeriod
- ScaleOutForWaitingJobs
- InstanceCreationTimeout
- Label:
default: Cost Allocation Configuration
Parameters:
- EnableCostAllocationTags
- CostAllocationTagName
- CostAllocationTagValue
- Label:
default: Docker Daemon Configuration
Parameters:
- EnableDockerUserNamespaceRemap
- EnableDockerExperimental
- Label:
default: Docker Registry Configuration
Parameters:
- ECRAccessPolicy
- Label:
default: Plugin Configuration
Parameters:
- EnableSecretsPlugin
- EnableECRPlugin
- EnableDockerLoginPlugin
Parameters:
KeyName:
Description: Optional - SSH keypair used to access the buildkite instances via ec2_user, setting this will enable SSH ingress
Type: String
Default: ""
BuildkiteAgentRelease:
Type: String
AllowedValues:
- stable
- beta
- edge
Default: "stable"
BuildkiteAgentToken:
Description: Buildkite agent registration token. Or, preload it into SSM Parameter Store and use BuildkiteAgentTokenParameterStorePath for secure environments.
Type: String
NoEcho: true
Default: ""
BuildkiteAgentTokenParameterStorePath:
Description: Existing SSM Parameter Store path to the Buildkite agent registration token (takes precedence over BuildkiteAgentToken). Expects a leading slash ('/').
Type: String
Default: ""
AllowedPattern: "^$|^/[a-zA-Z0-9_.\\-/]+$"
ConstraintDescription: "Expects a leading forward slash"
BuildkiteAgentTokenParameterStoreKMSKey:
Description: AWS KMS key ID used to encrypt the SSM parameter (if encrypted)
Type: String
Default: ""
BuildkiteAgentTags:
Description: Additional tags separated by commas to provide to the agent. E.g os=linux,llamas=always
Type: String
Default: ""
BuildkiteAgentTimestampLines:
Description: Set to true to prepend timestamps to every line of output
Type: String
AllowedValues:
- "true"
- "false"
Default: "false"
BuildkiteAgentExperiments:
Description: Agent experiments to enable, comma delimited. See https://github.com/buildkite/agent/blob/master/EXPERIMENTS.md.
Type: String
Default: ""
BuildkiteAgentScalerServerlessARN:
Description: ARN of the Serverless Application Repository that hosts the version of buildkite-agent-scaler to run. This needs to be public or shared with your AWS account. See https://aws.amazon.com/serverless/serverlessrepo/.
Type: String
Default: arn:aws:serverlessrepo:us-east-1:172840064832:applications/buildkite-agent-scaler
BuildkiteAgentScalerVersion:
Description: Version of the buildkite-agent-scaler to use
Type: String
Default: "1.4.0"
BuildkiteAgentTracingBackend:
Description: The tracing backend to use for CI tracing. See https://buildkite.com/docs/agent/v3/tracing
Type: String
AllowedValues:
- ""
- "datadog"
- "opentelemetry"
Default: ""
BuildkiteTerminateInstanceAfterJob:
Description: Set to "true" to terminate the instance after a job has completed.
Type: String
AllowedValues:
- "true"
- "false"
Default: "false"
BuildkiteAdditionalSudoPermissions:
Description: Optional - Comma separated list of commands to allow the buildkite-agent user to run using sudo. Note that the commands should be fully qualified paths to executables.
Type: String
Default: ""
BuildkiteWindowsAdministrator:
Description: Set to "true" to add the local "buildkite-agent" user account to the local Windows Administrator group.
Type: String
AllowedValues:
- "true"
- "false"
Default: "true"
BuildkiteQueue:
Description: Queue name that agents will use, targeted in pipeline steps using "queue={value}"
Type: String
Default: default
MinLength: 1
AgentsPerInstance:
Description: Number of Buildkite agents to run on each instance
Type: Number
Default: 1
MinValue: 1
SecretsBucket:
Description: Optional - Name of an existing S3 bucket containing pipeline secrets (Created if left blank)
Type: String
Default: ""
SecretsBucketRegion:
Description: Optional - Region for the SecretsBucket. If blank the bucket's region is dynamically discovered.
Type: String
Default: ""
SecretsBucketEncryption:
Description: Indicates whether the SecretsBucket should enforce encryption at rest and in transit
Type: String
AllowedValues:
- "true"
- "false"
Default: "false"
ArtifactsBucket:
Description: Optional - Name of an existing S3 bucket for build artifact storage
Type: String
Default: ""
BootstrapScriptUrl:
Description: Optional - HTTPS or S3 URL for a script to run on each instance during boot
Type: String
Default: ""
AgentEnvFileUrl:
Description: Optional - HTTPS or S3 URL for a list of environment variables to propagate into the agent's execution environment. Note that these environment variables **will not** be passed into builds running on the agent, only to the agent process itself.
Type: String
Default: ""
AuthorizedUsersUrl:
Description: Optional - HTTPS or S3 URL to periodically download ssh authorized_keys from, setting this will enable SSH ingress. authorized_keys are applied to ec2_user
Type: String
Default: ""
VpcId:
Type: String
Description: Optional - Id of an existing VPC to launch instances into. Leave blank to have a new VPC created
Default: ""
Subnets:
Type: CommaDelimitedList
Description: Optional - Comma separated list of two existing VPC subnet ids where EC2 instances will run. Required if setting VpcId.
Default: ""
AvailabilityZones:
Type: CommaDelimitedList
Description: Optional - Comma separated list of AZs that subnets are created in (if Subnets parameter is not specified)
Default: ""
InstanceType:
Description: Instance type. Comma-separated list with 1-4 instance types. The order is a prioritized preference for launching OnDemand instances, and a non-prioritized list of types to consider for Spot Instances (where used).
Type: String
Default: t3.large
MinLength: 1
AllowedPattern: "^[\\w\\.]+(,[\\w\\.]*){0,3}$"
ConstraintDescription: "must contain 1-4 instance types separated by commas. No space before/after the comma."
SpotPrice:
Description: Maximum spot price to use for the instances, in instance cost per hour. Values >0 will result in 100% of instances being spot. 0 means only use normal (non-spot) instances. This parameter is deprecated - we recommend setting to 0 and using OnDemandPercentage to opt into spot instances.
Type: String
Default: 0
MaxSize:
Description: Maximum number of instances
Type: Number
Default: 10
MinValue: 1
MinSize:
Description: Minimum number of instances
Type: Number
Default: 0
OnDemandPercentage:
Description: Percentage of total instances that should launch as OnDemand. Default is 100% OnDemand - reduce this to use some Spot Instances when they're available and cheaper than the OnDemand price. A value of 70 means 70% OnDemand and 30% Spot Instances.
Type: Number
Default: 100
MinValue: 0
MaxValue: 100
ScaleOutFactor:
Description: A decimal factor to apply to scale out changes to speed up or slow down scale-out
Type: Number
Default: 1.0
ScaleInIdlePeriod:
Description: Number of seconds an agent must be idle before terminating
Type: Number
Default: 600
ScaleOutForWaitingJobs:
Type: String
Description: Whether to scale-out for steps behind wait steps. Make sure you have a long enough idle period!
AllowedValues:
- "true"
- "false"
Default: "false"
InstanceCreationTimeout:
Description: Timeout period for Autoscaling Group Creation Policy
Type: String
Default: ""
RootVolumeSize:
Description: Size of each instance's root EBS volume (in GB)
Type: Number
Default: 250
MinValue: 10
RootVolumeName:
Description: Name of the root block device for your AMI
Type: String
Default: ""
RootVolumeType:
Description: Type of root volume to use
Type: String
Default: "gp3"
RootVolumeEncrypted:
Description: Indicates whether the EBS volume is encrypted
Type: String
AllowedValues:
- "true"
- "false"
Default: "false"
SecurityGroupId:
Type: String
Description: Optional - Comma separated list of security group ids to assign to instances
Default: ""
ImageId:
Type: String
Description: Optional - Custom AMI to use for instances (must be based on the stack's AMI)
Default: ""
ImageIdParameter:
Type: String
Description: Optional - Custom AMI SSM Parameter to use for instances (must be based on the stack's AMI)
Default: ""
ManagedPolicyARN:
Type: CommaDelimitedList
Description: Optional - Comma separated list of managed IAM policy ARNs to attach to the instance role
Default: ""
IMDSv2Tokens:
Type: String
Description: Whether IMDSv2 tokens must be used for the Instance Metadata Service.
AllowedValues:
- optional
- required
Default: optional
InstanceRoleName:
Type: String
Description: Optional - A name for the IAM Role attached to the Instance Profile
Default: ""
InstanceRolePermissionsBoundaryARN:
Type: String
Description: The ARN of the policy used to set the permissions boundary for the role.
Default: ""
InstanceOperatingSystem:
Type: String
Description: The operating system to run on the instances
AllowedValues:
- linux
- windows
Default: "linux"
ECRAccessPolicy:
Type: String
Description: ECR access policy to give container instances
AllowedValues:
- none
- readonly
- poweruser
- full
Default: "none"
AssociatePublicIpAddress:
Type: String
Description: Associate instances with public IP addresses
AllowedValues:
- "true"
- "false"
Default: "true"
EnableSecretsPlugin:
Type: String
Description: Enables s3-secrets plugin for all pipelines
AllowedValues:
- "true"
- "false"
Default: "true"
EnableECRPlugin:
Type: String
Description: Enables ecr plugin for all pipelines
AllowedValues:
- "true"
- "false"
Default: "true"
EnableDockerLoginPlugin:
Type: String
Description: Enables docker-login plugin for all pipelines
AllowedValues:
- "true"
- "false"
Default: "true"
EnableDockerUserNamespaceRemap:
Type: String
Description: Enables Docker user namespace remapping so docker runs as buildkite-agent
AllowedValues:
- "true"
- "false"
Default: "true"
EnableDockerExperimental:
Type: String
Description: Enables Docker experimental features
AllowedValues:
- "true"
- "false"
Default: "false"
EnableInstanceStorage:
Type: String
Description: Mount available NVMe Instance Storage at /mnt/ephemeral
AllowedValues:
- "true"
- "false"
Default: "false"
EnableCostAllocationTags:
Type: String
Description: Enables AWS Cost Allocation tags for all resources in the stack. See https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/cost-alloc-tags.html
AllowedValues:
- "true"
- "false"
Default: "false"
CostAllocationTagName:
Type: String
Description: The name of the Cost Allocation Tag used for billing purposes
Default: "CreatedBy"
CostAllocationTagValue:
Type: String
Description: The value of the Cost Allocation Tag used for billing purposes
Default: "buildkite-elastic-ci-stack-for-aws"
EnableAgentGitMirrorsExperiment:
Type: String
Description: Enables git-mirrors in the agent
AllowedValues:
- "true"
- "false"
Default: "false"
EnableDetailedMonitoring:
Type: String
Description: Enable detailed EC2 monitoring
AllowedValues:
- "true"
- "false"
Default: "false"
InstanceName:
Type: String
Description: Optional - Customise the EC2 instance Name tag
Default: "buildkite-agent"
Rules:
HasToken:
Assertions:
- Assert:
!Or
- !Not
- !Equals
- !Ref BuildkiteAgentToken
- ""
- !Not
- !Equals
- !Ref BuildkiteAgentTokenParameterStorePath
- ""
AssertDescription: "You must provide BuildkiteAgentToken or BuildkiteAgentTokenParameterStorePath"
Outputs:
VpcId:
Value:
!If [ CreateVpcResources, !Ref Vpc, !Ref VpcId ]
Export:
Name: !Sub '${AWS::StackName}-VpcId'
ManagedSecretsBucket:
Value:
!If [ CreateSecretsBucket, !Ref ManagedSecretsBucket, "Undefined" ]
Export:
Name: !Sub '${AWS::StackName}-ManagedSecretsBucket'
ManagedSecretsLoggingBucket:
Value:
!If [ CreateSecretsBucket, !Ref ManagedSecretsLoggingBucket, "Undefined" ]
Export:
Name: !Sub '${AWS::StackName}-ManagedSecretsLoggingBucket'
AutoScalingGroupName:
Value: !Ref AgentAutoScaleGroup
Export:
Name: !Sub '${AWS::StackName}-AutoScalingGroupName'
InstanceRoleName:
Value: !Ref IAMRole
Export:
Name: !Sub '${AWS::StackName}-InstanceRoleName'
Conditions:
SpotPriceSet:
!Not [ !Equals [ !Ref SpotPrice, 0 ] ]
CreateVpcResources:
!Equals [ !Ref VpcId, "" ]
CreateSecurityGroup:
!Equals [ !Ref SecurityGroupId, "" ]
CreateSecretsBucket:
!And
- !Equals [ !Ref EnableSecretsPlugin, "true"]
- !Equals [ !Ref SecretsBucket, "" ]
EnforceSecretsBucketEncryption:
!And
- !Condition CreateSecretsBucket
- !Equals [ !Ref SecretsBucketEncryption, "true"]
SetInstanceRoleName:
!Not [ !Equals [ !Ref InstanceRoleName, "" ] ]
SetInstanceRolePermissionsBoundaryARN:
!Not [ !Equals [ !Ref InstanceRolePermissionsBoundaryARN, "" ] ]
UseSpecifiedSecretsBucket:
!Not [ !Equals [ !Ref SecretsBucket, "" ] ]
HasSecretsBucket:
!Or [ !Condition CreateSecretsBucket, !Condition UseSpecifiedSecretsBucket ]
UseSpecifiedAvailabilityZones:
!Not [ !Equals [ !Join [ "", !Ref AvailabilityZones ], "" ] ]
UseArtifactsBucket:
!Not [ !Equals [ !Ref ArtifactsBucket, "" ] ]
HasImageId:
!Not [ !Equals [ !Ref ImageId, "" ] ]
HasImageIdParameter:
!Not [ !Equals [ !Ref ImageIdParameter, "" ] ]
UseDefaultInstanceCreationTimeout:
!Equals [ !Ref InstanceCreationTimeout, "" ]
UseDefaultRootVolumeName:
!Equals [ !Ref RootVolumeName, "" ]
UseInstanceType2:
!Not [ !Equals [ !Select [ "1", !Split [ ",", !Join [ ",", [ !Ref InstanceType, "", "", "" ] ] ] ], ""] ]
UseInstanceType3:
!Not [ !Equals [ !Select [ "2", !Split [ ",", !Join [ ",", [ !Ref InstanceType, "", "", "" ] ] ] ], ""] ]
UseInstanceType4:
!Not [ !Equals [ !Select [ "3", !Split [ ",", !Join [ ",", [ !Ref InstanceType, "", "", "" ] ] ] ], ""] ]
UseManagedPolicyARN:
!Not [ !Equals [ !Join [ "", !Ref ManagedPolicyARN ], "" ] ]
UseECR:
!Not [ !Equals [ !Ref ECRAccessPolicy, "none" ] ]
UseCustomerManagedParameterPath:
!Not [ !Equals [ !Ref BuildkiteAgentTokenParameterStorePath, "" ] ]
UseCustomerManagedKeyForParameterStore:
!Not [ !Equals [ !Ref BuildkiteAgentTokenParameterStoreKMSKey, "" ] ]
CreateAgentTokenParameter:
!Equals [ !Ref BuildkiteAgentTokenParameterStorePath, "" ]
HasVariableSize:
!Not [ !Equals [ !Ref MaxSize, !Ref MinSize ] ]
UseCostAllocationTags:
!Equals [ !Ref EnableCostAllocationTags, "true" ]
HasKeyName:
!Not [ !Equals [ !Ref KeyName, "" ] ]
EnableSshIngress:
!And
- { Condition : CreateSecurityGroup }
# Enable ingress if a key can be specified another way
- !Or
- { Condition: HasKeyName }
- !Not [ !Equals [ !Ref AuthorizedUsersUrl, "" ] ]
# Whether or not there's any managed polices to attach
HasManagedPolicies:
!Or [ { Condition: UseManagedPolicyARN }, { Condition: UseECR } ]
UseWindowsAgents:
!Equals [ !Ref InstanceOperatingSystem, "windows" ]
UseLinuxAgents:
!Equals [ !Ref InstanceOperatingSystem, "linux" ]
# Unfortunately, Cloudformation's !Or intrinsic function only accepts
# between 2 and 10 arguments. To get around this, we're grouping the
# instance families in sub-conditionals. At least this doesn't force us
# into using a Custom Resource.
UsingArmInstances:
!Or
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "a1" ]
- !Or
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "c6g" ]
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "c6gd" ]
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "c6gn" ]
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "c7g" ]
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "c7gn" ]
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "g5g" ]
- !Or
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "Im4gn" ]
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "Is4gen" ]
- !Or
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "m6g" ]
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "m6gd" ]
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "m7g" ]
- !Or
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "r6g" ]
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "r6gd" ]
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "r7g" ]
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "t4g" ]
- !Equals [ !Select [ 0, !Split [ ".", !Ref InstanceType ] ], "x2gd" ]
Mappings:
ECRManagedPolicy:
none : { Policy: '' }
readonly : { Policy: 'arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly' }
poweruser : { Policy: 'arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryPowerUser' }
full : { Policy: 'arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryFullAccess' }
# Generated from Makefile via build/mappings.yml
AWSRegion2AMI: { linuxamd64: !Ref ImageId, linuxarm64: !Ref ImageId, windows: !Ref ImageId }
Resources:
Vpc:
Type: AWS::EC2::VPC
Condition: CreateVpcResources
Properties:
CidrBlock: 10.0.0.0/16
InstanceTenancy: default
Tags:
- Key: Name
Value: !Ref 'AWS::StackName'
Gateway:
Type: AWS::EC2::InternetGateway
Condition: CreateVpcResources
Properties:
Tags:
- Key: Name
Value: !Ref 'AWS::StackName'
GatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Condition: CreateVpcResources
Properties:
InternetGatewayId: !Ref Gateway
VpcId: !Ref Vpc
Subnet0:
Type: AWS::EC2::Subnet
Condition: CreateVpcResources
DependsOn:
- GatewayAttachment
Properties:
AvailabilityZone:
!If
- "UseSpecifiedAvailabilityZones"
- !Select [ 0, !Ref AvailabilityZones ]
- !Select [ 0, !GetAZs '' ]
CidrBlock: 10.0.1.0/24
VpcId: !Ref Vpc
Tags:
- Key: Name
Value: !Ref 'AWS::StackName'
Subnet1:
Type: AWS::EC2::Subnet
Condition: CreateVpcResources
DependsOn:
- GatewayAttachment
Properties:
AvailabilityZone:
!If
- "UseSpecifiedAvailabilityZones"
- !Select [ 1, !Ref AvailabilityZones ]
- !Select [ 1, !GetAZs '' ]
CidrBlock: 10.0.2.0/24
VpcId: !Ref Vpc
Tags:
- Key: Name
Value: !Ref 'AWS::StackName'
Routes:
Type: AWS::EC2::RouteTable
Condition: CreateVpcResources
Properties:
VpcId: !Ref Vpc
Tags:
- Key: Name
Value: !Ref 'AWS::StackName'
RouteDefault:
Type: AWS::EC2::Route
Condition: CreateVpcResources
DependsOn:
- GatewayAttachment
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref Gateway
RouteTableId: !Ref Routes
Subnet0Routes:
Type: AWS::EC2::SubnetRouteTableAssociation
Condition: CreateVpcResources
Properties:
SubnetId: !Ref Subnet0
RouteTableId: !Ref Routes
Subnet1Routes:
Type: AWS::EC2::SubnetRouteTableAssociation
Condition: CreateVpcResources
Properties:
SubnetId: !Ref Subnet1
RouteTableId: !Ref Routes
# A resource that depends on the leaf nodes of the VPC configuration so that
# a strict ordering can be established on teardown.
VpcComplete:
Type: AWS::CloudFormation::WaitConditionHandle
Metadata:
VpcResources: !If
- CreateVpcResources
- [ !Ref RouteDefault, !Ref Subnet0Routes, !Ref Subnet1Routes ]
- !Ref "AWS::NoValue"
BuildkiteAgentTokenParameter:
Type: AWS::SSM::Parameter
Condition: CreateAgentTokenParameter
Properties:
Name: !Sub "/${AWS::StackName}/buildkite/agent-token"
Type: String
Value: !Ref BuildkiteAgentToken
# Allow ec2 instances to assume a role and be granted the IAMPolicies
IAMInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles: [ !Ref IAMRole ]
IAMRole:
Type: AWS::IAM::Role
Properties:
RoleName: !If [ SetInstanceRoleName, !Ref InstanceRoleName, !Sub "${AWS::StackName}-Role" ]
PermissionsBoundary: !If [ SetInstanceRolePermissionsBoundaryARN, !Ref InstanceRolePermissionsBoundaryARN, !Ref "AWS::NoValue" ]
ManagedPolicyArns: !If
- HasManagedPolicies
# Support multiple policies to attach by merging the values together and splitting on ','
- !Split
- ','
# Join will skip over AWS::NoValue values
- !Join
- ','
- - !If
- UseECR
- !FindInMap [ ECRManagedPolicy, !Ref ECRAccessPolicy, 'Policy' ]
- !Ref 'AWS::NoValue'
# This may support multiple values of its own (separated by commas)
- !If
- UseManagedPolicyARN
- !Join [ ',', !Ref ManagedPolicyARN ]
- !Ref 'AWS::NoValue'
- !Ref 'AWS::NoValue'
Policies:
- !If
- UseCustomerManagedKeyForParameterStore
- PolicyName: DecryptAgentToken
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- kms:Decrypt
Resource: !Sub arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/${BuildkiteAgentTokenParameterStoreKMSKey}
- !Ref 'AWS::NoValue'
- PolicyName: ReadAgentToken
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: ssm:GetParameter
Resource:
!Sub
- arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter${ParameterPath}
- ParameterPath: !If [ UseCustomerManagedParameterPath, !Ref BuildkiteAgentTokenParameterStorePath, !Ref BuildkiteAgentTokenParameter ]
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: [ autoscaling.amazonaws.com, ec2.amazonaws.com ]
Action: sts:AssumeRole
Path: /
IAMPolicies:
Type: AWS::IAM::Policy
Properties:
PolicyName: InstancePolicy
PolicyDocument:
Statement:
- !If
- HasSecretsBucket
- Sid: SecretsBucket
Effect: Allow
Action:
- s3:Get*
- s3:List*
Resource:
- !Sub
- "arn:aws:s3:::${Bucket}/*"
- Bucket: !If [ CreateSecretsBucket, !Ref ManagedSecretsBucket, !Ref SecretsBucket ]
- !Sub
- "arn:aws:s3:::${Bucket}"
- Bucket: !If [ CreateSecretsBucket, !Ref ManagedSecretsBucket, !Ref SecretsBucket ]
- !Ref "AWS::NoValue"
- !If
- UseArtifactsBucket
- Sid: ArtifactsBucket
Effect: Allow
Action:
- s3:GetObject
- s3:GetObjectAcl
- s3:GetObjectVersion
- s3:GetObjectVersionAcl
- s3:ListBucket
- s3:PutObject
- s3:PutObjectAcl
- s3:PutObjectVersionAcl
Resource:
- !Sub "arn:aws:s3:::${ArtifactsBucket}/*"
- !Sub "arn:aws:s3:::${ArtifactsBucket}"
- !Ref "AWS::NoValue"
- Effect: Allow
Action:
- autoscaling:DescribeAutoScalingInstances
- cloudwatch:PutMetricData
- cloudformation:DescribeStackResource
- ec2:DescribeTags
Resource: "*"
- Sid: TerminateInstance
Effect: Allow
Action:
- autoscaling:SetInstanceHealth
- autoscaling:TerminateInstanceInAutoScalingGroup
Resource: !Sub arn:${AWS::Partition}:autoscaling:${AWS::Region}:${AWS::AccountId}:autoScalingGroup:*:autoScalingGroupName/${AWS::StackName}-AgentAutoScaleGroup-*
- Sid: Logging
Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- logs:DescribeLogGroups
- logs:DescribeLogStreams
Resource: "*"
- Sid: Ssm
Effect: Allow
Action:
- ssm:DescribeInstanceProperties
- ssm:ListAssociations
- ssm:PutInventory
- ssm:UpdateInstanceInformation
- ssmmessages:CreateControlChannel
- ssmmessages:CreateDataChannel
- ssmmessages:OpenControlChannel
- ssmmessages:OpenDataChannel
- ec2messages:AcknowledgeMessage
- ec2messages:DeleteMessage
- ec2messages:FailMessage
- ec2messages:GetEndpoint
- ec2messages:GetMessages
- ec2messages:SendReply
Resource: "*"
Roles:
- !Ref IAMRole
ManagedSecretsLoggingBucket:
Type: AWS::S3::Bucket
Condition: CreateSecretsBucket
DeletionPolicy: Retain
Properties:
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
BucketEncryption:
!If
- EnforceSecretsBucketEncryption
-
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: "AES256"
- !Ref "AWS::NoValue"
Tags:
- !If
- UseCostAllocationTags
- Key: !Ref CostAllocationTagName
Value: !Ref CostAllocationTagValue
- !Ref "AWS::NoValue"
ManagedSecretsLoggingBucketPolicy:
Type: AWS::S3::BucketPolicy
Condition: CreateSecretsBucket
Properties:
Bucket: !Ref ManagedSecretsLoggingBucket
PolicyDocument:
Id: ManagedSecretsLoggingBucketPolicy
Version: "2012-10-17"
Statement:
- Sid: S3ServerAccessLogsPolicy # Grant permissions to the logging service principal using a bucket policy (https://docs.aws.amazon.com/AmazonS3/latest/userguide/enable-server-access-logging.html)
Effect: Allow
Principal:
Service: 'logging.s3.amazonaws.com'
Action:
- 's3:PutObject'
Resource:
- !GetAtt 'ManagedSecretsLoggingBucket.Arn'
- !Sub '${ManagedSecretsLoggingBucket.Arn}/*'
Condition:
ArnLike:
'aws:SourceArn': !GetAtt 'ManagedSecretsBucket.Arn'
StringEquals:
'aws:SourceAccount': !Ref 'AWS::AccountId'
- !If
- EnforceSecretsBucketEncryption
- Sid: AllowSSLRequestsOnly # AWS Foundational Security Best Practices v1.0.0 S3.5
Effect: Deny
Principal: '*'
Action: 's3:*'
Resource:
- !GetAtt 'ManagedSecretsLoggingBucket.Arn'
- !Sub '${ManagedSecretsLoggingBucket.Arn}/*'
Condition:
Bool: