Skip to content

Commit 3cff6ac

Browse files
author
James Healy
authored
Merge pull request #710 from buildkite/mixed-instance-asg
Support multiple instance types by switching to a Mixed Instance ASG
2 parents 061e549 + e2b54ee commit 3cff6ac

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

templates/aws-stack.yml

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Metadata:
5757
Parameters:
5858
- MinSize
5959
- MaxSize
60+
- OnDemandPercentage
6061
- ScaleOutFactor
6162
- ScaleInIdlePeriod
6263
- ScaleOutForWaitingJobs
@@ -204,13 +205,15 @@ Parameters:
204205
Default: ""
205206

206207
InstanceType:
207-
Description: Instance type
208+
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).
208209
Type: String
209210
Default: t3.large
210211
MinLength: 1
212+
AllowedPattern: "^[\\w\\.]+(,[\\w\\.]*){0,3}$"
213+
ConstraintDescription: "must contain 1-4 instance types separated by commas. No space before/after the comma."
211214

212215
SpotPrice:
213-
Description: Spot bid price to use for the instances. 0 means normal (non-spot) instances
216+
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.
214217
Type: String
215218
Default: 0
216219

@@ -225,6 +228,13 @@ Parameters:
225228
Type: Number
226229
Default: 0
227230

231+
OnDemandPercentage:
232+
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.
233+
Type: Number
234+
Default: 100
235+
MinValue: 0
236+
MaxValue: 100
237+
228238
ScaleOutFactor:
229239
Description: A decimal factor to apply to scale out changes to speed up or slow down scale-out
230240
Type: Number
@@ -411,7 +421,7 @@ Outputs:
411421
Name: !Sub '${AWS::StackName}-InstanceRoleName'
412422

413423
Conditions:
414-
UseSpotInstances:
424+
SpotPriceSet:
415425
!Not [ !Equals [ !Ref SpotPrice, 0 ] ]
416426

417427
CreateVpcResources:
@@ -446,6 +456,15 @@ Conditions:
446456
UseDefaultRootVolumeName:
447457
!Equals [ !Ref RootVolumeName, "" ]
448458

459+
UseInstanceType2:
460+
!Not [ !Equals [ !Select [ "1", !Split [ ",", !Join [ ",", [ !Ref InstanceType, "", "", "" ] ] ] ], ""] ]
461+
462+
UseInstanceType3:
463+
!Not [ !Equals [ !Select [ "2", !Split [ ",", !Join [ ",", [ !Ref InstanceType, "", "", "" ] ] ] ], ""] ]
464+
465+
UseInstanceType4:
466+
!Not [ !Equals [ !Select [ "3", !Split [ ",", !Join [ ",", [ !Ref InstanceType, "", "", "" ] ] ] ], ""] ]
467+
449468
UseManagedPolicyARN:
450469
!Not [ !Equals [ !Join [ "", !Ref ManagedPolicyARN ], "" ] ]
451470

@@ -803,13 +822,7 @@ Resources:
803822
KeyName: !If [ "HasKeyName", !Ref KeyName, !Ref 'AWS::NoValue' ]
804823
IamInstanceProfile:
805824
Arn: !GetAtt "IAMInstanceProfile.Arn"
806-
InstanceType: !Ref InstanceType
807-
InstanceMarketOptions: !If
808-
- UseSpotInstances
809-
- MarketType: spot
810-
SpotOptions:
811-
MaxPrice: !Ref SpotPrice
812-
- !Ref "AWS::NoValue"
825+
InstanceType: !Select [ "0", !Split [ ",", !Join [ ",", [ !Ref InstanceType, "", "", "" ] ] ] ]
813826
ImageId: !If [ HasImageId, !Ref ImageId, !If [ HasImageIdParameter, !GetAtt ImageIdParameterStack.Outputs.ImageId, !FindInMap [ AWSRegion2AMI, !Ref 'AWS::Region', !Ref InstanceOperatingSystem ] ] ]
814827
BlockDeviceMappings:
815828
- DeviceName: !If [ UseDefaultRootVolumeName, !If [ UseWindowsAgents, /dev/sda1, /dev/xvda ], !Ref RootVolumeName ]
@@ -914,9 +927,29 @@ Resources:
914927
Type: AWS::AutoScaling::AutoScalingGroup
915928
Properties:
916929
VPCZoneIdentifier: !If [ "CreateVpcResources", [ !Ref Subnet0, !Ref Subnet1 ], !Ref Subnets ]
917-
LaunchTemplate:
918-
LaunchTemplateId: !Ref AgentLaunchTemplate
919-
Version: !GetAtt "AgentLaunchTemplate.LatestVersionNumber"
930+
MixedInstancesPolicy:
931+
InstancesDistribution:
932+
OnDemandPercentageAboveBaseCapacity: !If [ SpotPriceSet, 0, !Ref OnDemandPercentage ]
933+
SpotAllocationStrategy: capacity-optimized
934+
SpotMaxPrice: !If [SpotPriceSet, !Ref SpotPrice, !Ref "AWS::NoValue"]
935+
LaunchTemplate:
936+
LaunchTemplateSpecification:
937+
LaunchTemplateId: !Ref AgentLaunchTemplate
938+
Version: !GetAtt "AgentLaunchTemplate.LatestVersionNumber"
939+
Overrides:
940+
- InstanceType: !Select [ "0", !Split [ ",", !Join [ ",", [ !Ref InstanceType, "", "", "" ] ] ] ]
941+
- InstanceType: !If
942+
- UseInstanceType2
943+
- !Select [ "1", !Split [ ",", !Join [ ",", [ !Ref InstanceType, "", "", "" ] ] ] ]
944+
- !Ref "AWS::NoValue"
945+
- InstanceType: !If
946+
- UseInstanceType3
947+
- !Select [ "2", !Split [ ",", !Join [ ",", [ !Ref InstanceType, "", "", "" ] ] ] ]
948+
- !Ref "AWS::NoValue"
949+
- InstanceType: !If
950+
- UseInstanceType4
951+
- !Select [ "3", !Split [ ",", !Join [ ",", [ !Ref InstanceType, "", "", "" ] ] ] ]
952+
- !Ref "AWS::NoValue"
920953
MinSize: !Ref MinSize
921954
MaxSize: !Ref MaxSize
922955
Cooldown: 0

0 commit comments

Comments
 (0)