diff --git a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/nlb/network-listener.ts b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/nlb/network-listener.ts index 99c89342fa7a2..cb3ef4787cf99 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/nlb/network-listener.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/nlb/network-listener.ts @@ -67,6 +67,7 @@ export class NetworkListener extends BaseListener implements INetworkListener { // New default target(s) for (const targetGroup of targetGroups) { this._addDefaultTargetGroup(targetGroup); + targetGroup.registerListener(this); } } diff --git a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/nlb/network-target-group.ts b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/nlb/network-target-group.ts index 4cb59fbdfad2e..2d51b48855709 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/nlb/network-target-group.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/nlb/network-target-group.ts @@ -80,12 +80,22 @@ export class NetworkTargetGroup extends BaseTargetGroup { */ // tslint:disable-next-line:no-empty-interface export interface INetworkTargetGroup extends ITargetGroup { + /** + * Register a listener that is load balancing to this target group. + * + * Don't call this directly. It will be called by listeners. + */ + registerListener(listener: INetworkListener): void; } /** * An imported network target group */ class ImportedNetworkTargetGroup extends BaseImportedTargetGroup implements INetworkTargetGroup { + public registerListener(_listener: INetworkListener) { + // Nothing to do, we know nothing of our members + } + public listenerDependency(): cdk.IDependable { return new LazyDependable([]); } diff --git a/packages/@aws-cdk/aws-elasticloadbalancingv2/test/nlb/test.listener.ts b/packages/@aws-cdk/aws-elasticloadbalancingv2/test/nlb/test.listener.ts index 8c32eff786f7a..4091cf386323d 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancingv2/test/nlb/test.listener.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancingv2/test/nlb/test.listener.ts @@ -1,4 +1,4 @@ -import { expect, haveResource } from '@aws-cdk/assert'; +import { expect, haveResource, MatchStyle } from '@aws-cdk/assert'; import ec2 = require('@aws-cdk/aws-ec2'); import cdk = require('@aws-cdk/cdk'); import { Test } from 'nodeunit'; @@ -112,4 +112,34 @@ export = { test.done(); }, + + 'Enable taking a dependency on an NLB target group\'s load balancer'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.VpcNetwork(stack, 'Stack'); + const lb = new elbv2.NetworkLoadBalancer(stack, 'LB', { vpc }); + const listener = lb.addListener('Listener', { port: 443 }); + const group = listener.addTargets('Group', { + port: 80, + targets: [new FakeSelfRegisteringTarget(stack, 'Target', vpc)] + }); + + // WHEN + const resource = new cdk.Resource(stack, 'MyResource', { + type: 'SomeResource', + }); + resource.addDependency(group.listenerDependency()); + + // THEN + expect(stack).toMatch({ + Resources: { + MyResource: { + Type: "SomeResource", + DependsOn: [ "LBListener49E825B4" ] + } + } + }, MatchStyle.SUPERSET); + + test.done(); + }, };