From 4f50297b98e73c2d3ea771c9b779b95bc23e66ce Mon Sep 17 00:00:00 2001 From: UnnatiParekh05 Date: Tue, 30 Nov 2021 17:22:53 -0800 Subject: [PATCH 1/6] depreacate scale on CPU utilization --- .../extensions/scale-on-cpu-utilization.ts | 25 +++----- .../ecs-service-extensions/lib/service.ts | 59 ++++++++++++------- .../test/scale-on-cpu-utilization.test.ts | 30 +++++++++- 3 files changed, 75 insertions(+), 39 deletions(-) diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/scale-on-cpu-utilization.ts b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/scale-on-cpu-utilization.ts index ec6ce9662535f..d6a06b5b36397 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/scale-on-cpu-utilization.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/scale-on-cpu-utilization.ts @@ -1,9 +1,13 @@ -import * as ecs from '@aws-cdk/aws-ecs'; import * as cdk from '@aws-cdk/core'; import { ServiceExtension, ServiceBuild } from './extension-interfaces'; + /** * The autoscaling settings. + * + * @deprecated use the `minTaskCount` and `maxTaskCount` properties of `autoScaleTaskCount` in the `Service` construct + * to configure the auto scaling target for the service. For more information, please refer + * https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk-containers/ecs-service-extensions/README.md#task-auto-scaling . */ export interface CpuScalingProps { /** @@ -61,6 +65,9 @@ const cpuScalingPropsDefault = { /** * This extension helps you scale your service according to CPU utilization. + * + * @deprecated To enable target tracking based on CPU utilization, use the `targetCpuUtilization` property of `autoScaleTaskCount` in the `Service` construct. + * For more information, please refer https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk-containers/ecs-service-extensions/README.md#task-auto-scaling . */ export class ScaleOnCpuUtilization extends ServiceExtension { /** @@ -112,6 +119,7 @@ export class ScaleOnCpuUtilization extends ServiceExtension { // This service modifies properties of the service prior // to construct creation. public modifyServiceProps(props: ServiceBuild): ServiceBuild { + this.parentService.enableAutoScalingPolicy(); return { ...props, @@ -122,19 +130,4 @@ export class ScaleOnCpuUtilization extends ServiceExtension { desiredCount: this.initialTaskCount, } as ServiceBuild; } - - // This hook utilizes the resulting service construct - // once it is created. - public useService(service: ecs.Ec2Service | ecs.FargateService) { - const scalingTarget = service.autoScaleTaskCount({ - minCapacity: this.minTaskCount, - maxCapacity: this.maxTaskCount, - }); - - scalingTarget.scaleOnCpuUtilization(`${this.parentService.id}-target-cpu-utilization-${this.targetCpuUtilization}`, { - targetUtilizationPercent: this.targetCpuUtilization, - scaleInCooldown: this.scaleInCooldown, - scaleOutCooldown: this.scaleOutCooldown, - }); - } } diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts b/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts index 2214f209fb935..108336f4df2af 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts @@ -4,6 +4,7 @@ import * as iam from '@aws-cdk/aws-iam'; import * as cdk from '@aws-cdk/core'; import { IEnvironment } from './environment'; import { EnvironmentCapacityType, ServiceBuild } from './extensions/extension-interfaces'; +import { ScaleOnCpuUtilization } from './extensions/scale-on-cpu-utilization'; import { ServiceDescription } from './service-description'; // keep this import separate from other imports to reduce chance for merge conflicts with v2-main @@ -221,9 +222,6 @@ export class Service extends Construct { } } - // Set desiredCount to `undefined` if auto scaling is configured for the service - const desiredCount = props.autoScaleTaskCount ? undefined : (props.desiredCount || 1); - // Give each extension a chance to mutate the service props before // service creation let serviceProps = { @@ -231,7 +229,7 @@ export class Service extends Construct { taskDefinition: this.taskDefinition, minHealthyPercent: 100, maxHealthyPercent: 200, - desiredCount, + desiredCount: props.desiredCount ?? 1, } as ServiceBuild; for (const extensions in this.serviceDescription.extensions) { @@ -240,6 +238,14 @@ export class Service extends Construct { } } + // Set desiredCount to `undefined` if auto scaling is configured for the service + if (props.autoScaleTaskCount || this.autoScalingPoliciesEnabled) { + serviceProps = { + ...serviceProps, + desiredCount: undefined, + }; + } + // If a maxHealthyPercent and desired count has been set while minHealthyPercent == 100% then we // need to do some failsafe checking to ensure that the maxHealthyPercent // actually allows a rolling deploy. Otherwise it is possible to end up with @@ -283,28 +289,39 @@ export class Service extends Construct { throw new Error(`Unknown capacity type for service ${this.id}`); } + const scaleOnCpuExtension = this.serviceDescription.get('scale-on-cpu-utilization') as ScaleOnCpuUtilization; + + if (props.autoScaleTaskCount && scaleOnCpuExtension) { + throw Error('Cannot specify \'minTaskCount\' and \'maxTaskCount\' both in the Service construct and the \'ScaleOnCpuUtilization\' extension (deprecated). Please only provide the values in either one.'); + } + // Create the auto scaling target and configure target tracking policies after the service is created - if (props.autoScaleTaskCount) { + const maxCapacity = props.autoScaleTaskCount?.maxTaskCount ?? scaleOnCpuExtension?.maxTaskCount; + if (maxCapacity) { this.scalableTaskCount = this.ecsService.autoScaleTaskCount({ - maxCapacity: props.autoScaleTaskCount.maxTaskCount, - minCapacity: props.autoScaleTaskCount.minTaskCount, + maxCapacity: props.autoScaleTaskCount?.maxTaskCount ?? scaleOnCpuExtension?.maxTaskCount, + minCapacity: props.autoScaleTaskCount?.minTaskCount ?? scaleOnCpuExtension?.minTaskCount, }); + } - if (props.autoScaleTaskCount.targetCpuUtilization) { - const targetUtilizationPercent = props.autoScaleTaskCount.targetCpuUtilization; - this.scalableTaskCount.scaleOnCpuUtilization(`${this.id}-target-cpu-utilization-${targetUtilizationPercent}`, { - targetUtilizationPercent, - }); - this.enableAutoScalingPolicy(); - } + const targetCpuUtilizationPercent = props.autoScaleTaskCount?.targetCpuUtilization ?? scaleOnCpuExtension?.targetCpuUtilization; + if (targetCpuUtilizationPercent) { + const scaleInCooldown = scaleOnCpuExtension?.scaleInCooldown; + const scaleOutCooldown = scaleOnCpuExtension?.scaleOutCooldown; + this.scalableTaskCount!.scaleOnCpuUtilization(`${this.id}-target-cpu-utilization-${targetCpuUtilizationPercent}`, { + targetUtilizationPercent: targetCpuUtilizationPercent, + scaleInCooldown, + scaleOutCooldown, + }); + this.enableAutoScalingPolicy(); + } - if (props.autoScaleTaskCount.targetMemoryUtilization) { - const targetUtilizationPercent = props.autoScaleTaskCount.targetMemoryUtilization; - this.scalableTaskCount.scaleOnMemoryUtilization(`${this.id}-target-memory-utilization-${targetUtilizationPercent}`, { - targetUtilizationPercent, - }); - this.enableAutoScalingPolicy(); - } + if (props.autoScaleTaskCount?.targetMemoryUtilization) { + const targetMemoryUtilizationPercent = props.autoScaleTaskCount.targetMemoryUtilization; + this.scalableTaskCount!.scaleOnMemoryUtilization(`${this.id}-target-memory-utilization-${targetMemoryUtilizationPercent}`, { + targetUtilizationPercent: targetMemoryUtilizationPercent, + }); + this.enableAutoScalingPolicy(); } // Now give all extensions a chance to use the service diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts b/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts index 1b8c9f82fb1a6..a12931df62dfc 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts @@ -32,7 +32,6 @@ describe('scale on cpu utilization', () => { MaximumPercent: 200, MinimumHealthyPercent: 100, }, - DesiredCount: 2, }); expect(stack).toHaveResource('AWS::ApplicationAutoScaling::ScalableTarget', { @@ -130,7 +129,6 @@ describe('scale on cpu utilization', () => { MaximumPercent: 200, MinimumHealthyPercent: 100, }, - DesiredCount: 25, }); expect(stack).toHaveResourceLike('AWS::ApplicationAutoScaling::ScalableTarget', { @@ -149,4 +147,32 @@ describe('scale on cpu utilization', () => { }); + test('should error if connfiguring autoscaling target both in the extension and the Service', () => { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + const environment = new Environment(stack, 'production'); + const serviceDescription = new ServiceDescription(); + + serviceDescription.add(new Container({ + cpu: 256, + memoryMiB: 512, + trafficPort: 80, + image: ecs.ContainerImage.fromRegistry('nathanpeck/name'), + })); + + serviceDescription.add(new ScaleOnCpuUtilization()); + // THEN + expect(() => { + new Service(stack, 'my-service', { + environment, + serviceDescription, + autoScaleTaskCount: { + maxTaskCount: 5, + }, + }); + }).toThrow('Cannot specify \'minTaskCount\' and \'maxTaskCount\' both in the Service construct and the \'ScaleOnCpuUtilization\' extension (deprecated). Please only provide the values in either one.'); + }); + }); \ No newline at end of file From c3156839f97b5d695a730f0754f647078133571a Mon Sep 17 00:00:00 2001 From: UnnatiParekh05 Date: Tue, 30 Nov 2021 17:23:54 -0800 Subject: [PATCH 2/6] Appmesh desired count related updates --- .../test/appmesh.test.ts | 22 +++++-------------- .../integ.all-service-addons.expected.json | 9 +++----- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/test/appmesh.test.ts b/packages/@aws-cdk-containers/ecs-service-extensions/test/appmesh.test.ts index 64ec202869f73..c6fbd24d9858f 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/test/appmesh.test.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/test/appmesh.test.ts @@ -2,7 +2,7 @@ import '@aws-cdk/assert-internal/jest'; import * as appmesh from '@aws-cdk/aws-appmesh'; import * as ecs from '@aws-cdk/aws-ecs'; import * as cdk from '@aws-cdk/core'; -import { AppMeshExtension, Container, Environment, ScaleOnCpuUtilization, ServiceDescription, Service } from '../lib'; +import { AppMeshExtension, Container, Environment, ServiceDescription, Service } from '../lib'; describe('appmesh', () => { test('should be able to add AWS App Mesh to a service', () => { @@ -276,9 +276,6 @@ describe('appmesh', () => { trafficPort: 80, image: ecs.ContainerImage.fromRegistry('nathanpeck/name'), })); - serviceDescription.add(new ScaleOnCpuUtilization({ - initialTaskCount: 1, - })); const mesh = new appmesh.Mesh(stack, 'my-mesh'); @@ -289,6 +286,7 @@ describe('appmesh', () => { new Service(stack, 'my-service', { environment, serviceDescription, + desiredCount: 1, }); expect(stack).toHaveResourceLike('AWS::ECS::Service', { @@ -317,9 +315,6 @@ describe('appmesh', () => { trafficPort: 80, image: ecs.ContainerImage.fromRegistry('nathanpeck/name'), })); - serviceDescription.add(new ScaleOnCpuUtilization({ - initialTaskCount: 2, - })); const mesh = new appmesh.Mesh(stack, 'my-mesh'); @@ -330,6 +325,7 @@ describe('appmesh', () => { new Service(stack, 'my-service', { environment, serviceDescription, + desiredCount: 2, }); expect(stack).toHaveResourceLike('AWS::ECS::Service', { @@ -358,9 +354,6 @@ describe('appmesh', () => { trafficPort: 80, image: ecs.ContainerImage.fromRegistry('nathanpeck/name'), })); - serviceDescription.add(new ScaleOnCpuUtilization({ - initialTaskCount: 3, - })); const mesh = new appmesh.Mesh(stack, 'my-mesh'); @@ -371,6 +364,7 @@ describe('appmesh', () => { new Service(stack, 'my-service', { environment, serviceDescription, + desiredCount: 3, }); expect(stack).toHaveResourceLike('AWS::ECS::Service', { @@ -399,9 +393,6 @@ describe('appmesh', () => { trafficPort: 80, image: ecs.ContainerImage.fromRegistry('nathanpeck/name'), })); - serviceDescription.add(new ScaleOnCpuUtilization({ - initialTaskCount: 4, - })); const mesh = new appmesh.Mesh(stack, 'my-mesh'); @@ -412,6 +403,7 @@ describe('appmesh', () => { new Service(stack, 'my-service', { environment, serviceDescription, + desiredCount: 4, }); expect(stack).toHaveResourceLike('AWS::ECS::Service', { @@ -440,9 +432,6 @@ describe('appmesh', () => { trafficPort: 80, image: ecs.ContainerImage.fromRegistry('nathanpeck/name'), })); - serviceDescription.add(new ScaleOnCpuUtilization({ - initialTaskCount: 8, - })); const mesh = new appmesh.Mesh(stack, 'my-mesh'); @@ -453,6 +442,7 @@ describe('appmesh', () => { new Service(stack, 'my-service', { environment, serviceDescription, + desiredCount: 8, }); expect(stack).toHaveResourceLike('AWS::ECS::Service', { diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json b/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json index bd9224e586933..c9c2eb658b946 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json +++ b/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json @@ -1116,10 +1116,9 @@ "Ref": "productionenvironmentclusterC6599D2D" }, "DeploymentConfiguration": { - "MaximumPercent": 150, + "MaximumPercent": 125, "MinimumHealthyPercent": 100 }, - "DesiredCount": 2, "EnableECSManagedTags": false, "LaunchType": "FARGATE", "NetworkConfiguration": { @@ -1989,10 +1988,9 @@ "Ref": "productionenvironmentclusterC6599D2D" }, "DeploymentConfiguration": { - "MaximumPercent": 150, + "MaximumPercent": 125, "MinimumHealthyPercent": 100 }, - "DesiredCount": 2, "EnableECSManagedTags": false, "LaunchType": "FARGATE", "NetworkConfiguration": { @@ -2985,10 +2983,9 @@ "Ref": "productionenvironmentclusterC6599D2D" }, "DeploymentConfiguration": { - "MaximumPercent": 150, + "MaximumPercent": 125, "MinimumHealthyPercent": 100 }, - "DesiredCount": 2, "EnableECSManagedTags": false, "HealthCheckGracePeriodSeconds": 60, "LaunchType": "FARGATE", From fccee3d7932d967cecc04e5d23ec7304ba406307 Mon Sep 17 00:00:00 2001 From: UnnatiParekh05 Date: Wed, 1 Dec 2021 12:14:11 -0800 Subject: [PATCH 3/6] Small updates --- .../@aws-cdk-containers/ecs-service-extensions/lib/service.ts | 2 +- .../test/scale-on-cpu-utilization.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts b/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts index 108336f4df2af..a7dc9ec3bfd92 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts @@ -299,7 +299,7 @@ export class Service extends Construct { const maxCapacity = props.autoScaleTaskCount?.maxTaskCount ?? scaleOnCpuExtension?.maxTaskCount; if (maxCapacity) { this.scalableTaskCount = this.ecsService.autoScaleTaskCount({ - maxCapacity: props.autoScaleTaskCount?.maxTaskCount ?? scaleOnCpuExtension?.maxTaskCount, + maxCapacity, minCapacity: props.autoScaleTaskCount?.minTaskCount ?? scaleOnCpuExtension?.minTaskCount, }); } diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts b/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts index a12931df62dfc..157949ebc88e5 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts @@ -147,7 +147,7 @@ describe('scale on cpu utilization', () => { }); - test('should error if connfiguring autoscaling target both in the extension and the Service', () => { + test('should error if configuring autoscaling target both in the extension and the Service', () => { // GIVEN const stack = new cdk.Stack(); From 3e8773f62aa0c767383ff0a81e3f6c0cbee775f0 Mon Sep 17 00:00:00 2001 From: UnnatiParekh05 Date: Mon, 13 Dec 2021 17:46:56 -0800 Subject: [PATCH 4/6] Revert max and min health percent to original vals --- .../ecs-service-extensions/lib/service.ts | 16 ++++++++-------- .../test/integ.all-service-addons.expected.json | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts b/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts index a7dc9ec3bfd92..090f67355422a 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts @@ -238,14 +238,6 @@ export class Service extends Construct { } } - // Set desiredCount to `undefined` if auto scaling is configured for the service - if (props.autoScaleTaskCount || this.autoScalingPoliciesEnabled) { - serviceProps = { - ...serviceProps, - desiredCount: undefined, - }; - } - // If a maxHealthyPercent and desired count has been set while minHealthyPercent == 100% then we // need to do some failsafe checking to ensure that the maxHealthyPercent // actually allows a rolling deploy. Otherwise it is possible to end up with @@ -279,6 +271,14 @@ export class Service extends Construct { } } + // Set desiredCount to `undefined` if auto scaling is configured for the service + if (props.autoScaleTaskCount || this.autoScalingPoliciesEnabled) { + serviceProps = { + ...serviceProps, + desiredCount: undefined, + }; + } + // Now that the service props are determined we can create // the service if (this.capacityType === EnvironmentCapacityType.EC2) { diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json b/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json index c9c2eb658b946..cee547dc9d1b1 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json +++ b/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json @@ -1116,7 +1116,7 @@ "Ref": "productionenvironmentclusterC6599D2D" }, "DeploymentConfiguration": { - "MaximumPercent": 125, + "MaximumPercent": 150, "MinimumHealthyPercent": 100 }, "EnableECSManagedTags": false, @@ -1988,7 +1988,7 @@ "Ref": "productionenvironmentclusterC6599D2D" }, "DeploymentConfiguration": { - "MaximumPercent": 125, + "MaximumPercent": 150, "MinimumHealthyPercent": 100 }, "EnableECSManagedTags": false, @@ -2983,7 +2983,7 @@ "Ref": "productionenvironmentclusterC6599D2D" }, "DeploymentConfiguration": { - "MaximumPercent": 125, + "MaximumPercent": 150, "MinimumHealthyPercent": 100 }, "EnableECSManagedTags": false, From c3c2a16e15bff01c2ee26c5d5c0b21594b07f40f Mon Sep 17 00:00:00 2001 From: UnnatiParekh05 Date: Fri, 7 Jan 2022 13:13:20 -0800 Subject: [PATCH 5/6] Revert to original - make both exclusive --- .../extensions/scale-on-cpu-utilization.ts | 21 ++++++++- .../ecs-service-extensions/lib/service.ts | 46 +++++++------------ .../integ.all-service-addons.expected.json | 3 ++ .../test/scale-on-cpu-utilization.test.ts | 4 +- 4 files changed, 43 insertions(+), 31 deletions(-) diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/scale-on-cpu-utilization.ts b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/scale-on-cpu-utilization.ts index d6a06b5b36397..c7f4637e4dcb9 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/scale-on-cpu-utilization.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/scale-on-cpu-utilization.ts @@ -1,3 +1,4 @@ +import * as ecs from '@aws-cdk/aws-ecs'; import * as cdk from '@aws-cdk/core'; import { ServiceExtension, ServiceBuild } from './extension-interfaces'; @@ -119,7 +120,6 @@ export class ScaleOnCpuUtilization extends ServiceExtension { // This service modifies properties of the service prior // to construct creation. public modifyServiceProps(props: ServiceBuild): ServiceBuild { - this.parentService.enableAutoScalingPolicy(); return { ...props, @@ -130,4 +130,23 @@ export class ScaleOnCpuUtilization extends ServiceExtension { desiredCount: this.initialTaskCount, } as ServiceBuild; } + + // This hook utilizes the resulting service construct + // once it is created. + public useService(service: ecs.Ec2Service | ecs.FargateService) { + if (this.parentService.scalableTaskCount) { + throw Error('Cannot specify \'minTaskCount\' or \'maxTaskCount\' in the Service construct and also provide a \'ScaleOnCpuUtilization\' extension. \'ScaleOnCpuUtilization\' is deprecated. Please only provide \'minTaskCount\' and \'maxTaskCount\'.'); + } + const scalingTarget = service.autoScaleTaskCount({ + minCapacity: this.minTaskCount, + maxCapacity: this.maxTaskCount, + }); + + scalingTarget.scaleOnCpuUtilization(`${this.parentService.id}-target-cpu-utilization-${this.targetCpuUtilization}`, { + targetUtilizationPercent: this.targetCpuUtilization, + scaleInCooldown: this.scaleInCooldown, + scaleOutCooldown: this.scaleOutCooldown, + }); + this.parentService.enableAutoScalingPolicy(); + } } diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts b/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts index 090f67355422a..7909d5b32053f 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts @@ -4,7 +4,6 @@ import * as iam from '@aws-cdk/aws-iam'; import * as cdk from '@aws-cdk/core'; import { IEnvironment } from './environment'; import { EnvironmentCapacityType, ServiceBuild } from './extensions/extension-interfaces'; -import { ScaleOnCpuUtilization } from './extensions/scale-on-cpu-utilization'; import { ServiceDescription } from './service-description'; // keep this import separate from other imports to reduce chance for merge conflicts with v2-main @@ -289,39 +288,28 @@ export class Service extends Construct { throw new Error(`Unknown capacity type for service ${this.id}`); } - const scaleOnCpuExtension = this.serviceDescription.get('scale-on-cpu-utilization') as ScaleOnCpuUtilization; - - if (props.autoScaleTaskCount && scaleOnCpuExtension) { - throw Error('Cannot specify \'minTaskCount\' and \'maxTaskCount\' both in the Service construct and the \'ScaleOnCpuUtilization\' extension (deprecated). Please only provide the values in either one.'); - } - // Create the auto scaling target and configure target tracking policies after the service is created - const maxCapacity = props.autoScaleTaskCount?.maxTaskCount ?? scaleOnCpuExtension?.maxTaskCount; - if (maxCapacity) { + if (props.autoScaleTaskCount) { this.scalableTaskCount = this.ecsService.autoScaleTaskCount({ - maxCapacity, - minCapacity: props.autoScaleTaskCount?.minTaskCount ?? scaleOnCpuExtension?.minTaskCount, + maxCapacity: props.autoScaleTaskCount.maxTaskCount, + minCapacity: props.autoScaleTaskCount.minTaskCount, }); - } - const targetCpuUtilizationPercent = props.autoScaleTaskCount?.targetCpuUtilization ?? scaleOnCpuExtension?.targetCpuUtilization; - if (targetCpuUtilizationPercent) { - const scaleInCooldown = scaleOnCpuExtension?.scaleInCooldown; - const scaleOutCooldown = scaleOnCpuExtension?.scaleOutCooldown; - this.scalableTaskCount!.scaleOnCpuUtilization(`${this.id}-target-cpu-utilization-${targetCpuUtilizationPercent}`, { - targetUtilizationPercent: targetCpuUtilizationPercent, - scaleInCooldown, - scaleOutCooldown, - }); - this.enableAutoScalingPolicy(); - } + if (props.autoScaleTaskCount.targetCpuUtilization) { + const targetCpuUtilizationPercent = props.autoScaleTaskCount.targetCpuUtilization; + this.scalableTaskCount.scaleOnCpuUtilization(`${this.id}-target-cpu-utilization-${targetCpuUtilizationPercent}`, { + targetUtilizationPercent: targetCpuUtilizationPercent, + }); + this.enableAutoScalingPolicy(); + } - if (props.autoScaleTaskCount?.targetMemoryUtilization) { - const targetMemoryUtilizationPercent = props.autoScaleTaskCount.targetMemoryUtilization; - this.scalableTaskCount!.scaleOnMemoryUtilization(`${this.id}-target-memory-utilization-${targetMemoryUtilizationPercent}`, { - targetUtilizationPercent: targetMemoryUtilizationPercent, - }); - this.enableAutoScalingPolicy(); + if (props.autoScaleTaskCount.targetMemoryUtilization) { + const targetMemoryUtilizationPercent = props.autoScaleTaskCount.targetMemoryUtilization; + this.scalableTaskCount.scaleOnMemoryUtilization(`${this.id}-target-memory-utilization-${targetMemoryUtilizationPercent}`, { + targetUtilizationPercent: targetMemoryUtilizationPercent, + }); + this.enableAutoScalingPolicy(); + } } // Now give all extensions a chance to use the service diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json b/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json index 9963b40010a21..04e3fb87689fa 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json +++ b/packages/@aws-cdk-containers/ecs-service-extensions/test/integ.all-service-addons.expected.json @@ -1119,6 +1119,7 @@ "MaximumPercent": 150, "MinimumHealthyPercent": 100 }, + "DesiredCount": 2, "EnableECSManagedTags": false, "LaunchType": "FARGATE", "NetworkConfiguration": { @@ -1991,6 +1992,7 @@ "MaximumPercent": 150, "MinimumHealthyPercent": 100 }, + "DesiredCount": 2, "EnableECSManagedTags": false, "LaunchType": "FARGATE", "NetworkConfiguration": { @@ -2986,6 +2988,7 @@ "MaximumPercent": 150, "MinimumHealthyPercent": 100 }, + "DesiredCount": 2, "EnableECSManagedTags": false, "HealthCheckGracePeriodSeconds": 60, "LaunchType": "FARGATE", diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts b/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts index 157949ebc88e5..04ff939169852 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts @@ -32,6 +32,7 @@ describe('scale on cpu utilization', () => { MaximumPercent: 200, MinimumHealthyPercent: 100, }, + DesiredCount: 2, }); expect(stack).toHaveResource('AWS::ApplicationAutoScaling::ScalableTarget', { @@ -129,6 +130,7 @@ describe('scale on cpu utilization', () => { MaximumPercent: 200, MinimumHealthyPercent: 100, }, + DesiredCount: 25, }); expect(stack).toHaveResourceLike('AWS::ApplicationAutoScaling::ScalableTarget', { @@ -172,7 +174,7 @@ describe('scale on cpu utilization', () => { maxTaskCount: 5, }, }); - }).toThrow('Cannot specify \'minTaskCount\' and \'maxTaskCount\' both in the Service construct and the \'ScaleOnCpuUtilization\' extension (deprecated). Please only provide the values in either one.'); + }).toThrow('Cannot specify \'minTaskCount\' or \'maxTaskCount\' in the Service construct and also provide a \'ScaleOnCpuUtilization\' extension. \'ScaleOnCpuUtilization\' is deprecated. Please only provide \'minTaskCount\' and \'maxTaskCount\'.'); }); }); \ No newline at end of file From 8179376e04e2bd4f5b971447eb7dc8a0ccc41987 Mon Sep 17 00:00:00 2001 From: UnnatiParekh05 Date: Wed, 12 Jan 2022 12:37:33 -0800 Subject: [PATCH 6/6] Error message updated --- .../lib/extensions/scale-on-cpu-utilization.ts | 2 +- .../test/scale-on-cpu-utilization.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/scale-on-cpu-utilization.ts b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/scale-on-cpu-utilization.ts index c7f4637e4dcb9..2aa60ebc96364 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/scale-on-cpu-utilization.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/scale-on-cpu-utilization.ts @@ -135,7 +135,7 @@ export class ScaleOnCpuUtilization extends ServiceExtension { // once it is created. public useService(service: ecs.Ec2Service | ecs.FargateService) { if (this.parentService.scalableTaskCount) { - throw Error('Cannot specify \'minTaskCount\' or \'maxTaskCount\' in the Service construct and also provide a \'ScaleOnCpuUtilization\' extension. \'ScaleOnCpuUtilization\' is deprecated. Please only provide \'minTaskCount\' and \'maxTaskCount\'.'); + throw Error('Cannot specify \'autoScaleTaskCount\' in the Service construct and also provide a \'ScaleOnCpuUtilization\' extension. \'ScaleOnCpuUtilization\' is deprecated. Please only provide \'autoScaleTaskCount\'.'); } const scalingTarget = service.autoScaleTaskCount({ minCapacity: this.minTaskCount, diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts b/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts index 04ff939169852..561e1f7991a94 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts @@ -174,7 +174,7 @@ describe('scale on cpu utilization', () => { maxTaskCount: 5, }, }); - }).toThrow('Cannot specify \'minTaskCount\' or \'maxTaskCount\' in the Service construct and also provide a \'ScaleOnCpuUtilization\' extension. \'ScaleOnCpuUtilization\' is deprecated. Please only provide \'minTaskCount\' and \'maxTaskCount\'.'); + }).toThrow('Cannot specify \'autoScaleTaskCount\' in the Service construct and also provide a \'ScaleOnCpuUtilization\' extension. \'ScaleOnCpuUtilization\' is deprecated. Please only provide \'autoScaleTaskCount\'.'); }); }); \ No newline at end of file