Creating an Application Load Balancer using the AWS Management Console
Step 1: Log in to your AWS Management Console.
Step 2: Navigate to the EC2 service and click on “Load Balancers” in the left-hand menu.
Step 3: Click on the “Create Load Balancer” button.
Step 4: Select “Application Load Balancer” as the type of load balancer you want to create.
Step 5: Configure the basic settings for your load balancer, including the name, scheme (Internet-facing or internal), and the VPC in which you want to create the load balancer.
Step 6: Configure the listeners for your load balancer. You can add one or more listeners, which specify the protocol and port that the load balancer should use to route traffic to your target groups.
Step 7: Configure the target groups for your load balancer. Target groups specify the instances or IP addresses that the load balancer should route traffic to based on the rules you define.
Step 8: Configure health checks for your target groups. Health checks ensure that the load balancer only routes traffic to healthy instances or IP addresses.
Step 9: Review your load balancer configuration and click on the “Create” button.
Creating an Application Load Balancer using the AWS CLI
Step 1: Install and configure the AWS CLI if you haven’t already.
Step 2: Create a JSON file that contains the configuration for your load balancer. Here’s an example configuration file:
{
"Name": "my-load-balancer",
"Subnets": ["subnet-abc123", "subnet-def456"],
"SecurityGroups": ["sg-123abc"],
"Listeners": [
{
"Protocol": "HTTP",
"Port": 80,
"DefaultActions": [
{
"Type": "forward",
"TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-target-group/abc123"
}
]
}
]
}
In this example, we’re creating an ALB named “my-load-balancer” that uses two subnets and one security group. We’re also creating an HTTP listener that routes traffic to a target group with the ARN “arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-target-group/abc123”.
Step 3: Use the create-load-balancer
command to create your load balancer:
aws elbv2 create-load-balancer --cli-input-json file://path/to/your/config.json
Make sure to replace “path/to/your/config.json” with the actual path to your configuration file.
Step 4: Use the create-target-group
command to create a target group:
aws elbv2 create-target-group --name my-target-group --protocol HTTP --port 80 --vpc-id vpc-abc123
In this example, we’re creating a target group named “my-target-group” that uses HTTP on port 80 and is associated with the VPC with the ID “vpc-abc123”.
Step 5: Use the register-targets
command to register instances or IP addresses with your target group:
aws elbv2 register-targets --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-target-group/abc123 --targets Id=i-abc1234567890,Port=80 Id=10.0.1.1,Port=80
In this example, we’re registering two targets: an EC2 instance with the ID “i-abc1234567890” and an IP address “10.0.1.1” both on port 80.
Step 6: Use the `create-listener` command to create a listener for your load balancer:
aws elbv2 create-listener –load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/my-load-balancer/abc123 –protocol HTTP –port 80 –default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-target-group/abc123
In this example, we’re creating an HTTP listener on port 80 that forwards traffic to our target group with the ARN “arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-target-group/abc123”.
Step 7: Verify that your load balancer is working correctly by sending traffic to it and checking that it’s being distributed to your targets correctly.
And that's it! You've now created an AWS Application Load Balancer using both the AWS Management Console and the AWS CLI.