Skip to content

Commit

Permalink
fix(elbv2): NLB Target Group does not inherit protocol (#9331) (#9651)
Browse files Browse the repository at this point in the history
fixes #9331 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
hoegertn committed Aug 18, 2020
1 parent 8debcf8 commit 171ab59
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Expand Up @@ -107,6 +107,11 @@ export class NetworkListener extends BaseListener implements INetworkListener {
*/
private readonly loadBalancer: INetworkLoadBalancer;

/**
* the protocol of the listener
*/
private readonly protocol: Protocol;

constructor(scope: Construct, id: string, props: NetworkListenerProps) {
const certs = props.certificates || [];
const proto = props.protocol || (certs.length > 0 ? Protocol.TLS : Protocol.TCP);
Expand All @@ -130,6 +135,7 @@ export class NetworkListener extends BaseListener implements INetworkListener {
});

this.loadBalancer = props.loadBalancer;
this.protocol = proto;

if (props.defaultAction && props.defaultTargetGroups) {
throw new Error('Specify at most one of \'defaultAction\' and \'defaultTargetGroups\'');
Expand Down Expand Up @@ -189,6 +195,7 @@ export class NetworkListener extends BaseListener implements INetworkListener {
deregistrationDelay: props.deregistrationDelay,
healthCheck: props.healthCheck,
port: props.port,
protocol: this.protocol,
proxyProtocolV2: props.proxyProtocolV2,
targetGroupName: props.targetGroupName,
targets: props.targets,
Expand Down
Expand Up @@ -86,6 +86,40 @@ export = {
test.done();
},

'implicitly created target group inherits protocol'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Stack');
const lb = new elbv2.NetworkLoadBalancer(stack, 'LB', { vpc });
const listener = lb.addListener('Listener', { port: 9700, protocol: elbv2.Protocol.TCP_UDP });

// WHEN
listener.addTargets('Targets', {
port: 9700,
targets: [new elbv2.InstanceTarget('i-12345')],
});

// THEN
expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::Listener', {
DefaultActions: [
{
TargetGroupArn: { Ref: 'LBListenerTargetsGroup76EF81E8' },
Type: 'forward',
},
],
}));
expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::TargetGroup', {
VpcId: { Ref: 'Stack8A423254' },
Port: 9700,
Protocol: 'TCP_UDP',
Targets: [
{ Id: 'i-12345' },
],
}));

test.done();
},

'Enable health check for targets'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit 171ab59

Please sign in to comment.