Skip to content

Commit

Permalink
feat(elbv2): support ALB target for NLB (#16687)
Browse files Browse the repository at this point in the history
This PR allows NLB to have a single ALB as the target.

Fixes: #16679 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
pahud authored Oct 7, 2021
1 parent 8a0d369 commit 27cc821
Show file tree
Hide file tree
Showing 8 changed files with 885 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2';

/**
* A single Application Load Balancer as the target for load balancing.
*/
export class AlbArnTarget implements elbv2.INetworkLoadBalancerTarget {
/**
* Create a new alb target
*
* @param albArn The ARN of the application load balancer to load balance to
* @param port The port on which the target is listening
*/
constructor(private readonly albArn: string, private readonly port: number) {
}

/**
* Register this alb target with a load balancer
*
* Don't call this, it is called automatically when you add the target to a
* load balancer.
*/
public attachToNetworkTargetGroup(targetGroup: elbv2.INetworkTargetGroup): elbv2.LoadBalancerTargetProps {
return this.attach(targetGroup);
}

private attach(_targetGroup: elbv2.ITargetGroup): elbv2.LoadBalancerTargetProps {
return {
targetType: elbv2.TargetType.ALB,
targetJson: { id: this.albArn, port: this.port },
};
}
}

/**
* A single Application Load Balancer as the target for load balancing.
*/
export class AlbTarget extends AlbArnTarget {
/**
* @param alb The application load balancer to load balance to
* @param port The port on which the target is listening
*/
constructor(alb: elbv2.ApplicationLoadBalancer, port: number) {
super(alb.loadBalancerArn, port);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './alb-target';
export * from './ip-target';
export * from './instance-target';
export * from './lambda-target';
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
"@aws-cdk/cdk-integ-tools": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@types/jest": "^26.0.24",
"jest": "^26.6.3"
"jest": "^26.6.3",
"@aws-cdk/aws-ecs": "0.0.0",
"@aws-cdk/aws-ecs-patterns": "0.0.0"
},
"dependencies": {
"@aws-cdk/aws-ec2": "0.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { expect, haveResource } from '@aws-cdk/assert-internal';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2';
import { Stack } from '@aws-cdk/core';
import * as targets from '../lib';

test('Can create target groups with alb target', () => {
// GIVEN
const stack = new Stack();
const vpc = new ec2.Vpc(stack, 'Stack');
const alb = new elbv2.ApplicationLoadBalancer(stack, 'ALB', { vpc });
const nlb = new elbv2.NetworkLoadBalancer(stack, 'NLB', { vpc });
const listener = nlb.addListener('Listener', { port: 80 });

// WHEN
listener.addTargets('Targets', {
targets: [new targets.AlbTarget(alb, 80)],
port: 80,
});

// THEN
expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::TargetGroup', {
Port: 80,
Protocol: 'TCP',
Targets: [
{
Id: {
Ref: 'ALBAEE750D2',
},
Port: 80,
},
],
TargetType: 'alb',
VpcId: {
Ref: 'Stack8A423254',
},
}));
});

test('Can create target groups with alb arn target', () => {
// GIVEN
const stack = new Stack();
const vpc = new ec2.Vpc(stack, 'Stack');
const nlb = new elbv2.NetworkLoadBalancer(stack, 'NLB', { vpc });
const listener = nlb.addListener('Listener', { port: 80 });

// WHEN
listener.addTargets('Targets', {
targets: [new targets.AlbArnTarget('MOCK_ARN', 80)],
port: 80,
});

// THEN
expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::TargetGroup', {
Port: 80,
Protocol: 'TCP',
Targets: [
{
Id: 'MOCK_ARN',
Port: 80,
},
],
TargetType: 'alb',
VpcId: {
Ref: 'Stack8A423254',
},
}));
});
Loading

0 comments on commit 27cc821

Please sign in to comment.