-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(elbv2): support ALB target for NLB (#16687)
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
Showing
8 changed files
with
885 additions
and
1 deletion.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
packages/@aws-cdk/aws-elasticloadbalancingv2-targets/lib/alb-target.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/@aws-cdk/aws-elasticloadbalancingv2-targets/lib/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/@aws-cdk/aws-elasticloadbalancingv2-targets/test/alb-target.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
})); | ||
}); |
Oops, something went wrong.