Skip to content

Commit

Permalink
feat(elasticloadbalancing): add crossZone load balancing (#2787)
Browse files Browse the repository at this point in the history
Defaults to cross-zone load balancing for a Classic Load Balancer by default, can be switched
off using 'crossZone' property.

Closes #2786.
  • Loading branch information
ScOut3R authored and rix0rrr committed Jun 12, 2019
1 parent 8b1f3ed commit 192bab7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@
"Protocol": "http"
}
],
"CrossZone": true,
"HealthCheck": {
"HealthyThreshold": "2",
"Interval": "30",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@
"Protocol": "http"
}
],
"CrossZone": true,
"Scheme": "internal",
"SecurityGroups": [
{
Expand Down
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-elasticloadbalancing/lib/load-balancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
"Protocol": "http"
}
],
"CrossZone": true,
"HealthCheck": {
"HealthyThreshold": "2",
"Interval": "30",
Expand Down Expand Up @@ -255,4 +256,4 @@
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 192bab7

Please sign in to comment.