From b0f9827eab224a08d0a6b291c1a400299ebabd78 Mon Sep 17 00:00:00 2001 From: watany <76135106+watany-dev@users.noreply.github.com> Date: Mon, 5 Dec 2022 23:55:46 +0900 Subject: [PATCH] chore(autoscaling): add price-capacity-optimized (#23232) add price-capacity-optimized to SpotAllocationStrategy enum ref https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-autoscaling-autoscalinggroup-instancesdistribution.html#cfn-autoscaling-autoscalinggroup-instancesdistribution-spotallocationstrategy fixed https://github.com/aws/aws-cdk/issues/23226 ---- ### All Submissions: * [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Construct Runtime Dependencies: * [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../aws-autoscaling/lib/auto-scaling-group.ts | 7 ++++ .../test/auto-scaling-group.test.ts | 41 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts b/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts index d3ca3fee08be5..df442537b7232 100644 --- a/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts +++ b/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts @@ -408,6 +408,13 @@ export enum SpotAllocationStrategy { * honors the instance type priorities on a best-effort basis but optimizes for capacity first. */ CAPACITY_OPTIMIZED_PRIORITIZED = 'capacity-optimized-prioritized', + + /** + * The price and capacity optimized allocation strategy looks at both price and + * capacity to select the Spot Instance pools that are the least likely to be + * interrupted and have the lowest possible price. + */ + PRICE_CAPACITY_OPTIMIZED = 'price-capacity-optimized', } /** diff --git a/packages/@aws-cdk/aws-autoscaling/test/auto-scaling-group.test.ts b/packages/@aws-cdk/aws-autoscaling/test/auto-scaling-group.test.ts index 13e735b1e0a0a..48062024b9fb4 100644 --- a/packages/@aws-cdk/aws-autoscaling/test/auto-scaling-group.test.ts +++ b/packages/@aws-cdk/aws-autoscaling/test/auto-scaling-group.test.ts @@ -1915,6 +1915,47 @@ test('can use Vpc imported from unparseable list tokens', () => { }); }); +test('add price-capacity-optimized', () => { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + const lt = LaunchTemplate.fromLaunchTemplateAttributes(stack, 'imported-lt', { + launchTemplateId: 'test-lt-id', + versionNumber: '0', + }); + + new autoscaling.AutoScalingGroup(stack, 'mip-asg', { + mixedInstancesPolicy: { + launchTemplate: lt, + launchTemplateOverrides: [{ + instanceType: new InstanceType('t4g.micro'), + launchTemplate: lt, + weightedCapacity: 9, + }], + instancesDistribution: { + onDemandAllocationStrategy: OnDemandAllocationStrategy.PRIORITIZED, + onDemandBaseCapacity: 1, + onDemandPercentageAboveBaseCapacity: 2, + spotAllocationStrategy: SpotAllocationStrategy.PRICE_CAPACITY_OPTIMIZED, + spotInstancePools: 3, + spotMaxPrice: '4', + }, + }, + vpc: mockVpc(stack), + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::AutoScaling::AutoScalingGroup', { + MixedInstancesPolicy: { + InstancesDistribution: { + SpotAllocationStrategy: 'price-capacity-optimized', + }, + }, + }); +}); + + function mockSecurityGroup(stack: cdk.Stack) { return ec2.SecurityGroup.fromSecurityGroupId(stack, 'MySG', 'most-secure'); }