Elastic Load Balancing

Elastic Load Balancing

Pro

LocalStack Pro supports Elastic Load Balancing operations for both version 1 and 2.

Application load balancers also support request forwarding to IP address and Lambda targets.

IP Targets

This example illustrates a load balancer configured for an IP target.

Start an HTTP server which will serve as the target of our load balancer.

  1. $ docker run -itd -p 5678:5678 docker.io/hashicorp/http-echo:latest -text 'hello from the IP target'

Create the load balancer, target group and register the target, which is the above Docker container in this case.

  1. $ awslocal ec2 describe-subnets --filters Name=availability-zone,Values=us-east-1f \
  2. | jq .Subnets[].SubnetId
  3. "subnet-22d4f64a"
  4. $ awslocal elbv2 create-load-balancer --name example-lb --subnets subnet-22d4f64a \
  5. | jq '.LoadBalancers[]|.LoadBalancerArn,.DNSName'
  6. "arn:aws:elasticloadbalancing:us-east-1:112233445566:loadbalancer/app/example-lb/50dc6c495c0c9188"
  7. "example-lb.elb.localhost.localstack.cloud"
  8. $ awslocal elbv2 create-target-group --name example-target-group --protocol HTTP --target-type ip \
  9. | jq .TargetGroups[].TargetGroupArn
  10. "arn:aws:elasticloadbalancing:us-east-1:112233445566:targetgroup/example-target-group/50dc6c495c0c9188"
  11. $ awslocal elbv2 register-targets \
  12. --targets Id=127.0.0.1,Port=5678,AvailabilityZone=all \
  13. --target-group-arn arn:aws:elasticloadbalancing:us-east-1:112233445566:targetgroup/example-target-group/50dc6c495c0c9188

Create a listener and a rule so that incoming requests are forwarded to the target.

  1. $ awslocal elbv2 create-listener \
  2. --default-actions '{"Type":"forward","TargetGroupArn":"arn:aws:elasticloadbalancing:us-east-1:112233445566:targetgroup/example-target-group/50dc6c495c0c9188","ForwardConfig":{"TargetGroups":[{"TargetGroupArn":"arn:aws:elasticloadbalancing:us-east-1:112233445566:targetgroup/example-target-group/50dc6c495c0c9188","Weight":11}]}}' \
  3. --load-balancer-arn arn:aws:elasticloadbalancing:us-east-1:112233445566:loadbalancer/app/example-lb/50dc6c495c0c9188 \
  4. | jq '.Listeners[]|.ListenerArn,.Port'
  5. "arn:aws:elasticloadbalancing:us-east-1:112233445566:listener/app/example/50dc6c495c0c9188/4566140160361637296"
  6. 4566
  7. $ awslocal elbv2 create-rule \
  8. --conditions Field=path-pattern,Values=/ \
  9. --priority 1 \
  10. --actions '{"Type":"forward","TargetGroupArn":"arn:aws:elasticloadbalancing:us-east-1:112233445566:targetgroup/example-target-group/50dc6c495c0c9188","ForwardConfig":{"TargetGroups":[{"TargetGroupArn":"arn:aws:elasticloadbalancing:us-east-1:112233445566:targetgroup/example-target-group/50dc6c495c0c9188","Weight":11}]}}' \
  11. --listener-arn arn:aws:elasticloadbalancing:us-east-1:112233445566:listener/app/example/50dc6c495c0c9188/4566140160361637296 \
  12. | jq .Rules[].RuleArn
  13. "arn:aws:elasticloadbalancing:us-east-1:112233445566:listener-rule/app/example/50dc6c495c0c9188/4566140160361637296/60d9790dd44c69e5"

Finally, issue HTTP request to the DNSName parameter of CreateLoadBalancer operation, and Port parameter of CreateListener command. In the above example, these parameter are filtered using jq.

  1. $ curl example-lb.elb.localhost.localstack.cloud:4566
  2. hello from the IP target

Last modified April 25, 2022: Add docs for ELB (#143) (560b3103)