Skip to content

Commit a85ad39

Browse files
authored
fix(elasticloadbalancingv2): target group health check does not validate interval versus timeout (#16107)
fix: Add validation to target group health check creation. Fixes issue #3703. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent de218ba commit a85ad39

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/base-target-group.ts

+5
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ export abstract class TargetGroupBase extends CoreConstruct implements ITargetGr
297297
* Set/replace the target group's health check
298298
*/
299299
public configureHealthCheck(healthCheck: HealthCheck) {
300+
if (healthCheck.interval && healthCheck.timeout) {
301+
if (healthCheck.interval.toMilliseconds() <= healthCheck.timeout.toMilliseconds()) {
302+
throw new Error(`Healthcheck interval ${healthCheck.interval.toHumanString()} must be greater than the timeout ${healthCheck.timeout.toHumanString()}`);
303+
}
304+
}
300305
this.healthCheck = healthCheck;
301306
}
302307

packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/listener.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -437,17 +437,17 @@ describe('tests', () => {
437437
});
438438
group.configureHealthCheck({
439439
unhealthyThresholdCount: 3,
440-
timeout: cdk.Duration.hours(1),
441-
interval: cdk.Duration.seconds(30),
440+
timeout: cdk.Duration.seconds(30),
441+
interval: cdk.Duration.seconds(60),
442442
path: '/test',
443443
});
444444

445445
// THEN
446446
expect(stack).toHaveResource('AWS::ElasticLoadBalancingV2::TargetGroup', {
447447
UnhealthyThresholdCount: 3,
448-
HealthCheckIntervalSeconds: 30,
448+
HealthCheckIntervalSeconds: 60,
449449
HealthCheckPath: '/test',
450-
HealthCheckTimeoutSeconds: 3600,
450+
HealthCheckTimeoutSeconds: 30,
451451
});
452452
});
453453

@@ -466,8 +466,8 @@ describe('tests', () => {
466466

467467
group.configureHealthCheck({
468468
unhealthyThresholdCount: 3,
469-
timeout: cdk.Duration.hours(1),
470-
interval: cdk.Duration.seconds(30),
469+
timeout: cdk.Duration.seconds(30),
470+
interval: cdk.Duration.seconds(60),
471471
path: '/test',
472472
protocol: elbv2.Protocol.TCP,
473473
});

packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/target-group.test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,45 @@ describe('tests', () => {
281281
}).toThrow(/Slow start duration value must be between 30 and 900 seconds./);
282282
});
283283
});
284+
285+
test('Interval equal to timeout', () => {
286+
// GIVEN
287+
const app = new cdk.App();
288+
const stack = new cdk.Stack(app, 'Stack');
289+
const vpc = new ec2.Vpc(stack, 'VPC', {});
290+
291+
// WHEN
292+
const tg = new elbv2.ApplicationTargetGroup(stack, 'TargetGroup', {
293+
vpc,
294+
});
295+
296+
// THEN
297+
expect(() => {
298+
tg.configureHealthCheck({
299+
interval: cdk.Duration.seconds(60),
300+
timeout: cdk.Duration.seconds(60),
301+
});
302+
}).toThrow(/Healthcheck interval 1 minute must be greater than the timeout 1 minute/);
303+
});
304+
305+
test('Interval smaller than timeout', () => {
306+
// GIVEN
307+
const app = new cdk.App();
308+
const stack = new cdk.Stack(app, 'Stack');
309+
const vpc = new ec2.Vpc(stack, 'VPC', {});
310+
311+
// WHEN
312+
const tg = new elbv2.ApplicationTargetGroup(stack, 'TargetGroup', {
313+
vpc,
314+
});
315+
316+
// THEN
317+
expect(() => {
318+
tg.configureHealthCheck({
319+
interval: cdk.Duration.seconds(60),
320+
timeout: cdk.Duration.seconds(120),
321+
});
322+
}).toThrow(/Healthcheck interval 1 minute must be greater than the timeout 2 minutes/);
323+
});
324+
284325
});

0 commit comments

Comments
 (0)