Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ecs-patterns): remove default value for desiredCount #12992

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ export interface ApplicationLoadBalancedServiceBaseProps {
* The desired number of instantiations of the task definition to keep running on the service.
* The minimum value is 1
*
* @default 1
* @default - When creating the service, default is 1; when updating the service, default uses
* the current task number.
*/
readonly desiredCount?: number;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export interface ApplicationMultipleTargetGroupsServiceBaseProps {
/**
* The desired number of instantiations of the task definition to keep running on the service.
*
* @default 1
* @default - When creating the service, default is 1; when updating the service, default uses
* the current task number.
*/
readonly desiredCount?: number;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export interface NetworkLoadBalancedServiceBaseProps {
* The desired number of instantiations of the task definition to keep running on the service.
* The minimum value is 1
*
* @default 1
* @default - When creating the service, default is 1; when updating the service, default uses
* the current task number.
*/
readonly desiredCount?: number;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export interface NetworkMultipleTargetGroupsServiceBaseProps {
* The desired number of instantiations of the task definition to keep running on the service.
* The minimum value is 1
*
* @default 1
* @default - When creating the service, default is 1; when updating the service, default uses
* the current task number.
*/
readonly desiredCount?: number;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export interface QueueProcessingServiceBaseProps {
* The desired number of instantiations of the task definition to keep running on the service.
*
* @default 1
* @deprecated - Use `minScalingCapacity` or a literal object instead.
*/
readonly desiredTaskCount?: number;

Expand Down Expand Up @@ -109,10 +110,17 @@ export interface QueueProcessingServiceBaseProps {
/**
* Maximum capacity to scale to.
*
* @default (desiredTaskCount * 2)
* @default 2
*/
readonly maxScalingCapacity?: number

/**
* Minimum capacity to scale to.
*
* @default 1
*/
readonly minScalingCapacity?: number

/**
* The intervals for scaling based on the SQS queue's ApproximateNumberOfMessagesVisible metric.
*
Expand Down Expand Up @@ -222,6 +230,11 @@ export abstract class QueueProcessingServiceBase extends CoreConstruct {
*/
public readonly maxCapacity: number;

/**
* The minimum number of instances for autoscaling to scale down to.
*/
public readonly minCapacity: number;

/**
* The scaling interval for autoscaling based off an SQS Queue size.
*/
Expand Down Expand Up @@ -274,12 +287,9 @@ export abstract class QueueProcessingServiceBase extends CoreConstruct {

// Determine the desired task count (minimum) and maximum scaling capacity
this.desiredCount = props.desiredTaskCount ?? 1;
this.minCapacity = props.minScalingCapacity || this.desiredCount;
this.maxCapacity = props.maxScalingCapacity || (2 * this.desiredCount);

if (!this.desiredCount && !this.maxCapacity) {
throw new Error('maxScalingCapacity must be set and greater than 0 if desiredCount is 0');
}

new CfnOutput(this, 'SQSQueue', { value: this.sqsQueue.queueName });
new CfnOutput(this, 'SQSQueueArn', { value: this.sqsQueue.queueArn });
}
Expand All @@ -290,7 +300,7 @@ export abstract class QueueProcessingServiceBase extends CoreConstruct {
* @param service the ECS/Fargate service for which to apply the autoscaling rules to
*/
protected configureAutoscalingForService(service: BaseService) {
const scalingTarget = service.autoScaleTaskCount({ maxCapacity: this.maxCapacity, minCapacity: this.desiredCount });
const scalingTarget = service.autoScaleTaskCount({ maxCapacity: this.maxCapacity, minCapacity: this.minCapacity });
scalingTarget.scaleOnCpuUtilization('CpuScaling', {
targetUtilizationPercent: 50,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class ApplicationLoadBalancedEc2Service extends ApplicationLoadBalancedSe

this.service = new Ec2Service(this, 'Service', {
cluster: this.cluster,
desiredCount: this.desiredCount,
desiredCount: props.desiredCount,
taskDefinition: this.taskDefinition,
assignPublicIp: false,
serviceName: props.serviceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class ApplicationMultipleTargetGroupsEc2Service extends ApplicationMultip
private createEc2Service(props: ApplicationMultipleTargetGroupsEc2ServiceProps): Ec2Service {
return new Ec2Service(this, 'Service', {
cluster: this.cluster,
desiredCount: this.desiredCount,
desiredCount: props.desiredCount,
taskDefinition: this.taskDefinition,
assignPublicIp: false,
serviceName: props.serviceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class NetworkLoadBalancedEc2Service extends NetworkLoadBalancedServiceBas

this.service = new Ec2Service(this, 'Service', {
cluster: this.cluster,
desiredCount: this.desiredCount,
desiredCount: props.desiredCount,
taskDefinition: this.taskDefinition,
assignPublicIp: false,
serviceName: props.serviceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class NetworkMultipleTargetGroupsEc2Service extends NetworkMultipleTarget
private createEc2Service(props: NetworkMultipleTargetGroupsEc2ServiceProps): Ec2Service {
return new Ec2Service(this, 'Service', {
cluster: this.cluster,
desiredCount: this.desiredCount,
desiredCount: props.desiredCount,
taskDefinition: this.taskDefinition,
assignPublicIp: false,
serviceName: props.serviceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export class QueueProcessingEc2Service extends QueueProcessingServiceBase {
// autoscaling based on cpu utilization and number of messages visible in the SQS queue.
this.service = new Ec2Service(this, 'QueueProcessingService', {
cluster: this.cluster,
desiredCount: this.desiredCount,
taskDefinition: this.taskDefinition,
serviceName: props.serviceName,
minHealthyPercent: props.minHealthyPercent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalanc

this.service = new FargateService(this, 'Service', {
cluster: this.cluster,
desiredCount: this.desiredCount,
desiredCount: props.desiredCount,
taskDefinition: this.taskDefinition,
assignPublicIp: this.assignPublicIp,
serviceName: props.serviceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export class ApplicationMultipleTargetGroupsFargateService extends ApplicationMu
private createFargateService(props: ApplicationMultipleTargetGroupsFargateServiceProps): FargateService {
return new FargateService(this, 'Service', {
cluster: this.cluster,
desiredCount: this.desiredCount,
desiredCount: props.desiredCount,
taskDefinition: this.taskDefinition,
assignPublicIp: this.assignPublicIp,
serviceName: props.serviceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServic

this.service = new FargateService(this, 'Service', {
cluster: this.cluster,
desiredCount: this.desiredCount,
desiredCount: props.desiredCount,
taskDefinition: this.taskDefinition,
assignPublicIp: this.assignPublicIp,
serviceName: props.serviceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export class NetworkMultipleTargetGroupsFargateService extends NetworkMultipleTa
private createFargateService(props: NetworkMultipleTargetGroupsFargateServiceProps): FargateService {
return new FargateService(this, 'Service', {
cluster: this.cluster,
desiredCount: this.desiredCount,
desiredCount: props.desiredCount,
taskDefinition: this.taskDefinition,
assignPublicIp: this.assignPublicIp,
serviceName: props.serviceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export class QueueProcessingFargateService extends QueueProcessingServiceBase {
// autoscaling based on cpu utilization and number of messages visible in the SQS queue.
this.service = new FargateService(this, 'QueueProcessingFargateService', {
cluster: this.cluster,
desiredCount: this.desiredCount,
taskDefinition: this.taskDefinition,
serviceName: props.serviceName,
minHealthyPercent: props.minHealthyPercent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"HealthCheckGracePeriodSeconds": 60,
"LaunchType": "EC2",
Expand Down
2 changes: 0 additions & 2 deletions packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export = {
expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::LoadBalancer'));

expect(stack).to(haveResource('AWS::ECS::Service', {
DesiredCount: 1,
LaunchType: 'EC2',
}));

Expand Down Expand Up @@ -878,7 +877,6 @@ export = {
expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::LoadBalancer'));

expect(stack).to(haveResource('AWS::ECS::Service', {
DesiredCount: 1,
LaunchType: 'EC2',
}));

Expand Down
2 changes: 0 additions & 2 deletions packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ export = {
}));

expect(stack).to(haveResource('AWS::ECS::Service', {
DesiredCount: 1,
LaunchType: 'FARGATE',
}));

Expand Down Expand Up @@ -488,7 +487,6 @@ export = {
}));

expect(stack).to(haveResource('AWS::ECS::Service', {
DesiredCount: 1,
LaunchType: 'FARGATE',
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export = {

// THEN - QueueWorker is of EC2 launch type, an SQS queue is created and all default properties are set.
expect(stack).to(haveResource('AWS::ECS::Service', {
DesiredCount: 1,
LaunchType: 'EC2',
}));

Expand Down Expand Up @@ -98,7 +97,6 @@ export = {

// THEN - QueueWorker is of EC2 launch type, an SQS queue is created and all default properties are set.
expect(stack).to(haveResource('AWS::ECS::Service', {
DesiredCount: 1,
LaunchType: 'EC2',
}));

Expand Down Expand Up @@ -190,7 +188,6 @@ export = {

// THEN - QueueWorker is of EC2 launch type, an SQS queue is created and all optional properties are set.
expect(stack).to(haveResource('AWS::ECS::Service', {
DesiredCount: 2,
DeploymentConfiguration: {
MinimumHealthyPercent: 60,
MaximumPercent: 150,
Expand Down Expand Up @@ -261,33 +258,12 @@ export = {

// THEN - QueueWorker is of EC2 launch type, an SQS queue is created and all default properties are set.
expect(stack).to(haveResource('AWS::ECS::Service', {
DesiredCount: 0,
LaunchType: 'EC2',
}));

test.done();
},

'throws if desiredTaskCount and maxScalingCapacity are 0'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });

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

test.done();
},

'can set custom containerName'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"HealthCheckGracePeriodSeconds": 60,
"LaunchType": "FARGATE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"HealthCheckGracePeriodSeconds": 60,
"LaunchType": "FARGATE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"HealthCheckGracePeriodSeconds": 60,
"LaunchType": "FARGATE",
Expand Down Expand Up @@ -894,7 +893,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"HealthCheckGracePeriodSeconds": 60,
"LaunchType": "FARGATE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"HealthCheckGracePeriodSeconds": 60,
"LaunchType": "FARGATE",
Expand Down Expand Up @@ -1249,7 +1248,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"HealthCheckGracePeriodSeconds": 60,
"LaunchType": "FARGATE",
Expand Down Expand Up @@ -1560,7 +1558,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"HealthCheckGracePeriodSeconds": 60,
"LaunchType": "FARGATE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"HealthCheckGracePeriodSeconds": 60,
"LaunchType": "FARGATE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": true,
"HealthCheckGracePeriodSeconds": 60,
"LaunchType": "FARGATE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"HealthCheckGracePeriodSeconds": 60,
"LaunchType": "FARGATE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"LaunchType": "FARGATE",
"NetworkConfiguration": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"LaunchType": "FARGATE",
"NetworkConfiguration": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"HealthCheckGracePeriodSeconds": 60,
"LaunchType": "FARGATE",
Expand Down Expand Up @@ -826,7 +825,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"HealthCheckGracePeriodSeconds": 60,
"LaunchType": "FARGATE",
Expand Down
Loading