Skip to content

Commit

Permalink
fix(elbv2): correct wrong timeout invalidation
Browse files Browse the repository at this point in the history
Fixes: aws#26023.
  • Loading branch information
tmyoda committed Jun 17, 2023
1 parent 83dc73c commit 49a3199
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,17 @@ export class NetworkTargetGroup extends TargetGroupBase implements INetworkTarge
`Must be one of [${NLB_PATH_HEALTH_CHECK_PROTOCOLS.join(', ')}]`,
].join(' '));
}
if (healthCheck.timeout && healthCheck.timeout.toSeconds() !== NLB_HEALTH_CHECK_TIMEOUTS[healthCheck.protocol]) {
ret.push([
'Custom health check timeouts are not supported for Network Load Balancer health checks.',
`Expected ${NLB_HEALTH_CHECK_TIMEOUTS[healthCheck.protocol]} seconds for ${healthCheck.protocol}, got ${healthCheck.timeout.toSeconds()}`,
].join(' '));

const lowHealthCheckTimeout = 2;
const highHealthCheckTimeout = 120;
if (healthCheck.timeout) {
const timeoutSeconds = healthCheck.timeout.toSeconds();
if (timeoutSeconds < lowHealthCheckTimeout || timeoutSeconds > highHealthCheckTimeout) {
ret.push(`Health check timeout '${timeoutSeconds}' not supported. Must be a number between ${lowHealthCheckTimeout} and ${highHealthCheckTimeout}.`);
}
if (healthCheck.interval && healthCheck.interval.toSeconds() < timeoutSeconds) {
ret.push(`Health check timeout '${timeoutSeconds}' must not be greater than the interval '${healthCheck.interval.toSeconds()}'`);
}
}

return ret;
Expand Down Expand Up @@ -365,9 +371,4 @@ export interface INetworkLoadBalancerTarget {
}

const NLB_HEALTH_CHECK_PROTOCOLS = [Protocol.HTTP, Protocol.HTTPS, Protocol.TCP];
const NLB_PATH_HEALTH_CHECK_PROTOCOLS = [Protocol.HTTP, Protocol.HTTPS];
const NLB_HEALTH_CHECK_TIMEOUTS: { [protocol in Protocol]?: number } = {
[Protocol.HTTP]: 6,
[Protocol.HTTPS]: 10,
[Protocol.TCP]: 10,
};
const NLB_PATH_HEALTH_CHECK_PROTOCOLS = [Protocol.HTTP, Protocol.HTTPS];
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,40 @@ describe('tests', () => {
},
});

targetGroup.configureHealthCheck({
interval: cdk.Duration.seconds(150),
protocol: elbv2.Protocol.HTTP,
timeout: cdk.Duration.seconds(130),
});

// THEN
const validationErrors: string[] = targetGroup.node.validate();
const timeoutError = validationErrors.find((err) => /Health check timeout '130' not supported. Must be a number between/.test(err));
expect(timeoutError).toBeDefined();
});

test('validation error if Health check timeout is greater than the interval', () => {
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Stack');
const lb = new elbv2.NetworkLoadBalancer(stack, 'LB', { vpc });
const listener = lb.addListener('PublicListener', { port: 80 });
const targetGroup = listener.addTargets('ECS', {
port: 80,
healthCheck: {
interval: cdk.Duration.seconds(60),
},
});

targetGroup.configureHealthCheck({
interval: cdk.Duration.seconds(30),
protocol: elbv2.Protocol.HTTP,
timeout: cdk.Duration.seconds(10),
timeout: cdk.Duration.seconds(40),
});

// THEN
const validationErrors: string[] = targetGroup.node.validate();
expect(validationErrors).toEqual([
'Custom health check timeouts are not supported for Network Load Balancer health checks. Expected 6 seconds for HTTP, got 10',
"Health check timeout '40' must not be greater than the interval '30'",
]);
});

Expand Down

0 comments on commit 49a3199

Please sign in to comment.