Skip to content

Commit

Permalink
fix(elasticloadbalancingv2): target group health check does not valid…
Browse files Browse the repository at this point in the history
…ate interval versus timeout (aws#16107)

fix: Add validation to target group health check creation. Fixes issue aws#3703.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
mprencipe authored and TikiTDO committed Sep 6, 2021
1 parent 814cae3 commit fc3e858
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});

Expand All @@ -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,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
});

});

0 comments on commit fc3e858

Please sign in to comment.