|
| 1 | +# Create an Application Load Balancer with Elastic Load Balancing |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +In this tutorial, you use the AWS CLI to create an Application Load Balancer (ALB) in your default VPC. You create a security group, target group, and HTTP listener, then verify the ALB is active. You then delete all resources during cleanup. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +- AWS CLI installed and configured with appropriate permissions. |
| 10 | +- A default VPC with at least two subnets in different Availability Zones. |
| 11 | +- An IAM principal with permissions for `elbv2:CreateLoadBalancer`, `elbv2:CreateTargetGroup`, `elbv2:CreateListener`, `elbv2:DescribeLoadBalancers`, `elbv2:DeleteLoadBalancer`, `elbv2:DeleteTargetGroup`, `elbv2:DeleteListener`, `ec2:CreateSecurityGroup`, `ec2:AuthorizeSecurityGroupIngress`, `ec2:DeleteSecurityGroup`, `ec2:DescribeVpcs`, and `ec2:DescribeSubnets`. |
| 12 | + |
| 13 | +## Step 1: Get VPC and subnets |
| 14 | + |
| 15 | +Identify the default VPC and select two subnets for the ALB. An ALB requires subnets in at least two Availability Zones. |
| 16 | + |
| 17 | +```bash |
| 18 | +VPC_ID=$(aws ec2 describe-vpcs --filters "Name=isDefault,Values=true" \ |
| 19 | + --query 'Vpcs[0].VpcId' --output text) |
| 20 | + |
| 21 | +SUBNETS=$(aws ec2 describe-subnets --filters "Name=vpc-id,Values=$VPC_ID" \ |
| 22 | + --query 'Subnets[:2].SubnetId' --output text) |
| 23 | +SUBNET1=$(echo "$SUBNETS" | awk '{print $1}') |
| 24 | +SUBNET2=$(echo "$SUBNETS" | awk '{print $2}') |
| 25 | +echo "VPC: $VPC_ID Subnets: $SUBNET1, $SUBNET2" |
| 26 | +``` |
| 27 | + |
| 28 | +## Step 2: Create a security group |
| 29 | + |
| 30 | +Create a security group that allows inbound HTTP traffic on port 80. |
| 31 | + |
| 32 | +```bash |
| 33 | +RANDOM_ID=$(openssl rand -hex 4) |
| 34 | + |
| 35 | +SG_ID=$(aws ec2 create-security-group --group-name "tut-alb-sg-${RANDOM_ID}" \ |
| 36 | + --description "Tutorial ALB security group" --vpc-id "$VPC_ID" \ |
| 37 | + --query 'GroupId' --output text) |
| 38 | + |
| 39 | +aws ec2 authorize-security-group-ingress --group-id "$SG_ID" \ |
| 40 | + --protocol tcp --port 80 --cidr 0.0.0.0/0 > /dev/null |
| 41 | +echo "Security group: $SG_ID" |
| 42 | +``` |
| 43 | + |
| 44 | +This rule allows HTTP traffic from any source. In production, restrict the CIDR to known IP ranges. |
| 45 | + |
| 46 | +## Step 3: Create a target group |
| 47 | + |
| 48 | +Create an IP-based target group. The ALB forwards traffic to targets registered in this group. |
| 49 | + |
| 50 | +```bash |
| 51 | +TG_NAME="tut-tg-${RANDOM_ID}" |
| 52 | + |
| 53 | +TG_ARN=$(aws elbv2 create-target-group --name "$TG_NAME" \ |
| 54 | + --protocol HTTP --port 80 --vpc-id "$VPC_ID" \ |
| 55 | + --target-type ip \ |
| 56 | + --query 'TargetGroups[0].TargetGroupArn' --output text) |
| 57 | +echo "Target group: $TG_ARN" |
| 58 | +``` |
| 59 | + |
| 60 | +Target type `ip` lets you register IP addresses directly. Use `instance` to register EC2 instances by ID instead. |
| 61 | + |
| 62 | +## Step 4: Create the Application Load Balancer |
| 63 | + |
| 64 | +Create the ALB across the two subnets with the security group attached. |
| 65 | + |
| 66 | +```bash |
| 67 | +ALB_NAME="tut-alb-${RANDOM_ID}" |
| 68 | + |
| 69 | +ALB_ARN=$(aws elbv2 create-load-balancer --name "$ALB_NAME" \ |
| 70 | + --subnets $SUBNET1 $SUBNET2 \ |
| 71 | + --security-groups "$SG_ID" \ |
| 72 | + --query 'LoadBalancers[0].LoadBalancerArn' --output text) |
| 73 | +echo "ALB ARN: $ALB_ARN" |
| 74 | +``` |
| 75 | + |
| 76 | +## Step 5: Wait for ALB to be active |
| 77 | + |
| 78 | +The ALB takes 1–2 minutes to provision. Wait for it to reach the `active` state. |
| 79 | + |
| 80 | +```bash |
| 81 | +aws elbv2 wait load-balancer-available --load-balancer-arns "$ALB_ARN" |
| 82 | + |
| 83 | +DNS_NAME=$(aws elbv2 describe-load-balancers --load-balancer-arns "$ALB_ARN" \ |
| 84 | + --query 'LoadBalancers[0].DNSName' --output text) |
| 85 | +echo "DNS: $DNS_NAME" |
| 86 | +``` |
| 87 | + |
| 88 | +The DNS name is publicly resolvable. Without registered targets, requests to this DNS return a 503 error. |
| 89 | + |
| 90 | +## Step 6: Create an HTTP listener |
| 91 | + |
| 92 | +Create a listener on port 80 that forwards traffic to the target group. |
| 93 | + |
| 94 | +```bash |
| 95 | +LISTENER_ARN=$(aws elbv2 create-listener --load-balancer-arn "$ALB_ARN" \ |
| 96 | + --protocol HTTP --port 80 \ |
| 97 | + --default-actions "Type=forward,TargetGroupArn=$TG_ARN" \ |
| 98 | + --query 'Listeners[0].ListenerArn' --output text) |
| 99 | +echo "Listener: $LISTENER_ARN" |
| 100 | +``` |
| 101 | + |
| 102 | +The default action forwards all requests to the target group. You can add rules to route requests based on path or host header. |
| 103 | + |
| 104 | +## Step 7: Describe the ALB |
| 105 | + |
| 106 | +View the ALB configuration. |
| 107 | + |
| 108 | +```bash |
| 109 | +aws elbv2 describe-load-balancers --load-balancer-arns "$ALB_ARN" \ |
| 110 | + --query 'LoadBalancers[0].{Name:LoadBalancerName,DNS:DNSName,State:State.Code,Type:Type}' \ |
| 111 | + --output table |
| 112 | +``` |
| 113 | + |
| 114 | +## Cleanup |
| 115 | + |
| 116 | +Delete resources in reverse order. The ALB must be fully deleted before you can remove the target group. |
| 117 | + |
| 118 | +```bash |
| 119 | +aws elbv2 delete-listener --listener-arn "$LISTENER_ARN" |
| 120 | +aws elbv2 delete-load-balancer --load-balancer-arn "$ALB_ARN" |
| 121 | + |
| 122 | +echo "Waiting for ALB deletion..." |
| 123 | +aws elbv2 wait load-balancers-deleted --load-balancer-arns "$ALB_ARN" |
| 124 | + |
| 125 | +aws elbv2 delete-target-group --target-group-arn "$TG_ARN" |
| 126 | +aws ec2 delete-security-group --group-id "$SG_ID" |
| 127 | +``` |
| 128 | + |
| 129 | +ALBs incur hourly charges (~$0.02/hr) plus data processing fees. Clean up promptly to avoid costs. |
| 130 | + |
| 131 | +The script automates all steps including cleanup: |
| 132 | + |
| 133 | +```bash |
| 134 | +bash elastic-load-balancing-gs.sh |
| 135 | +``` |
| 136 | + |
| 137 | +## Related resources |
| 138 | + |
| 139 | +- [Getting started with Application Load Balancers](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/application-load-balancer-getting-started.html) |
| 140 | +- [Create an Application Load Balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/create-application-load-balancer.html) |
| 141 | +- [Target groups for ALBs](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html) |
| 142 | +- [Elastic Load Balancing pricing](https://aws.amazon.com/elasticloadbalancing/pricing/) |
0 commit comments