Skip to content

Commit

Permalink
feat(elbv2): allow control of ingress rules on redirect listener (#12768
Browse files Browse the repository at this point in the history
)

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*
  • Loading branch information
MatthewMooreZA authored Feb 25, 2021
1 parent 465cd9c commit b7b441f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-elasticloadbalancingv2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ lb.addRedirect({

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

By default all ingress traffic will be allowed on the source port. If you want to be more selective with your
ingress rules then set `open: false` and use the listener's `connections` object to selectively grant access to the listener.

## Defining a Network Load Balancer

Network Load Balancers are defined in a similar way to Application Load
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class ApplicationLoadBalancer extends BaseLoadBalancer implements IApplic
return this.addListener(`Redirect${sourcePort}To${targetPort}`, {
protocol: props.sourceProtocol ?? ApplicationProtocol.HTTP,
port: sourcePort,
open: true,
open: props.open ?? true,
defaultAction: ListenerAction.redirect({
port: targetPort,
protocol: props.targetProtocol ?? ApplicationProtocol.HTTPS,
Expand Down Expand Up @@ -665,4 +665,19 @@ export interface ApplicationLoadBalancerRedirectConfig {
*/
readonly targetPort?: number;

/**
* Allow anyone to connect to this listener
*
* If this is specified, the listener will be opened up to anyone who can reach it.
* For internal load balancers this is anyone in the same VPC. For public load
* balancers, this is anyone on the internet.
*
* If you want to be more selective about who can access this load
* balancer, set this to `false` and use the listener's `connections`
* object to selectively grant access to the listener.
*
* @default true
*/
readonly open?: boolean;

}
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,31 @@ describe('tests', () => {
});
});

test('Can supress default ingress rules on a simple redirect response', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Stack');

const loadBalancer = new elbv2.ApplicationLoadBalancer(stack, 'LB', {
vpc,
});

// WHEN
loadBalancer.addRedirect({ open: false });

// THEN
expect(stack).not.toHaveResourceLike('AWS::EC2::SecurityGroup', {
SecurityGroupIngress: [
{
CidrIp: '0.0.0.0/0',
Description: 'Allow from anyone on port 80',
IpProtocol: 'tcp',
},
],
});

});

test('Can add simple redirect responses with custom values', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit b7b441f

Please sign in to comment.