forked from buildkite/elastic-ci-stack-for-aws
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbootstrap.yml
513 lines (453 loc) · 13.9 KB
/
bootstrap.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
---
AWSTemplateFormatVersion: 2010-09-09
Resources:
Vpc:
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock: 10.0.0.0/16
Tags:
- Key: Name
Value: !Ref AWS::StackName
Gateway:
Type: 'AWS::EC2::InternetGateway'
Properties:
Tags:
- Key: Name
Value: !Ref AWS::StackName
GatewayAttachment:
Type: 'AWS::EC2::VPCGatewayAttachment'
DependsOn: Gateway
Properties:
InternetGatewayId: !Ref Gateway
VpcId: !Ref Vpc
Eip0:
Type: 'AWS::EC2::EIP'
DependsOn: Vpc
Eip1:
Type: 'AWS::EC2::EIP'
DependsOn: Vpc
PrivateSubnet0:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone: !Ref AvailabilityZone0
CidrBlock: 10.0.0.0/22
MapPublicIpOnLaunch: false
VpcId: !Ref Vpc
Tags:
- Key: Name
Value: !Sub ${AWS::StackName} Private 0
PrivateSubnet1:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone: !Ref AvailabilityZone1
CidrBlock: 10.0.4.0/22
MapPublicIpOnLaunch: false
VpcId: !Ref Vpc
Tags:
- Key: Name
Value: !Sub ${AWS::StackName} Private 1
PublicSubnet0:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone: !Ref AvailabilityZone0
CidrBlock: 10.0.255.235/28
MapPublicIpOnLaunch: true
VpcId: !Ref Vpc
Tags:
- Key: Name
Value: !Sub ${AWS::StackName} Public 0
PublicSubnet1:
Type: 'AWS::EC2::Subnet'
Properties:
AvailabilityZone: !Ref AvailabilityZone1
CidrBlock: 10.0.255.240/28
MapPublicIpOnLaunch: true
VpcId: !Ref Vpc
Tags:
- Key: Name
Value: !Sub ${AWS::StackName} Public 1
PrivateRoutes0:
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref Vpc
Tags:
- Key: Name
Value: !Sub ${AWS::StackName} Private 0
PrivateRoutes1:
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref Vpc
Tags:
- Key: Name
Value: !Sub ${AWS::StackName} Private 1
PublicRoutes0:
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref Vpc
Tags:
- Key: Name
Value: !Sub ${AWS::StackName} Public 0
PublicRoutes1:
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref Vpc
Tags:
- Key: Name
Value: !Sub ${AWS::StackName} Public 1
PrivateRouteDefault0:
Type: 'AWS::EC2::Route'
Properties:
DestinationCidrBlock: 0.0.0.0/0
RouteTableId: !Ref PrivateRoutes0
NatGatewayId: !Ref Nat0
PrivateRouteDefault1:
Type: 'AWS::EC2::Route'
Properties:
DestinationCidrBlock: 0.0.0.0/0
RouteTableId: !Ref PrivateRoutes1
NatGatewayId: !Ref Nat1
PublicRouteDefault0:
Type: 'AWS::EC2::Route'
Properties:
DestinationCidrBlock: 0.0.0.0/0
RouteTableId: !Ref PublicRoutes0
GatewayId: !Ref Gateway
PublicRouteDefault1:
Type: 'AWS::EC2::Route'
Properties:
DestinationCidrBlock: 0.0.0.0/0
RouteTableId: !Ref PublicRoutes1
GatewayId: !Ref Gateway
PrivateRoutesAssociation0:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref PrivateSubnet0
RouteTableId: !Ref PrivateRoutes0
PrivateRoutesAssociation1:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref PrivateSubnet1
RouteTableId: !Ref PrivateRoutes1
PublicRoutesAssociation0:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref PublicSubnet0
RouteTableId: !Ref PublicRoutes0
PublicRoutesAssociation1:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
SubnetId: !Ref PublicSubnet1
RouteTableId: !Ref PublicRoutes1
Nat0:
Type: 'AWS::EC2::NatGateway'
DependsOn:
- PublicRouteDefault0
- PublicRoutesAssociation0
Properties:
SubnetId: !Ref PublicSubnet0
AllocationId: !GetAtt Eip0.AllocationId
Nat1:
Type: 'AWS::EC2::NatGateway'
DependsOn:
- PublicRouteDefault1
- PublicRoutesAssociation1
Properties:
SubnetId: !Ref PublicSubnet1
AllocationId: !GetAtt Eip1.AllocationId
SecretsBucket:
Type: 'AWS::S3::Bucket'
Properties:
LoggingConfiguration:
DestinationBucketName: !Ref SecretsLoggingBucket
VersioningConfiguration:
Status: Enabled
Tags:
- Key: Name
Value: !Sub ${AWS::StackName} Secrets
SecretsLoggingBucket:
Type: 'AWS::S3::Bucket'
Properties:
AccessControl: LogDeliveryWrite
Tags:
- Key: Name
Value: !Sub ${AWS::StackName} Secrets Access Logs
ArtifactsBucket:
Type: 'AWS::S3::Bucket'
Properties:
LifecycleConfiguration:
Rules:
- Id: ObjectTimeToLive
Status: Enabled
Prefix: '*'
ExpirationInDays: !Ref ArtifactLifespan
Tags:
- Key: Name
Value: !Sub ${AWS::StackName} Artifacts
DefaultManagedPolicy:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
ManagedPolicyName: DefaultManagedPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: s3:PutObject
Resource: !Sub 'arn:aws:s3:::${SecretsBucket}/terraform/*'
Condition:
StringEquals:
s3:x-amz-server-side-encryption: [ 'AES256', 'true' ]
- !If
- AssumeRole
- Effect: Allow
Action: sts:AssumeRole
Resource: !Ref AccountRoles
- !Ref AWS::NoValue
PackerManagedPolicy:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
ManagedPolicyName: PackerManagedPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ec2:AttachVolume
- ec2:AuthorizeSecurityGroupIngress
- ec2:CopyImage
- ec2:CreateImage
- ec2:CreateKeypair
- ec2:CreateSecurityGroup
- ec2:CreateSnapshot
- ec2:CreateTags
- ec2:CreateVolume
- ec2:DeleteKeypair
- ec2:DeleteSecurityGroup
- ec2:DeleteSnapshot
- ec2:DeleteVolume
- ec2:DeregisterImage
- ec2:DescribeImageAttribute
- ec2:DescribeImages
- ec2:DescribeInstanceStatus
- ec2:DescribeInstances
- ec2:DescribeKeyPairs
- ec2:DescribeRegions
- ec2:DescribeSecurityGroups
- ec2:DescribeSnapshots
- ec2:DescribeSubnets
- ec2:DescribeTags
- ec2:DescribeVolumes
- ec2:DetachVolume
- ec2:GetPasswordData
- ec2:ModifyImageAttribute
- ec2:ModifyInstanceAttribute
- ec2:ModifySnapshotAttribute
- ec2:RegisterImage
- ec2:RunInstances
- ec2:StopInstances
- ec2:TerminateInstances
Resource: '*'
Condition:
StringEqualsIfExists:
ec2:Region: !Ref AWS::Region
ec2:Vpc: !Sub arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc/${PackerAllowedVpc}
ec2:Subnet: !Sub arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:subnet/${PackerAllowedSubnet}
ec2:InstanceType: !Ref PackerAllowedInstanceTypes
Default:
Type: 'AWS::CloudFormation::Stack'
Properties:
TemplateURL: !Sub https://s3.amazonaws.com/buildkite-aws-stack/${BuildkiteStackVersion}/aws-stack.yml
Parameters:
AgentsPerInstance: 1
ArtifactsBucket: !Ref ArtifactsBucket
AssociatePublicIpAddress: false
BootstrapScriptUrl: !Ref BootstrapScriptUrl
BuildkiteAgentRelease: !Ref BuildkiteAgentRelease
BuildkiteAgentToken: !Ref BuildkiteAgentToken
BuildkiteQueue: default
InstanceType: !Ref DefaultQueueInstanceType
KeyName: !Ref KeyName
ManagedPolicyARN: !Ref DefaultManagedPolicy
MaxSize: !Ref DefaultQueueMaxSize
MinSize: !Ref DefaultQueueMinSize
RootVolumeSize: !Ref DefaultQueueRootVolumeSize
ScaleDownAdjustment: -1
ScaleUpAdjustment: 5
SecretsBucket: !Ref SecretsBucket
SpotPrice: !Ref DefaultQueueSpotPrice
Subnets: !Join [ ',', [ !Ref PrivateSubnet0, !Ref PrivateSubnet1 ] ]
VpcId: !Ref Vpc
Build:
Type: 'AWS::CloudFormation::Stack'
Properties:
TemplateURL: !Sub https://s3.amazonaws.com/buildkite-aws-stack/${BuildkiteStackVersion}/aws-stack.yml
Parameters:
AgentsPerInstance: !Ref BuildQueueAgentsPerInstance
ArtifactsBucket: !Ref ArtifactsBucket
AssociatePublicIpAddress: false
BootstrapScriptUrl: !Ref BootstrapScriptUrl
BuildkiteAgentRelease: !Ref BuildkiteAgentRelease
BuildkiteAgentToken: !Ref BuildkiteAgentToken
BuildkiteQueue: build
InstanceType: !Ref BuildQueueInstanceType
KeyName: !Ref KeyName
MaxSize: 1
MinSize: 1
RootVolumeSize: !Ref BuildQueueRootVolumeSize
ScaleDownAdjustment: -1
ScaleUpAdjustment: 5
SecretsBucket: !Ref SecretsBucket
SpotPrice: !Ref BuildQueueSpotPrice
Subnets: !Join [ ',', [ !Ref PrivateSubnet0, !Ref PrivateSubnet1 ] ]
VpcId: !Ref Vpc
Packer:
Type: 'AWS::CloudFormation::Stack'
Properties:
TemplateURL: !Sub https://s3.amazonaws.com/buildkite-aws-stack/${BuildkiteStackVersion}/aws-stack.yml
Parameters:
AgentsPerInstance: 1
ArtifactsBucket: !Ref ArtifactsBucket
AssociatePublicIpAddress: false
BootstrapScriptUrl: !Ref BootstrapScriptUrl
BuildkiteAgentRelease: !Ref BuildkiteAgentRelease
BuildkiteAgentToken: !Ref BuildkiteAgentToken
BuildkiteQueue: packer
InstanceType: !Ref PackerQueueInstanceType
KeyName: !Ref KeyName
ManagedPolicyARN: !Ref PackerManagedPolicy
MaxSize: 1
MinSize: 0
RootVolumeSize: !Ref PackerQueueRootVolumeSize
ScaleDownAdjustment: -1
ScaleUpAdjustment: 5
SecretsBucket: !Ref SecretsBucket
SpotPrice: !Ref PackerQueueSpotPrice
Subnets: !Join [ ',', [ !Ref PrivateSubnet0, !Ref PrivateSubnet1 ] ]
VpcId: !Ref Vpc
Outputs:
VpcId:
Value: !Ref Vpc
Export:
Name: !Sub ${AWS::StackName}-VpcId
Subnets:
Value: !Join [ ',', [ !Ref PrivateSubnet0, !Ref PrivateSubnet1 ] ]
Export:
Name: !Sub ${AWS::StackName}-Subnets
SecretsBucket:
Value: !Ref SecretsBucket
Export:
Name: !Sub ${AWS::StackName}-SecretsBucket
ArtifactsBucket:
Value: !Ref ArtifactsBucket
Export:
Name: !Sub ${AWS::StackName}-ArtifactsBucket
KeyName:
Value: !Ref KeyName
Export:
Name: !Sub ${AWS::StackName}-KeyName
Parameters:
ArtifactLifespan:
Type: Number
Default: 7
MinValue: 1
Description: How long to keep artifacts in days
BuildkiteAgentToken:
Type: String
Description: Buildkite agent token
NoEcho: true
MinLength: 1
BuildkiteStackVersion:
Type: String
Description: 'The stack version to use. Default is latest.'
Default: latest
MinLength: 1
BuildkiteAgentRelease:
Type: String
Default: stable
AllowedValues:
- stable
- beta
- edge
KeyName:
Type: 'AWS::EC2::KeyPair::KeyName'
DefaultQueueInstanceType:
Type: String
Description: Instance type
Default: t2.nano
MinLength: 1
DefaultQueueMinSize:
Type: Number
Description: Minimum number of instances for default queue
Default: 0
MinValue: 0
DefaultQueueMaxSize:
Type: Number
Description: Maximum number of instances for default queue
Default: 10
MinValue: 1
DefaultQueueRootVolumeSize:
Type: Number
Description: Size of each instance's root EBS volume (in GB)
Default: 50
MinValue: 50
DefaultQueueSpotPrice:
Type: Number
Description: The price for spot instances. Leave blank to not use spot instances.
AccountRoles:
Type: 'CommaDelimitedList'
Description: A comma separated list roles that can be assumed as
Default: ''
BuildQueueInstanceType:
Type: String
Description: Instance type
Default: t2.nano
MinLength: 1
BuildQueueAgentsPerInstance:
Type: Number
Description: Number of Buildkite agents to run on each the build instance
Default: 1
MinValue: 1
BuildQueueSpotPrice:
Type: Number
Description: The price for spot instances. Leave blank to not use spot instances.
BuildQueueRootVolumeSize:
Type: Number
Description: Size of each instance's root EBS volume (in GB)
Default: 50
MinValue: 50
PackerQueueInstanceType:
Type: String
Description: Instance type
Default: t2.nano
MinLength: 1
PackerQueueSpotPrice:
Type: Number
Description: The price for spot instances. Leave blank to not use spot instances.
PackerQueueRootVolumeSize:
Type: Number
Description: Size of each instance's root EBS volume (in GB)
Default: 50
MinValue: 50
PackerAllowedVpc:
Type: 'AWS::EC2::VPC::Id'
Description: The VPC permitted to launch EC2 instances
PackerAllowedSubnet:
Type: 'AWS::EC2::Subnet::Id'
Description: The subnet permitted to launch EC2 instances
PackerAllowedInstanceTypes:
Type: CommaDelimitedList
Description: The EC2 instance types permitted
Default: t2.nano
AvailabilityZone0:
Type: 'AWS::EC2::AvailabilityZone::Name'
Description: The availability zone for the first subnet
Default: us-east-1a
AvailabilityZone1:
Type: 'AWS::EC2::AvailabilityZone::Name'
Description: The availability zone for the second subnet
Default: us-east-1b
BootstrapScriptUrl:
Type: String
Description: A script that is executed on boot on each instance
Conditions:
AssumeRole: !Not
- !Equals [ !Join [ ',', !Ref AccountRoles ], '' ]