0
0
AwsHow-ToBeginner · 3 min read

How to Create an Application Load Balancer (ALB) in AWS

To create an Application Load Balancer (ALB) in AWS, use the aws elbv2 create-load-balancer command specifying the name, subnets, and security groups. This sets up a load balancer that distributes incoming traffic across targets like EC2 instances.
📐

Syntax

The basic syntax to create an ALB using AWS CLI is:

  • --name: The name of your load balancer.
  • --subnets: The subnet IDs where the ALB will be placed.
  • --security-groups: Security group IDs to control traffic.
  • --scheme: (Optional) Defines if ALB is internet-facing or internal.
  • --type: Must be application for ALB.
bash
aws elbv2 create-load-balancer --name my-alb --subnets subnet-12345678 subnet-87654321 --security-groups sg-12345678 --scheme internet-facing --type application
💻

Example

This example creates an internet-facing ALB named my-alb in two subnets with a security group attached. It shows the full command and the expected JSON output confirming creation.

bash
aws elbv2 create-load-balancer --name my-alb --subnets subnet-0abc1234 subnet-0def5678 --security-groups sg-0a1b2c3d --scheme internet-facing --type application
Output
{ "LoadBalancers": [ { "LoadBalancerArn": "arn:aws:elasticloadbalancing:region:account-id:loadbalancer/app/my-alb/50dc6c495c0c9188", "DNSName": "my-alb-1234567890.region.elb.amazonaws.com", "CanonicalHostedZoneId": "Z2P70J7EXAMPLE", "CreatedTime": "2024-06-01T12:00:00.000Z", "LoadBalancerName": "my-alb", "Scheme": "internet-facing", "VpcId": "vpc-1a2b3c4d", "State": { "Code": "provisioning" }, "Type": "application", "AvailabilityZones": [ { "ZoneName": "us-west-2a", "SubnetId": "subnet-0abc1234" }, { "ZoneName": "us-west-2b", "SubnetId": "subnet-0def5678" } ], "SecurityGroups": [ "sg-0a1b2c3d" ] } ] }
⚠️

Common Pitfalls

Common mistakes when creating an ALB include:

  • Using subnets from different VPCs, which causes errors.
  • Not attaching a security group that allows inbound traffic on the ALB listener ports (like 80 or 443).
  • Forgetting to specify --type application, which defaults to a Network Load Balancer.
  • Choosing private subnets without proper routing if the ALB is internet-facing.
bash
aws elbv2 create-load-balancer --name my-alb --subnets subnet-0abc1234 subnet-0def5678 --scheme internet-facing

# This command misses --security-groups and --type application, causing failure or wrong load balancer type.

# Corrected command:
aws elbv2 create-load-balancer --name my-alb --subnets subnet-0abc1234 subnet-0def5678 --security-groups sg-0a1b2c3d --scheme internet-facing --type application
📊

Quick Reference

Remember these key points when creating an ALB:

  • Use aws elbv2 create-load-balancer with --type application.
  • Specify at least two subnets in the same VPC for high availability.
  • Attach security groups that allow inbound traffic on your listener ports.
  • Choose internet-facing or internal scheme based on your use case.

Key Takeaways

Use the AWS CLI command 'aws elbv2 create-load-balancer' with '--type application' to create an ALB.
Specify multiple subnets in the same VPC to ensure availability and proper placement.
Attach security groups that allow inbound traffic on the ports your ALB will listen on.
Choose the correct scheme: 'internet-facing' for public access or 'internal' for private use.
Avoid mixing subnets from different VPCs and always specify the load balancer type explicitly.