From 192bab7671361c27b21585b7a9dc8b524664039e Mon Sep 17 00:00:00 2001 From: Mate Gabri Date: Wed, 12 Jun 2019 22:34:09 +1000 Subject: [PATCH] feat(elasticloadbalancing): add crossZone load balancing (#2787) Defaults to cross-zone load balancing for a Classic Load Balancer by default, can be switched off using 'crossZone' property. Closes #2786. --- ...g.asg-w-classic-loadbalancer.expected.json | 1 + .../integ.deployment-group.expected.json | 1 + .../lib/load-balancer.ts | 11 ++++ .../test/integ.elb.expected.json | 3 +- .../test/test.loadbalancer.ts | 59 ++++++++++++++++++- 5 files changed, 73 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-classic-loadbalancer.expected.json b/packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-classic-loadbalancer.expected.json index 53dbaa02298cd..c5482ded0fe5b 100644 --- a/packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-classic-loadbalancer.expected.json +++ b/packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-classic-loadbalancer.expected.json @@ -691,6 +691,7 @@ "Protocol": "http" } ], + "CrossZone": true, "HealthCheck": { "HealthyThreshold": "2", "Interval": "30", diff --git a/packages/@aws-cdk/aws-codedeploy/test/server/integ.deployment-group.expected.json b/packages/@aws-cdk/aws-codedeploy/test/server/integ.deployment-group.expected.json index f6e9aef12d51e..74cf1cf212580 100644 --- a/packages/@aws-cdk/aws-codedeploy/test/server/integ.deployment-group.expected.json +++ b/packages/@aws-cdk/aws-codedeploy/test/server/integ.deployment-group.expected.json @@ -725,6 +725,7 @@ "Protocol": "http" } ], + "CrossZone": true, "Scheme": "internal", "SecurityGroups": [ { diff --git a/packages/@aws-cdk/aws-elasticloadbalancing/lib/load-balancer.ts b/packages/@aws-cdk/aws-elasticloadbalancing/lib/load-balancer.ts index fad3d826d81fc..5eda1e6af5eb7 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancing/lib/load-balancer.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancing/lib/load-balancer.ts @@ -49,6 +49,16 @@ export interface LoadBalancerProps { * @default - None. */ readonly healthCheck?: HealthCheck; + + /** + * Whether cross zone load balancing is enabled + * + * This controls whether the load balancer evenly distributes requests + * across each availability zone + * + * @default true + */ + readonly crossZone?: boolean; } /** @@ -226,6 +236,7 @@ export class LoadBalancer extends Resource implements IConnectable { listeners: Lazy.anyValue({ produce: () => this.listeners }), scheme: props.internetFacing ? 'internet-facing' : 'internal', healthCheck: props.healthCheck && healthCheckToJSON(props.healthCheck), + crossZone: (props.crossZone === undefined || props.crossZone) ? true : false }); if (props.internetFacing) { this.elb.node.addDependency(...subnets.map(s => s.internetConnectivityEstablished)); diff --git a/packages/@aws-cdk/aws-elasticloadbalancing/test/integ.elb.expected.json b/packages/@aws-cdk/aws-elasticloadbalancing/test/integ.elb.expected.json index e880c454c6447..e56301280aec1 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancing/test/integ.elb.expected.json +++ b/packages/@aws-cdk/aws-elasticloadbalancing/test/integ.elb.expected.json @@ -228,6 +228,7 @@ "Protocol": "http" } ], + "CrossZone": true, "HealthCheck": { "HealthyThreshold": "2", "Interval": "30", @@ -255,4 +256,4 @@ ] } } -} +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-elasticloadbalancing/test/test.loadbalancer.ts b/packages/@aws-cdk/aws-elasticloadbalancing/test/test.loadbalancer.ts index 96c94a973def7..e228d4bce9f35 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancing/test/test.loadbalancer.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancing/test/test.loadbalancer.ts @@ -93,7 +93,64 @@ export = { })); test.done(); - } + }, + + 'enable cross zone load balancing'(test: Test) { + // GIVEN + const stack = new Stack(); + const vpc = new Vpc(stack, 'VCP'); + + // WHEN + new LoadBalancer(stack, 'LB', { + vpc, + crossZone: true, + }); + + // THEN + expect(stack).to(haveResource('AWS::ElasticLoadBalancing::LoadBalancer', { + CrossZone: true + })); + + test.done(); + }, + + 'disable cross zone load balancing'(test: Test) { + // GIVEN + const stack = new Stack(); + const vpc = new Vpc(stack, 'VCP'); + + // WHEN + new LoadBalancer(stack, 'LB', { + vpc, + crossZone: false, + }); + + // THEN + expect(stack).to(haveResource('AWS::ElasticLoadBalancing::LoadBalancer', { + CrossZone: false + })); + + test.done(); + }, + + 'cross zone load balancing enabled by default'(test: Test) { + // GIVEN + const stack = new Stack(); + const vpc = new Vpc(stack, 'VCP'); + + // WHEN + new LoadBalancer(stack, 'LB', { + vpc, + }); + + // THEN + expect(stack).to(haveResource('AWS::ElasticLoadBalancing::LoadBalancer', { + CrossZone: true + })); + + test.done(); + }, + }; class FakeTarget implements ILoadBalancerTarget {