Skip to content

Commit 7dfd6be

Browse files
parisholleyrix0rrr
authored andcommitted
fix(elbv2): fix cross-stack use of ALB (#4111)
Create the security group rules in the stack of the Load Balancing Target, rather than the stack of the Load Balancer itself. This is better in nearly all interesting cases, where we have long-running services that register themselves into a potentially shared ALB.
1 parent 8911e7a commit 7dfd6be

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

packages/@aws-cdk/aws-ecs/lib/base/base-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,4 +535,4 @@ export enum PropagatedTagSource {
535535
* Do not propagate
536536
*/
537537
NONE = 'NONE'
538-
}
538+
}

packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-listener.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
249249
* Don't call this directly. It is called by ApplicationTargetGroup.
250250
*/
251251
public registerConnectable(connectable: ec2.IConnectable, portRange: ec2.Port): void {
252-
this.connections.allowTo(connectable, portRange, 'Load balancer to target');
252+
connectable.connections.allowFrom(this.loadBalancer, portRange, 'Load balancer to target');
253253
}
254254

255255
/**

packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/test.security-groups.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,43 @@ export = {
9999
test.done();
100100
},
101101

102+
'ingress is added to child stack SG instead of parent stack'(test: Test) {
103+
// GIVEN
104+
const fixture = new TestFixture(true);
105+
106+
const parentGroup = new elbv2.ApplicationTargetGroup(fixture.stack, 'TargetGroup', {
107+
vpc: fixture.vpc,
108+
port: 8008,
109+
targets: [new FakeSelfRegisteringTarget(fixture.stack, 'Target', fixture.vpc)],
110+
});
111+
112+
// listener requires at least one rule for ParentStack to create
113+
fixture.listener.addTargetGroups('Default', { targetGroups: [parentGroup] });
114+
115+
const childStack = new cdk.Stack(fixture.app, 'childStack');
116+
117+
// WHEN
118+
const childGroup = new elbv2.ApplicationTargetGroup(childStack, 'TargetGroup', {
119+
// We're assuming the 2nd VPC is peered to the 1st, or something.
120+
vpc: fixture.vpc,
121+
port: 8008,
122+
targets: [new FakeSelfRegisteringTarget(childStack, 'Target', fixture.vpc)],
123+
});
124+
125+
new elbv2.ApplicationListenerRule(childStack, 'ListenerRule', {
126+
listener: fixture.listener,
127+
targetGroups: [childGroup],
128+
priority: 100,
129+
hostHeader: 'www.foo.com'
130+
});
131+
132+
// THEN
133+
expectSameStackSGRules(fixture.stack);
134+
expectedImportedSGRules(childStack);
135+
136+
test.done();
137+
},
138+
102139
'SG peering works on exported/imported load balancer'(test: Test) {
103140
// GIVEN
104141
const fixture = new TestFixture(false);

0 commit comments

Comments
 (0)