Skip to content

Commit 0535d36

Browse files
hencricerix0rrr
authored andcommitted
feat(ecs): set default health check grace period to 60s (#2942)
Set the default `HealthCheckGracePeriodSeconds` in base-service to 60 seconds if it's not already set and at least one load balancer is configured This was a FIXME in the ECS CDK codebase. Closes #2936.
1 parent c42523e commit 0535d36

File tree

9 files changed

+37
-7
lines changed

9 files changed

+37
-7
lines changed

packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.asset-image.expected.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@
779779
}
780780
}
781781
],
782+
"HealthCheckGracePeriodSeconds": 60,
782783
"NetworkConfiguration": {
783784
"AwsvpcConfiguration": {
784785
"AssignPublicIp": "DISABLED",
@@ -1038,4 +1039,4 @@
10381039
"Description": "Artifact hash for asset \"aws-ecs-integ/AdoptEcrRepositorydbc60defc59544bcaa5c28c95d68f62c/Code\""
10391040
}
10401041
}
1041-
}
1042+
}

packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.executionrole.expected.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@
615615
},
616616
"DesiredCount": 1,
617617
"LaunchType": "FARGATE",
618+
"HealthCheckGracePeriodSeconds": 60,
618619
"LoadBalancers": [
619620
{
620621
"ContainerName": "web",
@@ -701,4 +702,4 @@
701702
}
702703
}
703704
}
704-
}
705+
}

packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3.expected.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@
602602
},
603603
"DesiredCount": 1,
604604
"LaunchType": "FARGATE",
605+
"HealthCheckGracePeriodSeconds": 60,
605606
"LoadBalancers": [
606607
{
607608
"ContainerName": "web",

packages/@aws-cdk/aws-ecs/lib/base/base-service.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import ec2 = require('@aws-cdk/aws-ec2');
44
import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
55
import iam = require('@aws-cdk/aws-iam');
66
import cloudmap = require('@aws-cdk/aws-servicediscovery');
7-
import { Construct, Duration, Fn, IResource, Lazy, PhysicalName, Resource, Stack } from '@aws-cdk/cdk';
7+
import { Construct, Duration, Fn, IResolvable, IResource, Lazy, PhysicalName, Resource, Stack } from '@aws-cdk/cdk';
88
import { NetworkMode, TaskDefinition } from '../base/task-definition';
99
import { ICluster } from '../cluster';
1010
import { CfnService } from '../ecs.generated';
@@ -63,7 +63,7 @@ export interface BaseServiceProps {
6363
/**
6464
* Time after startup to ignore unhealthy load balancer checks.
6565
*
66-
* @default ??? FIXME
66+
* @default - defaults to 60 seconds if at least one load balancer is in-use and it is not already set
6767
*/
6868
readonly healthCheckGracePeriod?: Duration;
6969

@@ -150,7 +150,7 @@ export abstract class BaseService extends Resource
150150
maximumPercent: props.maximumPercent || 200,
151151
minimumHealthyPercent: props.minimumHealthyPercent === undefined ? 50 : props.minimumHealthyPercent
152152
},
153-
healthCheckGracePeriodSeconds: props.healthCheckGracePeriod && props.healthCheckGracePeriod.toSeconds(),
153+
healthCheckGracePeriodSeconds: this.evaluateHealthGracePeriod(props.healthCheckGracePeriod),
154154
/* role: never specified, supplanted by Service Linked Role */
155155
networkConfiguration: Lazy.anyValue({ produce: () => this.networkConfiguration }),
156156
serviceRegistries: Lazy.anyValue({ produce: () => this.serviceRegistries }),
@@ -389,6 +389,18 @@ export abstract class BaseService extends Resource
389389

390390
return cloudmapService;
391391
}
392+
393+
/**
394+
* Return the default grace period when load balancers are configured and
395+
* healthCheckGracePeriod is not already set
396+
*/
397+
private evaluateHealthGracePeriod(providedHealthCheckGracePeriod?: Duration): IResolvable {
398+
return Lazy.anyValue({
399+
produce: () => providedHealthCheckGracePeriod !== undefined ? providedHealthCheckGracePeriod.toSeconds() :
400+
this.loadBalancers.length > 0 ? 60 :
401+
undefined
402+
});
403+
}
392404
}
393405

394406
/**

packages/@aws-cdk/aws-ecs/test/ec2/integ.lb-awsvpc-nw.expected.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@
852852
"MinimumHealthyPercent": 50
853853
},
854854
"DesiredCount": 1,
855+
"HealthCheckGracePeriodSeconds": 60,
855856
"LaunchType": "EC2",
856857
"LoadBalancers": [
857858
{

packages/@aws-cdk/aws-ecs/test/ec2/integ.lb-bridge-nw.expected.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,7 @@
874874
"MinimumHealthyPercent": 50
875875
},
876876
"DesiredCount": 1,
877+
"HealthCheckGracePeriodSeconds": 60,
877878
"LaunchType": "EC2",
878879
"LoadBalancers": [
879880
{

packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,17 @@ export = {
598598
ContainerPort: 808,
599599
LoadBalancerName: { Ref: "LB8A12904C" }
600600
}
601-
],
601+
]
602+
}));
603+
604+
expect(stack).to(haveResource('AWS::ECS::Service', {
605+
// if any load balancer is configured and healthCheckGracePeriodSeconds is not
606+
// set, then it should default to 60 seconds.
607+
HealthCheckGracePeriodSeconds: 60
602608
}));
603609

604610
test.done();
605-
},
611+
}
606612
},
607613

608614
'When enabling service discovery': {

packages/@aws-cdk/aws-ecs/test/fargate/integ.lb-awsvpc-nw.expected.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@
423423
"MinimumHealthyPercent": 50
424424
},
425425
"DesiredCount": 1,
426+
"HealthCheckGracePeriodSeconds": 60,
426427
"LaunchType": "FARGATE",
427428
"LoadBalancers": [
428429
{

packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ export = {
251251
}
252252
}));
253253

254+
expect(stack).to(haveResource('AWS::ECS::Service', {
255+
// if any load balancer is configured and healthCheckGracePeriodSeconds is not
256+
// set, then it should default to 60 seconds.
257+
HealthCheckGracePeriodSeconds: 60
258+
}));
259+
254260
test.done();
255261
},
256262

0 commit comments

Comments
 (0)