DEV Community

Cover image for Step 4: Create your load balancer
Sebastian Torres
Sebastian Torres

Posted on

Step 4: Create your load balancer

To create your first load balancer, complete the following steps.

To create a load balancer

  • Use the create-load-balancer command to create a load balancer. You must specify two subnets that are not from the same Availability Zone.
$ aws elbv2 create-load-balancer --name my-load-balancer --subnets subnet-b46032ec subnet-a46032fc --security-groups sg-e1fb8c9a
Enter fullscreen mode Exit fullscreen mode

The output includes the Amazon Resource Name (ARN) of the load balancer, with the following format:

arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/my-load-balancer/1234567890123456
Enter fullscreen mode Exit fullscreen mode
  • Use the create-target-group command to create a target group, specifying the same VPC that you used for your EC2 instances.

You can create PIv4 and IPv6 target groups to associate with dualstack load balancers. The target group's IP address type determines the IP version that the load balancer will use to both communicate with, and check the health of, your backend targets.

$ aws elbv2 create-target-group --name my-targets --protocol HTTP --port 80 --vpc-id vpc-2f09a348 --ip-address-type [ipv4 or ipv6]
Enter fullscreen mode Exit fullscreen mode

The output includes the ARN of the target group, with this format:

arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-targets/1234567890123456
Enter fullscreen mode Exit fullscreen mode
  • Use the register-targets command to register your instances with your target group:
$ aws elbv2 register-targets --target-group-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-targets/1234567890123456 --targets Id=i-0146854b7443af453 Id=i-1234567890abcdef0
Enter fullscreen mode Exit fullscreen mode
  • Use the create-listener command to create a listener for your load balancer with a default rule that forwards request to your target group:
$ aws elbv2 create-listener --load-balancer-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/my-load-balancer/1234567890123456 --protocol HTTP --port 80 --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-targets/1234567890123456
Enter fullscreen mode Exit fullscreen mode
  • (Optional) You can verify the health of the registered targets for your target group using this describe-target-health command:
aws elbv2 describe-target-health --target-group-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-targets/1234567890123456
Enter fullscreen mode Exit fullscreen mode

Delete your load balancer

When you no longer need your load balancer and target group, you can delete them as follows:

$ aws elbv2 delete-load-balancer --load-balancer-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/my-load-balancer/1234567890123456
$ aws elbv2 delete-target-group --target-group-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-targets/1234567890123456
Enter fullscreen mode Exit fullscreen mode

Top comments (0)