diff --git a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/base-target-group.ts b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/base-target-group.ts index 175f63ddc4d3d..96acd45b34a4c 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/base-target-group.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/base-target-group.ts @@ -297,6 +297,11 @@ export abstract class TargetGroupBase extends CoreConstruct implements ITargetGr * Set/replace the target group's health check */ public configureHealthCheck(healthCheck: HealthCheck) { + if (healthCheck.interval && healthCheck.timeout) { + if (healthCheck.interval.toMilliseconds() <= healthCheck.timeout.toMilliseconds()) { + throw new Error(`Healthcheck interval ${healthCheck.interval.toHumanString()} must be greater than the timeout ${healthCheck.timeout.toHumanString()}`); + } + } this.healthCheck = healthCheck; } diff --git a/packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/listener.test.ts b/packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/listener.test.ts index 2d945aeb2621f..885c872482e9c 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/listener.test.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/listener.test.ts @@ -437,17 +437,17 @@ describe('tests', () => { }); group.configureHealthCheck({ unhealthyThresholdCount: 3, - timeout: cdk.Duration.hours(1), - interval: cdk.Duration.seconds(30), + timeout: cdk.Duration.seconds(30), + interval: cdk.Duration.seconds(60), path: '/test', }); // THEN expect(stack).toHaveResource('AWS::ElasticLoadBalancingV2::TargetGroup', { UnhealthyThresholdCount: 3, - HealthCheckIntervalSeconds: 30, + HealthCheckIntervalSeconds: 60, HealthCheckPath: '/test', - HealthCheckTimeoutSeconds: 3600, + HealthCheckTimeoutSeconds: 30, }); }); @@ -466,8 +466,8 @@ describe('tests', () => { group.configureHealthCheck({ unhealthyThresholdCount: 3, - timeout: cdk.Duration.hours(1), - interval: cdk.Duration.seconds(30), + timeout: cdk.Duration.seconds(30), + interval: cdk.Duration.seconds(60), path: '/test', protocol: elbv2.Protocol.TCP, }); diff --git a/packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/target-group.test.ts b/packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/target-group.test.ts index 97c36c33ce237..f1a3db5eb9508 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/target-group.test.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/target-group.test.ts @@ -281,4 +281,45 @@ describe('tests', () => { }).toThrow(/Slow start duration value must be between 30 and 900 seconds./); }); }); + + test('Interval equal to timeout', () => { + // GIVEN + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'Stack'); + const vpc = new ec2.Vpc(stack, 'VPC', {}); + + // WHEN + const tg = new elbv2.ApplicationTargetGroup(stack, 'TargetGroup', { + vpc, + }); + + // THEN + expect(() => { + tg.configureHealthCheck({ + interval: cdk.Duration.seconds(60), + timeout: cdk.Duration.seconds(60), + }); + }).toThrow(/Healthcheck interval 1 minute must be greater than the timeout 1 minute/); + }); + + test('Interval smaller than timeout', () => { + // GIVEN + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'Stack'); + const vpc = new ec2.Vpc(stack, 'VPC', {}); + + // WHEN + const tg = new elbv2.ApplicationTargetGroup(stack, 'TargetGroup', { + vpc, + }); + + // THEN + expect(() => { + tg.configureHealthCheck({ + interval: cdk.Duration.seconds(60), + timeout: cdk.Duration.seconds(120), + }); + }).toThrow(/Healthcheck interval 1 minute must be greater than the timeout 2 minutes/); + }); + });