Skip to content

Commit

Permalink
feat(ecs): pass healthy percent & deregistraiton delay params in L3 c…
Browse files Browse the repository at this point in the history
…onstruct
  • Loading branch information
cheruvian committed Jan 9, 2024
1 parent ca91626 commit df9505c
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,10 @@
"Port": 80,
"Protocol": "HTTP",
"TargetGroupAttributes": [
{
"Key": "deregistration_delay.timeout_seconds",
"Value": "10"
},
{
"Key": "stickiness.enabled",
"Value": "false"
Expand Down Expand Up @@ -1188,7 +1192,7 @@
"Rollback": false
},
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
"MinimumHealthyPercent": 33
},
"EnableECSManagedTags": false,
"EnableExecuteCommand": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InstanceType, Vpc } from 'aws-cdk-lib/aws-ec2';
import { Cluster, ContainerImage } from 'aws-cdk-lib/aws-ecs';
import { App, Stack } from 'aws-cdk-lib';
import { App, Duration, Stack } from 'aws-cdk-lib';
import * as integ from '@aws-cdk/integ-tests-alpha';
import { ApplicationMultipleTargetGroupsEc2Service } from 'aws-cdk-lib/aws-ecs-patterns';
import { AUTOSCALING_GENERATE_LAUNCH_TEMPLATE } from 'aws-cdk-lib/cx-api';
Expand All @@ -19,9 +19,11 @@ new ApplicationMultipleTargetGroupsEc2Service(stack, 'myService', {
image: ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
},
enableExecuteCommand: true,
minHealthyPercent: 33,
targetGroups: [
{
containerPort: 80,
deregistrationDelay: Duration.seconds(10),
},
{
containerPort: 90,
Expand Down
4 changes: 4 additions & 0 deletions packages/aws-cdk-lib/aws-ecs-patterns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ const loadBalancedEc2Service = new ecsPatterns.ApplicationMultipleTargetGroupsEc
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
},
minHealthyPercent: 33,
targetGroups: [
{
containerPort: 80,
deregistrationDelay: 300
},
{
containerPort: 90,
Expand All @@ -108,9 +110,11 @@ const loadBalancedFargateService = new ecsPatterns.ApplicationMultipleTargetGrou
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
},
minHealthyPercent: 33,
targetGroups: [
{
containerPort: 80,
deregistrationDelay: 300
},
{
containerPort: 90,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ export interface ApplicationMultipleTargetGroupsServiceBaseProps {
* @default - false
*/
readonly enableExecuteCommand?: boolean;

/**
* The minimum number of tasks, specified as a percentage of
* the Amazon ECS service's DesiredCount value, that must
* continue to run and remain healthy during a deployment.
*
* @default - 0 if daemon, otherwise 50
*/
readonly minHealthyPercent?: number;
}

/**
Expand Down Expand Up @@ -268,6 +277,15 @@ export interface ApplicationTargetProps {
* @default No path condition
*/
readonly pathPattern?: string;

/**
* The amount of time for Elastic Load Balancing to wait before deregistering a target.
*
* The range is 0-3600 seconds.
*
* @default 300
*/
readonly deregistrationDelay?: Duration;
}

/**
Expand Down Expand Up @@ -516,6 +534,7 @@ export abstract class ApplicationMultipleTargetGroupsServiceBase extends Constru
}),
],
conditions,
deregistrationDelay: targetProps.deregistrationDelay,
priority: targetProps.priority,
});
this.targetGroups.push(targetGroup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ export interface NetworkMultipleTargetGroupsServiceBaseProps {
* @default - false
*/
readonly enableExecuteCommand?: boolean;

/**
* The minimum number of tasks, specified as a percentage of
* the Amazon ECS service's DesiredCount value, that must
* continue to run and remain healthy during a deployment.
*
* @default - 0 if daemon, otherwise 50
*/
readonly minHealthyPercent?: number;
}

/**
Expand Down Expand Up @@ -268,6 +277,15 @@ export interface NetworkTargetProps {
* @default - default listener (first added listener)
*/
readonly listener?: string;

/**
* The amount of time for Elastic Load Balancing to wait before deregistering a target.
*
* The range is 0-3600 seconds.
*
* @default 300
*/
readonly deregistrationDelay?: Duration;
}

/**
Expand Down Expand Up @@ -394,6 +412,7 @@ export abstract class NetworkMultipleTargetGroupsServiceBase extends Construct {
containerPort: targetProps.containerPort,
}),
],
deregistrationDelay: targetProps.deregistrationDelay,
});
this.targetGroups.push(targetGroup);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export class ApplicationMultipleTargetGroupsEc2Service extends ApplicationMultip
enableExecuteCommand: props.enableExecuteCommand,
placementConstraints: props.placementConstraints,
placementStrategies: props.placementStrategies,
minHealthyPercent: props.minHealthyPercent,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export class NetworkMultipleTargetGroupsEc2Service extends NetworkMultipleTarget
enableExecuteCommand: props.enableExecuteCommand,
placementConstraints: props.placementConstraints,
placementStrategies: props.placementStrategies,
minHealthyPercent: props.minHealthyPercent,
});
}
}
4 changes: 4 additions & 0 deletions packages/aws-cdk-lib/aws-ecs-patterns/test/ec2/l3s-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ describe('When Application Load Balancer', () => {
],
placementStrategies: [PlacementStrategy.spreadAcrossInstances(), PlacementStrategy.packedByCpu(), PlacementStrategy.randomly()],
placementConstraints: [PlacementConstraint.memberOf('attribute:ecs.instance-type =~ m5a.*')],
minHealthyPercent: 50,
});

// THEN
Expand Down Expand Up @@ -197,6 +198,7 @@ describe('When Application Load Balancer', () => {
ServiceName: 'myService',
PlacementConstraints: [{ Type: 'memberOf', Expression: 'attribute:ecs.instance-type =~ m5a.*' }],
PlacementStrategies: [{ Field: 'instanceId', Type: 'spread' }, { Field: 'CPU', Type: 'binpack' }, { Type: 'random' }],
DeploymentConfiguration: { MinimumHealthyPercent: 50 },
});

Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
Expand Down Expand Up @@ -1148,6 +1150,7 @@ describe('When Network Load Balancer', () => {
],
placementStrategies: [PlacementStrategy.spreadAcrossInstances(), PlacementStrategy.packedByCpu(), PlacementStrategy.randomly()],
placementConstraints: [PlacementConstraint.memberOf('attribute:ecs.instance-type =~ m5a.*')],
minHealthyPercent: 50,
});

// THEN
Expand Down Expand Up @@ -1178,6 +1181,7 @@ describe('When Network Load Balancer', () => {
ServiceName: 'myService',
PlacementConstraints: [{ Type: 'memberOf', Expression: 'attribute:ecs.instance-type =~ m5a.*' }],
PlacementStrategies: [{ Field: 'instanceId', Type: 'spread' }, { Field: 'CPU', Type: 'binpack' }, { Type: 'random' }],
DeploymentConfiguration: { MinimumHealthyPercent: 50 },
});

Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
Expand Down

0 comments on commit df9505c

Please sign in to comment.