Skip to content

Commit 192bab7

Browse files
ScOut3Rrix0rrr
authored andcommitted
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.
1 parent 8b1f3ed commit 192bab7

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

packages/@aws-cdk/aws-autoscaling/test/integ.asg-w-classic-loadbalancer.expected.json

+1
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,7 @@
691691
"Protocol": "http"
692692
}
693693
],
694+
"CrossZone": true,
694695
"HealthCheck": {
695696
"HealthyThreshold": "2",
696697
"Interval": "30",

packages/@aws-cdk/aws-codedeploy/test/server/integ.deployment-group.expected.json

+1
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@
725725
"Protocol": "http"
726726
}
727727
],
728+
"CrossZone": true,
728729
"Scheme": "internal",
729730
"SecurityGroups": [
730731
{

packages/@aws-cdk/aws-elasticloadbalancing/lib/load-balancer.ts

+11
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ export interface LoadBalancerProps {
4949
* @default - None.
5050
*/
5151
readonly healthCheck?: HealthCheck;
52+
53+
/**
54+
* Whether cross zone load balancing is enabled
55+
*
56+
* This controls whether the load balancer evenly distributes requests
57+
* across each availability zone
58+
*
59+
* @default true
60+
*/
61+
readonly crossZone?: boolean;
5262
}
5363

5464
/**
@@ -226,6 +236,7 @@ export class LoadBalancer extends Resource implements IConnectable {
226236
listeners: Lazy.anyValue({ produce: () => this.listeners }),
227237
scheme: props.internetFacing ? 'internet-facing' : 'internal',
228238
healthCheck: props.healthCheck && healthCheckToJSON(props.healthCheck),
239+
crossZone: (props.crossZone === undefined || props.crossZone) ? true : false
229240
});
230241
if (props.internetFacing) {
231242
this.elb.node.addDependency(...subnets.map(s => s.internetConnectivityEstablished));

packages/@aws-cdk/aws-elasticloadbalancing/test/integ.elb.expected.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@
228228
"Protocol": "http"
229229
}
230230
],
231+
"CrossZone": true,
231232
"HealthCheck": {
232233
"HealthyThreshold": "2",
233234
"Interval": "30",
@@ -255,4 +256,4 @@
255256
]
256257
}
257258
}
258-
}
259+
}

packages/@aws-cdk/aws-elasticloadbalancing/test/test.loadbalancer.ts

+58-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,64 @@ export = {
9393
}));
9494

9595
test.done();
96-
}
96+
},
97+
98+
'enable cross zone load balancing'(test: Test) {
99+
// GIVEN
100+
const stack = new Stack();
101+
const vpc = new Vpc(stack, 'VCP');
102+
103+
// WHEN
104+
new LoadBalancer(stack, 'LB', {
105+
vpc,
106+
crossZone: true,
107+
});
108+
109+
// THEN
110+
expect(stack).to(haveResource('AWS::ElasticLoadBalancing::LoadBalancer', {
111+
CrossZone: true
112+
}));
113+
114+
test.done();
115+
},
116+
117+
'disable cross zone load balancing'(test: Test) {
118+
// GIVEN
119+
const stack = new Stack();
120+
const vpc = new Vpc(stack, 'VCP');
121+
122+
// WHEN
123+
new LoadBalancer(stack, 'LB', {
124+
vpc,
125+
crossZone: false,
126+
});
127+
128+
// THEN
129+
expect(stack).to(haveResource('AWS::ElasticLoadBalancing::LoadBalancer', {
130+
CrossZone: false
131+
}));
132+
133+
test.done();
134+
},
135+
136+
'cross zone load balancing enabled by default'(test: Test) {
137+
// GIVEN
138+
const stack = new Stack();
139+
const vpc = new Vpc(stack, 'VCP');
140+
141+
// WHEN
142+
new LoadBalancer(stack, 'LB', {
143+
vpc,
144+
});
145+
146+
// THEN
147+
expect(stack).to(haveResource('AWS::ElasticLoadBalancing::LoadBalancer', {
148+
CrossZone: true
149+
}));
150+
151+
test.done();
152+
},
153+
97154
};
98155

99156
class FakeTarget implements ILoadBalancerTarget {

0 commit comments

Comments
 (0)