diff --git a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/nlb/network-target-group.ts b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/nlb/network-target-group.ts index 4700bf3a6a374..eff0a6efc0c97 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/nlb/network-target-group.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/nlb/network-target-group.ts @@ -126,6 +126,28 @@ export class NetworkTargetGroup extends TargetGroupBase implements INetworkTarge } } + if (healthCheck.healthyThresholdCount) { + const thresholdCount = healthCheck.healthyThresholdCount; + if (thresholdCount < 2 || thresholdCount > 10) { + ret.push(`Healthy Threshold Count '${thresholdCount}' not supported. Must be a number between 2 and 10.`); + } + } + + if (healthCheck.unhealthyThresholdCount) { + const thresholdCount = healthCheck.unhealthyThresholdCount; + if (thresholdCount < 2 || thresholdCount > 10) { + ret.push(`Unhealthy Threshold Count '${thresholdCount}' not supported. Must be a number between 2 and 10.`); + } + } + + if (healthCheck.healthyThresholdCount && healthCheck.unhealthyThresholdCount && + healthCheck.healthyThresholdCount !== healthCheck.unhealthyThresholdCount) { + ret.push([ + `Healthy and Unhealthy Threshold Counts must be the same: ${healthCheck.healthyThresholdCount}`, + `is not equal to ${healthCheck.unhealthyThresholdCount}.`, + ].join(' ')); + } + if (!healthCheck.protocol) { return ret; } diff --git a/packages/@aws-cdk/aws-elasticloadbalancingv2/test/nlb/target-group.test.ts b/packages/@aws-cdk/aws-elasticloadbalancingv2/test/nlb/target-group.test.ts index 692d76e5f42ac..6f50cd19be618 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancingv2/test/nlb/target-group.test.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancingv2/test/nlb/target-group.test.ts @@ -91,4 +91,117 @@ describe('tests', () => { }); }).toThrow(); }); + + test('Throws error for invalid health check interval', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'Stack'); + const vpc = new ec2.Vpc(stack, 'Vpc'); + + new elbv2.NetworkTargetGroup(stack, 'Group', { + vpc, + port: 80, + healthCheck: { + interval: cdk.Duration.seconds(5), + }, + }); + + expect(() => { + app.synth(); + }).toThrow(/Health check interval '5' not supported. Must be one of the following values '10,30'./); + }); + + test('Throws error for invalid health check protocol', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'Stack'); + const vpc = new ec2.Vpc(stack, 'Vpc'); + + new elbv2.NetworkTargetGroup(stack, 'Group', { + vpc, + port: 80, + healthCheck: { + protocol: elbv2.Protocol.UDP, + }, + }); + + expect(() => { + app.synth(); + }).toThrow(/Health check protocol 'UDP' is not supported. Must be one of \[HTTP, HTTPS, TCP\]/); + }); + + test('Throws error for health check path property when protocol does not support it', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'Stack'); + const vpc = new ec2.Vpc(stack, 'Vpc'); + + new elbv2.NetworkTargetGroup(stack, 'Group', { + vpc, + port: 80, + healthCheck: { + path: '/my-path', + protocol: elbv2.Protocol.TCP, + }, + }); + + expect(() => { + app.synth(); + }).toThrow(/'TCP' health checks do not support the path property. Must be one of \[HTTP, HTTPS\]/); + }); + + test('Throws error for invalid health check healthy threshold', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'Stack'); + const vpc = new ec2.Vpc(stack, 'Vpc'); + + new elbv2.NetworkTargetGroup(stack, 'Group', { + vpc, + port: 80, + healthCheck: { + protocol: elbv2.Protocol.TCP, + healthyThresholdCount: 11, + }, + }); + + expect(() => { + app.synth(); + }).toThrow(/Healthy Threshold Count '11' not supported. Must be a number between 2 and 10./); + }); + + test('Throws error for invalid health check unhealthy threshold', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'Stack'); + const vpc = new ec2.Vpc(stack, 'Vpc'); + + new elbv2.NetworkTargetGroup(stack, 'Group', { + vpc, + port: 80, + healthCheck: { + protocol: elbv2.Protocol.TCP, + unhealthyThresholdCount: 1, + }, + }); + + expect(() => { + app.synth(); + }).toThrow(/Unhealthy Threshold Count '1' not supported. Must be a number between 2 and 10./); + }); + + test('Throws error for unequal healthy and unhealthy threshold counts', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'Stack'); + const vpc = new ec2.Vpc(stack, 'Vpc'); + + new elbv2.NetworkTargetGroup(stack, 'Group', { + vpc, + port: 80, + healthCheck: { + protocol: elbv2.Protocol.TCP, + healthyThresholdCount: 5, + unhealthyThresholdCount: 3, + }, + }); + + expect(() => { + app.synth(); + }).toThrow(/Healthy and Unhealthy Threshold Counts must be the same: 5 is not equal to 3./); + }); });