Tutorial: Increase the Availability of Your Application on Amazon EC2
Suppose that you start out running your app or website on a single EC2 instance, and over time, traffic increases to the point that you require more than one instance to meet the demand. You can launch multiple EC2 instances from your AMI and then use Elastic Load Balancing to distribute incoming traffic for your application across these EC2 instances. This increases the availability of your application. Placing your instances in multiple Availability Zones also improves the fault tolerance in your application. If one Availability Zone experiences an outage, traffic is routed to the other Availability Zone.
You can use Auto Scaling to maintain a minimum number of running instances for your application at all times. Auto Scaling can detect when your instance or application is unhealthy and replace it automatically to maintain the availability of your application. You can also use Auto Scaling to scale your Amazon EC2 capacity up or down automatically based on demand, using criteria that you specify.
In this tutorial, we use Auto Scaling with Elastic Load Balancing to ensure that you maintain a specified number of healthy EC2 instances behind your load balancer. Note that these instances do not need public IP addresses, because traffic goes to the load balancer and is then routed to the instances. For more information, see Auto Scaling and Elastic Load Balancing.
This tutorial assumes that you have already done the following:
If you don't have a default virtual private cloud (VPC), create a VPC with one public subnet in two or more Availability Zones. For more information, see Create a Virtual Private Cloud (VPC).
Launch an instance in the VPC.
Connect to the instance and customize it. For example, you can install software and applications, copy data, and attach additional EBS volumes. For information about setting up a web server on your instance, see Tutorial: Installing a LAMP Web Server on Amazon Linux.
Test your application on your instance to ensure that your instance is configured correctly.
(Optional) Terminate the instance if you no longer need it.
Create an IAM role that grants your application the access to AWS that it needs. For more information, see To create an IAM role using the IAM console.
Scale and Load Balance Your Application
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.
Choose Application Load Balancer, and then choose Continue.
On the Configure Load Balancer page, do the following:
For Name, type a name for your load balancer. For example,
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 VPC, select the same VPC that you used for your instances.
For Available subnets, select at least two public subnets using their add icons. The subnets are moved under Selected subnets. Note that you can select only one subnet per Availability Zone. If you select a subnet from an Availability Zone where there is already a selected subnet, this subnet replaces the currently selected subnet for the Availability Zone.
Choose Next: Configure Security Settings.
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 and Port as 80.
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 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 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,
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.
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 SSH traffic 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 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,
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.
On the Configure scaling policies page, choose Review, as we will let 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
Pendingstate. When their states are
InService, they are ready for use.
Verify that your instances are registered with the load balancer. From the Target Groups page, 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 is
healthy, 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:
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.