Skip to content

Commit bb67345

Browse files
authored
chore(ecs-service-extensions): Deprecate scale on CPU utilization extension (#17802)
---- This PR deprecates one of the existing `scaleOnCpuUtilization` extension. We recommend users to configure task auto scaling using the [`autoScaleTaskCount`](https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk-containers/ecs-service-extensions/lib/service.ts#L61) in the `Service` construct. Related PR: #17101 *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent a9038ae commit bb67345

File tree

4 files changed

+61
-26
lines changed

4 files changed

+61
-26
lines changed

packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/scale-on-cpu-utilization.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import * as ecs from '@aws-cdk/aws-ecs';
22
import * as cdk from '@aws-cdk/core';
33
import { ServiceExtension, ServiceBuild } from './extension-interfaces';
44

5+
56
/**
67
* The autoscaling settings.
8+
*
9+
* @deprecated use the `minTaskCount` and `maxTaskCount` properties of `autoScaleTaskCount` in the `Service` construct
10+
* to configure the auto scaling target for the service. For more information, please refer
11+
* https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk-containers/ecs-service-extensions/README.md#task-auto-scaling .
712
*/
813
export interface CpuScalingProps {
914
/**
@@ -61,6 +66,9 @@ const cpuScalingPropsDefault = {
6166

6267
/**
6368
* This extension helps you scale your service according to CPU utilization.
69+
*
70+
* @deprecated To enable target tracking based on CPU utilization, use the `targetCpuUtilization` property of `autoScaleTaskCount` in the `Service` construct.
71+
* 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 .
6472
*/
6573
export class ScaleOnCpuUtilization extends ServiceExtension {
6674
/**
@@ -126,6 +134,9 @@ export class ScaleOnCpuUtilization extends ServiceExtension {
126134
// This hook utilizes the resulting service construct
127135
// once it is created.
128136
public useService(service: ecs.Ec2Service | ecs.FargateService) {
137+
if (this.parentService.scalableTaskCount) {
138+
throw Error('Cannot specify \'autoScaleTaskCount\' in the Service construct and also provide a \'ScaleOnCpuUtilization\' extension. \'ScaleOnCpuUtilization\' is deprecated. Please only provide \'autoScaleTaskCount\'.');
139+
}
129140
const scalingTarget = service.autoScaleTaskCount({
130141
minCapacity: this.minTaskCount,
131142
maxCapacity: this.maxTaskCount,
@@ -136,5 +147,6 @@ export class ScaleOnCpuUtilization extends ServiceExtension {
136147
scaleInCooldown: this.scaleInCooldown,
137148
scaleOutCooldown: this.scaleOutCooldown,
138149
});
150+
this.parentService.enableAutoScalingPolicy();
139151
}
140152
}

packages/@aws-cdk-containers/ecs-service-extensions/lib/service.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,14 @@ export class Service extends Construct {
221221
}
222222
}
223223

224-
// Set desiredCount to `undefined` if auto scaling is configured for the service
225-
const desiredCount = props.autoScaleTaskCount ? undefined : (props.desiredCount || 1);
226-
227224
// Give each extension a chance to mutate the service props before
228225
// service creation
229226
let serviceProps = {
230227
cluster: this.cluster,
231228
taskDefinition: this.taskDefinition,
232229
minHealthyPercent: 100,
233230
maxHealthyPercent: 200,
234-
desiredCount,
231+
desiredCount: props.desiredCount ?? 1,
235232
} as ServiceBuild;
236233

237234
for (const extensions in this.serviceDescription.extensions) {
@@ -273,6 +270,14 @@ export class Service extends Construct {
273270
}
274271
}
275272

273+
// Set desiredCount to `undefined` if auto scaling is configured for the service
274+
if (props.autoScaleTaskCount || this.autoScalingPoliciesEnabled) {
275+
serviceProps = {
276+
...serviceProps,
277+
desiredCount: undefined,
278+
};
279+
}
280+
276281
// Now that the service props are determined we can create
277282
// the service
278283
if (this.capacityType === EnvironmentCapacityType.EC2) {
@@ -291,17 +296,17 @@ export class Service extends Construct {
291296
});
292297

293298
if (props.autoScaleTaskCount.targetCpuUtilization) {
294-
const targetUtilizationPercent = props.autoScaleTaskCount.targetCpuUtilization;
295-
this.scalableTaskCount.scaleOnCpuUtilization(`${this.id}-target-cpu-utilization-${targetUtilizationPercent}`, {
296-
targetUtilizationPercent,
299+
const targetCpuUtilizationPercent = props.autoScaleTaskCount.targetCpuUtilization;
300+
this.scalableTaskCount.scaleOnCpuUtilization(`${this.id}-target-cpu-utilization-${targetCpuUtilizationPercent}`, {
301+
targetUtilizationPercent: targetCpuUtilizationPercent,
297302
});
298303
this.enableAutoScalingPolicy();
299304
}
300305

301306
if (props.autoScaleTaskCount.targetMemoryUtilization) {
302-
const targetUtilizationPercent = props.autoScaleTaskCount.targetMemoryUtilization;
303-
this.scalableTaskCount.scaleOnMemoryUtilization(`${this.id}-target-memory-utilization-${targetUtilizationPercent}`, {
304-
targetUtilizationPercent,
307+
const targetMemoryUtilizationPercent = props.autoScaleTaskCount.targetMemoryUtilization;
308+
this.scalableTaskCount.scaleOnMemoryUtilization(`${this.id}-target-memory-utilization-${targetMemoryUtilizationPercent}`, {
309+
targetUtilizationPercent: targetMemoryUtilizationPercent,
305310
});
306311
this.enableAutoScalingPolicy();
307312
}

packages/@aws-cdk-containers/ecs-service-extensions/test/appmesh.test.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import '@aws-cdk/assert-internal/jest';
22
import * as appmesh from '@aws-cdk/aws-appmesh';
33
import * as ecs from '@aws-cdk/aws-ecs';
44
import * as cdk from '@aws-cdk/core';
5-
import { AppMeshExtension, Container, Environment, ScaleOnCpuUtilization, ServiceDescription, Service } from '../lib';
5+
import { AppMeshExtension, Container, Environment, ServiceDescription, Service } from '../lib';
66

77
describe('appmesh', () => {
88
test('should be able to add AWS App Mesh to a service', () => {
@@ -276,9 +276,6 @@ describe('appmesh', () => {
276276
trafficPort: 80,
277277
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
278278
}));
279-
serviceDescription.add(new ScaleOnCpuUtilization({
280-
initialTaskCount: 1,
281-
}));
282279

283280
const mesh = new appmesh.Mesh(stack, 'my-mesh');
284281

@@ -289,6 +286,7 @@ describe('appmesh', () => {
289286
new Service(stack, 'my-service', {
290287
environment,
291288
serviceDescription,
289+
desiredCount: 1,
292290
});
293291

294292
expect(stack).toHaveResourceLike('AWS::ECS::Service', {
@@ -317,9 +315,6 @@ describe('appmesh', () => {
317315
trafficPort: 80,
318316
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
319317
}));
320-
serviceDescription.add(new ScaleOnCpuUtilization({
321-
initialTaskCount: 2,
322-
}));
323318

324319
const mesh = new appmesh.Mesh(stack, 'my-mesh');
325320

@@ -330,6 +325,7 @@ describe('appmesh', () => {
330325
new Service(stack, 'my-service', {
331326
environment,
332327
serviceDescription,
328+
desiredCount: 2,
333329
});
334330

335331
expect(stack).toHaveResourceLike('AWS::ECS::Service', {
@@ -358,9 +354,6 @@ describe('appmesh', () => {
358354
trafficPort: 80,
359355
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
360356
}));
361-
serviceDescription.add(new ScaleOnCpuUtilization({
362-
initialTaskCount: 3,
363-
}));
364357

365358
const mesh = new appmesh.Mesh(stack, 'my-mesh');
366359

@@ -371,6 +364,7 @@ describe('appmesh', () => {
371364
new Service(stack, 'my-service', {
372365
environment,
373366
serviceDescription,
367+
desiredCount: 3,
374368
});
375369

376370
expect(stack).toHaveResourceLike('AWS::ECS::Service', {
@@ -399,9 +393,6 @@ describe('appmesh', () => {
399393
trafficPort: 80,
400394
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
401395
}));
402-
serviceDescription.add(new ScaleOnCpuUtilization({
403-
initialTaskCount: 4,
404-
}));
405396

406397
const mesh = new appmesh.Mesh(stack, 'my-mesh');
407398

@@ -412,6 +403,7 @@ describe('appmesh', () => {
412403
new Service(stack, 'my-service', {
413404
environment,
414405
serviceDescription,
406+
desiredCount: 4,
415407
});
416408

417409
expect(stack).toHaveResourceLike('AWS::ECS::Service', {
@@ -440,9 +432,6 @@ describe('appmesh', () => {
440432
trafficPort: 80,
441433
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
442434
}));
443-
serviceDescription.add(new ScaleOnCpuUtilization({
444-
initialTaskCount: 8,
445-
}));
446435

447436
const mesh = new appmesh.Mesh(stack, 'my-mesh');
448437

@@ -453,6 +442,7 @@ describe('appmesh', () => {
453442
new Service(stack, 'my-service', {
454443
environment,
455444
serviceDescription,
445+
desiredCount: 8,
456446
});
457447

458448
expect(stack).toHaveResourceLike('AWS::ECS::Service', {

packages/@aws-cdk-containers/ecs-service-extensions/test/scale-on-cpu-utilization.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,32 @@ describe('scale on cpu utilization', () => {
149149

150150
});
151151

152+
test('should error if configuring autoscaling target both in the extension and the Service', () => {
153+
// GIVEN
154+
const stack = new cdk.Stack();
155+
156+
// WHEN
157+
const environment = new Environment(stack, 'production');
158+
const serviceDescription = new ServiceDescription();
159+
160+
serviceDescription.add(new Container({
161+
cpu: 256,
162+
memoryMiB: 512,
163+
trafficPort: 80,
164+
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
165+
}));
166+
167+
serviceDescription.add(new ScaleOnCpuUtilization());
168+
// THEN
169+
expect(() => {
170+
new Service(stack, 'my-service', {
171+
environment,
172+
serviceDescription,
173+
autoScaleTaskCount: {
174+
maxTaskCount: 5,
175+
},
176+
});
177+
}).toThrow('Cannot specify \'autoScaleTaskCount\' in the Service construct and also provide a \'ScaleOnCpuUtilization\' extension. \'ScaleOnCpuUtilization\' is deprecated. Please only provide \'autoScaleTaskCount\'.');
178+
});
179+
152180
});

0 commit comments

Comments
 (0)