Skip to content

Commit

Permalink
feat(aws-autoscaling): allow setting spotPrice
Browse files Browse the repository at this point in the history
Fixes aws#2208.
  • Loading branch information
SoManyHs committed May 17, 2019
1 parent cfe46f6 commit 52b3fa2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ export interface AutoScalingGroupProps extends CommonAutoScalingGroupProps {
* @default A role will automatically be created, it can be accessed via the `role` property
*/
readonly role?: iam.IRole;

/**
* The maximum hourly price to be paid for any Spot Instance launched to fulfill the request. Spot Instances are
* launched when the price you specify exceeds the current Spot market price.
*
* @default none
*/
readonly spotPrice?: string;
}

abstract class AutoScalingGroupBase extends Resource implements IAutoScalingGroup {
Expand Down Expand Up @@ -369,6 +377,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
iamInstanceProfile: iamProfile.ref,
userData: userDataToken,
associatePublicIpAddress: props.associatePublicIpAddress,
spotPrice: props.spotPrice,
});

launchConfig.node.addDependency(this.role);
Expand Down
28 changes: 28 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ export = {

test.done();
},

'can add Security Group to Fleet'(test: Test) {
// GIVEN
const stack = new cdk.Stack(undefined, 'MyStack', { env: { region: 'us-east-1', account: '1234' }});
Expand All @@ -363,6 +364,7 @@ export = {
}));
test.done();
},

'can set tags'(test: Test) {
// GIVEN
const stack = getTestStack();
Expand Down Expand Up @@ -405,6 +407,29 @@ export = {
}));
test.done();
},

'allows setting spot price'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = mockVpc(stack);

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyStack', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
machineImage: new ec2.AmazonLinuxImage(),
vpc,

spotPrice: "0.05",
});

// THEN
expect(stack).to(haveResource("AWS::AutoScaling::LaunchConfiguration", {
SpotPrice: "0.05",
}));

test.done();
},

'allows association of public IP address'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand All @@ -430,6 +455,7 @@ export = {
));
test.done();
},

'association of public IP address requires public subnet'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand All @@ -449,6 +475,7 @@ export = {
});
test.done();
},

'allows disassociation of public IP address'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand All @@ -472,6 +499,7 @@ export = {
));
test.done();
},

'does not specify public IP address association by default'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-ecs/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,14 @@ export interface AddCapacityOptions extends AddAutoScalingGroupCapacityOptions,
* The type of EC2 instance to launch into your Autoscaling Group
*/
readonly instanceType: ec2.InstanceType;

/**
* The maximum hourly price to be paid for any Spot Instance launched to fulfill the request. Spot Instances are
* launched when the price you specify exceeds the current Spot market price.
*
* @default none
*/
readonly spotPrice?: string;
}

export interface NamespaceOptions {
Expand Down
20 changes: 20 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,26 @@ export = {
test.done();
},

"allows specifying spot fleet"(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.VpcNetwork(stack, 'MyVpc', {});

const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', {
instanceType: new ec2.InstanceType('t2.micro'),
spotPrice: "0.31"
});

// THEN
expect(stack).to(haveResource("AWS::AutoScaling::LaunchConfiguration", {
SpotPrice: "0.31"
}));

test.done();
},


"allows adding default service discovery namespace"(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit 52b3fa2

Please sign in to comment.