Skip to content

Commit b7b441f

Browse files
feat(elbv2): allow control of ingress rules on redirect listener (#12768)
This change adds the ability to specify whether you'd like a redirect listener to be open or not. If you don't want the open ingress rule created you'll now be able to do the following: ```ts loadBalancer.addRedirect({ open: false }); ``` fixes #12766 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 465cd9c commit b7b441f

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

packages/@aws-cdk/aws-elasticloadbalancingv2/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ lb.addRedirect({
182182

183183
If you do not provide any options for this method, it redirects HTTP port 80 to HTTPS port 443.
184184

185+
By default all ingress traffic will be allowed on the source port. If you want to be more selective with your
186+
ingress rules then set `open: false` and use the listener's `connections` object to selectively grant access to the listener.
187+
185188
## Defining a Network Load Balancer
186189

187190
Network Load Balancers are defined in a similar way to Application Load

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export class ApplicationLoadBalancer extends BaseLoadBalancer implements IApplic
119119
return this.addListener(`Redirect${sourcePort}To${targetPort}`, {
120120
protocol: props.sourceProtocol ?? ApplicationProtocol.HTTP,
121121
port: sourcePort,
122-
open: true,
122+
open: props.open ?? true,
123123
defaultAction: ListenerAction.redirect({
124124
port: targetPort,
125125
protocol: props.targetProtocol ?? ApplicationProtocol.HTTPS,
@@ -665,4 +665,19 @@ export interface ApplicationLoadBalancerRedirectConfig {
665665
*/
666666
readonly targetPort?: number;
667667

668+
/**
669+
* Allow anyone to connect to this listener
670+
*
671+
* If this is specified, the listener will be opened up to anyone who can reach it.
672+
* For internal load balancers this is anyone in the same VPC. For public load
673+
* balancers, this is anyone on the internet.
674+
*
675+
* If you want to be more selective about who can access this load
676+
* balancer, set this to `false` and use the listener's `connections`
677+
* object to selectively grant access to the listener.
678+
*
679+
* @default true
680+
*/
681+
readonly open?: boolean;
682+
668683
}

packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/listener.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,31 @@ describe('tests', () => {
727727
});
728728
});
729729

730+
test('Can supress default ingress rules on a simple redirect response', () => {
731+
// GIVEN
732+
const stack = new cdk.Stack();
733+
const vpc = new ec2.Vpc(stack, 'Stack');
734+
735+
const loadBalancer = new elbv2.ApplicationLoadBalancer(stack, 'LB', {
736+
vpc,
737+
});
738+
739+
// WHEN
740+
loadBalancer.addRedirect({ open: false });
741+
742+
// THEN
743+
expect(stack).not.toHaveResourceLike('AWS::EC2::SecurityGroup', {
744+
SecurityGroupIngress: [
745+
{
746+
CidrIp: '0.0.0.0/0',
747+
Description: 'Allow from anyone on port 80',
748+
IpProtocol: 'tcp',
749+
},
750+
],
751+
});
752+
753+
});
754+
730755
test('Can add simple redirect responses with custom values', () => {
731756
// GIVEN
732757
const stack = new cdk.Stack();

0 commit comments

Comments
 (0)