Skip to content

Commit

Permalink
fix(elbv2): add protocol to AddNetworkTargetsProps (#10054)
Browse files Browse the repository at this point in the history
Fixes #10044


----

*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 Sep 1, 2020
1 parent 663913f commit c7c00e7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class NetworkListener extends BaseListener implements INetworkListener {
deregistrationDelay: props.deregistrationDelay,
healthCheck: props.healthCheck,
port: props.port,
protocol: this.protocol,
protocol: props.protocol ?? this.protocol,
proxyProtocolV2: props.proxyProtocolV2,
targetGroupName: props.targetGroupName,
targets: props.targets,
Expand Down Expand Up @@ -248,6 +248,13 @@ export interface AddNetworkTargetsProps {
*/
readonly port: number;

/**
* Protocol for target group, expects TCP, TLS, UDP, or TCP_UDP.
*
* @default - inherits the protocol of the listener
*/
readonly protocol?: Protocol;

/**
* The targets to add to this target group.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,55 @@ describe('tests', () => {
});
});

test('implicitly created target group but overrides inherited protocol', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Stack');
const lb = new elbv2.NetworkLoadBalancer(stack, 'LB', { vpc });
const cert = new acm.Certificate(stack, 'Certificate', {
domainName: 'example.com',
});

// WHEN
const listener = lb.addListener('Listener', {
port: 443,
protocol: elbv2.Protocol.TLS,
certificates: [elbv2.ListenerCertificate.fromCertificateManager(cert)],
sslPolicy: elbv2.SslPolicy.TLS12,
});

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

// THEN
expect(stack).toHaveResource('AWS::ElasticLoadBalancingV2::Listener', {
Protocol: 'TLS',
Port: 443,
Certificates: [
{ CertificateArn: { Ref: 'Certificate4E7ABB08' } },
],
SslPolicy: 'ELBSecurityPolicy-TLS-1-2-2017-01',
DefaultActions: [
{
TargetGroupArn: { Ref: 'LBListenerTargetsGroup76EF81E8' },
Type: 'forward',
},
],
});
expect(stack).toHaveResource('AWS::ElasticLoadBalancingV2::TargetGroup', {
VpcId: { Ref: 'Stack8A423254' },
Port: 80,
Protocol: 'TCP',
Targets: [
{ Id: 'i-12345' },
],
});
});

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

0 comments on commit c7c00e7

Please sign in to comment.