0
0
AWScloud~5 mins

Cross-zone load balancing in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you have servers in different locations to handle your app traffic, you want to share the work evenly. Cross-zone load balancing helps send users to servers in all locations fairly, so no server gets too busy or too quiet.
When your app runs in multiple data centers or zones and you want to spread user requests evenly across all servers.
When some zones have more servers than others and you want to make sure all servers get a fair share of traffic.
When you want to improve your app's availability by using servers from all zones even if one zone is busier.
When you notice some servers are overloaded while others are idle in different zones.
When you want to avoid users being sent only to servers in their own zone, which can cause uneven load.
Config File - elb-cross-zone-config.json
elb-cross-zone-config.json
{
  "LoadBalancerName": "my-app-elb",
  "Listeners": [
    {
      "Protocol": "HTTP",
      "LoadBalancerPort": 80,
      "InstanceProtocol": "HTTP",
      "InstancePort": 80
    }
  ],
  "AvailabilityZones": ["us-east-1a", "us-east-1b"],
  "CrossZoneLoadBalancing": {
    "Enabled": true
  }
}

This JSON configures an Elastic Load Balancer (ELB) named my-app-elb that listens on HTTP port 80. It is set to work across two availability zones: us-east-1a and us-east-1b. The key part is CrossZoneLoadBalancing.Enabled: true, which tells AWS to share incoming traffic evenly across all servers in both zones, not just within each zone.

Commands
This command creates a new Elastic Load Balancer named 'my-app-elb' that listens on HTTP port 80 and spans two availability zones to distribute traffic.
Terminal
aws elb create-load-balancer --load-balancer-name my-app-elb --listeners Protocol=HTTP,LoadBalancerPort=80,InstanceProtocol=HTTP,InstancePort=80 --availability-zones us-east-1a us-east-1b
Expected OutputExpected
{ "DNSName": "my-app-elb-1234567890.us-east-1.elb.amazonaws.com" }
--load-balancer-name - Sets the name of the load balancer.
--listeners - Defines the protocol and ports for incoming and backend traffic.
--availability-zones - Specifies which zones the load balancer will cover.
This command enables cross-zone load balancing on the existing load balancer to ensure traffic is spread evenly across all zones.
Terminal
aws elb modify-load-balancer-attributes --load-balancer-name my-app-elb --load-balancer-attributes '{"CrossZoneLoadBalancing":{"Enabled":true}}'
Expected OutputExpected
{ "LoadBalancerName": "my-app-elb", "LoadBalancerAttributes": { "CrossZoneLoadBalancing": { "Enabled": true } } }
--load-balancer-name - Specifies which load balancer to modify.
--load-balancer-attributes - Sets the attributes, here enabling cross-zone load balancing.
This command checks the current settings of the load balancer to confirm cross-zone load balancing is enabled.
Terminal
aws elb describe-load-balancer-attributes --load-balancer-name my-app-elb
Expected OutputExpected
{ "LoadBalancerAttributes": { "CrossZoneLoadBalancing": { "Enabled": true }, "AccessLog": { "Enabled": false }, "ConnectionDraining": { "Enabled": false } } }
--load-balancer-name - Specifies which load balancer to inspect.
Key Concept

If you remember nothing else from this pattern, remember: enabling cross-zone load balancing makes sure all your servers in different zones share the work fairly.

Common Mistakes
Not enabling cross-zone load balancing after creating the load balancer.
Traffic will only be balanced within each zone, causing uneven load if zones have different numbers of servers.
Run the modify-load-balancer-attributes command to enable cross-zone load balancing.
Specifying only one availability zone when creating the load balancer.
The load balancer cannot distribute traffic across multiple zones if only one zone is included.
Include all desired availability zones in the create-load-balancer command.
Assuming cross-zone load balancing is enabled by default.
By default, it is disabled for some load balancer types, so traffic may not be evenly distributed.
Always verify and explicitly enable cross-zone load balancing if needed.
Summary
Create an Elastic Load Balancer spanning multiple availability zones to distribute traffic.
Enable cross-zone load balancing to share traffic evenly across all servers in all zones.
Verify the load balancer attributes to confirm cross-zone load balancing is active.