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.

Prerequisites
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.
Create a custom Amazon Machine Image (AMI) from your instance. For more information, see Creating an Amazon EBS-Backed Linux AMI or Creating an Instance Store-Backed Linux AMI.
(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 Creating an IAM Role Using the 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/.
In the navigation pane, under LOAD BALANCING, click Load Balancers.
Click Create Load Balancer.
On the Define Load Balancer page, do the following:
Enter a name for the load balancer in Load Balancer name. For example,
my-lb.Select your VPC from Create LB Inside.
Leave the default for Listener Configuration, which is an HTTP listener.
If you selected a nondefault VPC, you are prompted to select subnets under Select Subnets. Click the icon in the Action column for each of your two public subnets.
Click Next: Assign Security Groups.
On the Assign Security Groups page, do the following:
Click Create a new security group.
Enter a name and description for your security group. This new security group contains a rule that allows traffic to the port for your HTTP listener.
Click Next: Configure Security Settings.
On the Configure Security Settings page, click Next: Configure Health Check to continue to the next page, as we are not creating an HTTPS load balancer.
On the Configure Health Check page, do the following:
Leave Ping Protocol and Ping Port at their default values.
Set Ping Path to
/. This sends queries to your default page, whether it is namedindex.htmlor something else.Click Next: Add EC2 Instances.
On the Add EC2 Instances page, click Next: Add Tags to continue to the next page, as we'll use Auto Scaling to add EC2 instances to the load balancer.
On the Add Tags page, add any tags that you need, and then click Review and Create.
On the Review page, review your settings and then click Create. After the load balancer is created, click Close.
In the navigation pane, under AUTO SCALING, click Launch Configurations.
If you are new to Auto Scaling, you see a welcome page. Click Create Auto Scaling group to start the Create Auto Scaling Group wizard, and then click Create launch configuration.
Otherwise, click 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 click Next: Configure details.
On the Configure details page, do the following:
In Name, enter a name for your launch configuration (for example,
my-launch-config).From IAM role, select the IAM role that you created in Prerequisites.
(Optional) If you need to run a startup script, expand Advanced Details and enter the script in User data.
Click Skip to review.
On the Review page, click 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, click Review.
On the Review page, click Create launch configuration.
When prompted, select an existing key pair, create a new key pair, or proceed without a key pair. Click the acknowledgment check box, and then click 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, click Create an Auto Scaling group using this launch configuration.
On the Configure Auto Scaling group details page, do the following:
Enter a name for the Auto Scaling group. For example,
my-asg.In Group size, enter the number of instances in the text box (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.
Expand Advanced Details. Select Receive traffic from Elastic Load Balancer(s). Select your load balancer from the text field.
Click Next: Configure scaling policies.
On the Configure scaling policies page, click Review, as we will simply 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, click Create Auto Scaling group.
After the group is created, click Close.
Test Your Load Balancer
First, verify that Auto Scaling has launched your instances and that they are ready. From the Auto Scaling Groups page, select your Auto Scaling group and then select the Instances tab.

Initially, your instances are in the Pending state.
When their states are InService, they are ready for use.
Next, verify that your instances are registered with the load balancer and ready to receive traffic from the load balancer. From the Load Balancers page, select your load balancer and then select the Instances tab.

If the state of your instances is OutOfService, it's possible that they
are still registering. When their states are InService, they are ready for use.
After your instances are ready, you can test your load balancer as follows.
To test your load balancer
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.comIn a web browser, paste the DNS name for the load balancer into the address bar and press Enter. You'll see your website displayed.

