Skip to content

Commit

Permalink
feat: 32056 remove desired task count from ecs patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
spensireli committed Nov 9, 2024
1 parent 5648199 commit ce49da7
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
ICluster, LogDriver, PropagatedTagSource, Secret,
} from '../../../aws-ecs';
import { IQueue, Queue } from '../../../aws-sqs';
import { CfnOutput, Duration, FeatureFlags, Stack } from '../../../core';
import * as cxapi from '../../../cx-api';
import { CfnOutput, Duration, Stack } from '../../../core';

/**
* The properties for the base QueueProcessingEc2Service or QueueProcessingFargateService service.
Expand Down Expand Up @@ -55,15 +54,6 @@ export interface QueueProcessingServiceBaseProps {
*/
readonly command?: string[];

/**
* The desired number of instantiations of the task definition to keep running on the service.
*
* @default - The minScalingCapacity is 1 for all new services and uses the existing services desired count
* when updating an existing service.
* @deprecated - Use `minScalingCapacity` or a literal object instead.
*/
readonly desiredTaskCount?: number;

/**
* Flag to indicate whether to enable logging.
*
Expand Down Expand Up @@ -286,12 +276,6 @@ export abstract class QueueProcessingServiceBase extends Construct {
*/
public readonly secrets?: { [key: string]: Secret };

/**
* The minimum number of tasks to run.
* @deprecated - Use `minCapacity` instead.
*/
public readonly desiredCount: number;

/**
* The maximum number of instances for autoscaling to scale up to.
*/
Expand Down Expand Up @@ -381,25 +365,8 @@ export abstract class QueueProcessingServiceBase extends Construct {
this.disableCpuBasedScaling = props.disableCpuBasedScaling ?? false;
this.cpuTargetUtilizationPercent = props.cpuTargetUtilizationPercent ?? 50;

this.desiredCount = props.desiredTaskCount ?? 1;

// Determine the desired task count (minimum) and maximum scaling capacity
if (!FeatureFlags.of(this).isEnabled(cxapi.ECS_REMOVE_DEFAULT_DESIRED_COUNT)) {
this.minCapacity = props.minScalingCapacity ?? this.desiredCount;
this.maxCapacity = props.maxScalingCapacity || (2 * this.desiredCount);
} else {
if (props.desiredTaskCount != null) {
this.minCapacity = props.minScalingCapacity ?? this.desiredCount;
this.maxCapacity = props.maxScalingCapacity || (2 * this.desiredCount);
} else {
this.minCapacity = props.minScalingCapacity ?? 1;
this.maxCapacity = props.maxScalingCapacity || 2;
}
}

if (!this.desiredCount && !this.maxCapacity) {
throw new Error('maxScalingCapacity must be set and greater than 0 if desiredCount is 0');
}
this.minCapacity = props.minScalingCapacity ?? 1;
this.maxCapacity = props.maxScalingCapacity || 2;

new CfnOutput(this, 'SQSQueue', { value: this.sqsQueue.queueName });
new CfnOutput(this, 'SQSQueueArn', { value: this.sqsQueue.queueArn });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Construct } from 'constructs';
import { Ec2Service, Ec2TaskDefinition, PlacementConstraint, PlacementStrategy } from '../../../aws-ecs';
import { FeatureFlags } from '../../../core';
import * as cxapi from '../../../cx-api';
import { QueueProcessingServiceBase, QueueProcessingServiceBaseProps } from '../base/queue-processing-service-base';

/**
Expand Down Expand Up @@ -128,14 +126,10 @@ export class QueueProcessingEc2Service extends QueueProcessingServiceBase {
logging: this.logDriver,
});

// The desiredCount should be removed from the fargate service when the feature flag is removed.
const desiredCount = FeatureFlags.of(this).isEnabled(cxapi.ECS_REMOVE_DEFAULT_DESIRED_COUNT) ? undefined : this.desiredCount;

// Create an ECS service with the previously defined Task Definition and configure
// autoscaling based on cpu utilization and number of messages visible in the SQS queue.
this.service = new Ec2Service(this, 'QueueProcessingService', {
cluster: this.cluster,
desiredCount: desiredCount,
taskDefinition: this.taskDefinition,
serviceName: props.serviceName,
minHealthyPercent: props.minHealthyPercent,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Construct } from 'constructs';
import * as ec2 from '../../../aws-ec2';
import { FargateService, FargateTaskDefinition, HealthCheck } from '../../../aws-ecs';
import { FeatureFlags } from '../../../core';
import * as cxapi from '../../../cx-api';
import { FargateServiceBaseProps } from '../base/fargate-service-base';
import { QueueProcessingServiceBase, QueueProcessingServiceBaseProps } from '../base/queue-processing-service-base';

Expand Down Expand Up @@ -95,14 +93,10 @@ export class QueueProcessingFargateService extends QueueProcessingServiceBase {
throw new Error('You must specify one of: taskDefinition or image');
}

// The desiredCount should be removed from the fargate service when the feature flag is removed.
const desiredCount = FeatureFlags.of(this).isEnabled(cxapi.ECS_REMOVE_DEFAULT_DESIRED_COUNT) ? undefined : this.desiredCount;

// Create a Fargate service with the previously defined Task Definition and configure
// autoscaling based on cpu utilization and number of messages visible in the SQS queue.
this.service = new FargateService(this, 'QueueProcessingFargateService', {
cluster: this.cluster,
desiredCount: desiredCount,
taskDefinition: this.taskDefinition,
serviceName: props.serviceName,
minHealthyPercent: props.minHealthyPercent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,30 +352,6 @@ testDeprecated('test ECS queue worker service construct - with optional props',
});
});

testDeprecated('throws if desiredTaskCount and maxScalingCapacity are 0', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
cluster.addAsgCapacityProvider(new AsgCapacityProvider(stack, 'DefaultAutoScalingGroupProvider', {
autoScalingGroup: new AutoScalingGroup(stack, 'DefaultAutoScalingGroup', {
vpc,
instanceType: new ec2.InstanceType('t2.micro'),
machineImage: MachineImage.latestAmazonLinux(),
}),
}));

// THEN
expect(() =>
new ecsPatterns.QueueProcessingEc2Service(stack, 'Service', {
cluster,
desiredTaskCount: 0,
memoryLimitMiB: 512,
image: ecs.ContainerImage.fromRegistry('test'),
}),
).toThrow(/maxScalingCapacity must be set and greater than 0 if desiredCount is 0/);
});

test('can set custom containerName', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit ce49da7

Please sign in to comment.