Use the following procedure to create a load balancer, create a launch configuration for your instances, create an Auto Scaling group with two or more instances, and associate the load balancer with the Auto Scaling group.
To scale and load-balance your application
- Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
- On the navigation pane, under LOAD BALANCING, choose Load Balancers.
- Choose Create Load Balancer.
- For Application Load Balancer, choose Create.
- On the Configure Load Balancer page, do the following:
- For Name, type a name for your load balancer. For example,
my-lb
. - For Scheme, keep the default value, internet-facing.
- For Listeners, keep the default, which is a listener that accepts HTTP traffic on port 80.
- For Availability Zones, select the VPC that you used for your instances. Select an Availability Zone and then select the public subnet for that Availability Zone. Repeat for a second Availability Zone.
- Choose Next: Configure Security Settings.
- For Name, type a name for your load balancer. For example,
- For this tutorial, you are not using a secure listener. Choose Next: Configure Security Groups.
- On the Configure Security Groups page, do the following:
- Choose Create a new security group.
- Type a name and description for the security group, or keep the default name and description. This new security group contains a rule that allows traffic to the port configured for the listener.
- Choose Next: Configure Routing.
- On the Configure Routing page, do the following:
- For Target group, keep the default, New target group.
- For Name, type a name for the target group.
- Keep Protocol as HTTP, Port as 80, and Target type as instance.
- For Health checks, keep the default protocol and path.
- Choose Next: Register Targets.
- On the Register Targets page, choose Next: Review to continue to the next page, as we’ll use Amazon EC2 Auto Scaling to add EC2 instances to the target group.
- On the Review page, choose Create. After the load balancer is created, choose Close.
- On the navigation pane, under AUTO SCALING, choose Launch Configurations.
- If you are new to Amazon EC2 Auto Scaling, you see a welcome page. Choose Create Auto Scaling group to start the Create Auto Scaling Group wizard, and then choose Create launch configuration.
- Otherwise, choose Create launch configuration.
- On the Choose AMI page, select the My AMIs tab, and then select the AMI that you created in Prerequisites.
- On the Choose Instance Type page, select an instance type, and then choose Next: Configure details.
- On the Configure details page, do the following:
- For Name, type a name for your launch configuration (for example,
my-launch-config
). - For IAM role, select the IAM role that you created in Prerequisites.
- (Optional) If you need to run a startup script, expand Advanced Details and type the script in User data.
- Choose Skip to review.
- For Name, type a name for your launch configuration (for example,
- On the Review page, choose Edit security groups. You can select an existing security group or create a new one. This security group must allow HTTP traffic and health checks from the load balancer. If your instances will have public IP addresses, you can optionally allow SSHtraffic if you need to connect to the instances. When you are finished, choose Review.
- On the Review page, choose Create launch configuration.
- When prompted, select an existing key pair, create a new key pair, or proceed without a key pair. Select the acknowledgment check box, and then choose Create launch configuration.
- After the launch configuration is created, you must create an Auto Scaling group.
- If you are new to Amazon EC2 Auto Scaling and you are using the Create Auto Scaling group wizard, you are taken to the next step automatically.
- Otherwise, choose Create an Auto Scaling group using this launch configuration.
- On the Configure Auto Scaling group details page, do the following:
- For Group name, type a name for the Auto Scaling group. For example,
my-asg
. - For Group size, type the number of instances (for example,
2
). Note that we recommend that you maintain approximately the same number of instances in each Availability Zone. - Select your VPC from Network and your two public subnets from Subnet.
- Under Advanced Details, select Receive traffic from one or more load balancers. Select your target group from Target Groups.
- Choose Next: Configure scaling policies.
- For Group name, type a name for the Auto Scaling group. For example,
- On the Configure scaling policies page, choose Review, as we will let Amazon EC2 Auto Scaling maintain the group at the specified size. Note that later on, you can manually scale this Auto Scaling group, configure the group to scale on a schedule, or configure the group to scale based on demand.
- On the Review page, choose Create Auto Scaling group.
- After the group is created, choose Close.
Test Your Load Balancer
When a client sends a request to your load balancer, the load balancer routes the request to one of its registered instances.
To test your load balancer
- Verify that your instances are ready. From the Auto Scaling Groups page, select your Auto Scaling group, and then choose the Instances tab. Initially, your instances are in the
Pending
state. When their states areInService
, they are ready for use. - Verify that your instances are registered with the load balancer. From the Target Groupspage, select your target group, and then choose the Targets tab. If the state of your instances is
initial
, it’s possible that they are still registering. When the state of your instances ishealthy
, they are ready for use. After your instances are ready, you can test your load balancer as follows. - From the Load Balancers page, select your load balancer.
- On the Description tab, locate the DNS name. This name has the following form:
my-lb
-xxxxxxxxxx
.us-west-2
.elb.amazonaws.com - In a web browser, paste the DNS name for the load balancer into the address bar and press Enter. You’ll see your website displayed.