Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(elbv2): support ALB target for NLB #16687

Merged
merged 9 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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