Menu
Amazon Elastic Compute Cloud
User Guide for Linux Instances

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.


			Architecture with Auto Scaling and Elastic Load Balancing

Prerequisites

This tutorial assumes that you have already done the following:

  1. 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).

  2. Launch an instance in the VPC.

  3. 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.

  4. Test your application on your instance to ensure that your instance is configured correctly.

  5. 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.

  6. (Optional) Terminate the instance if you no longer need it.

  7. 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

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. On the navigation pane, under LOAD BALANCING, choose Load Balancers.

  3. Choose Create Load Balancer.

  4. Choose Application Load Balancer, and then choose Continue.

  5. On the Configure Load Balancer page, do the following:

    1. For Name, type a name for your load balancer. For example, my-lb.

    2. For Scheme, keep the default value, internet-facing.

    3. For Listeners, keep the default, which is a listener that accepts HTTP traffic on port 80.

    4. For Availability Zones, select the VPC that you used for your instances. Select an Availability Zone and then select the public subset for that Availability Zone. Repeat for a second Availability Zone.

    5. Choose Next: Configure Security Settings.

  6. For this tutorial, you are not using a secure listener. Choose Next: Configure Security Groups.

  7. On the Configure Security Groups page, do the following:

    1. Choose Create a new security group.

    2. 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.

    3. Choose Next: Configure Routing.

  8. On the Configure Routing page, do the following:

    1. For Target group, keep the default, New target group.

    2. For Name, type a name for the target group.

    3. Keep Protocol as HTTP and Port as 80.

    4. For Health checks, keep the default protocol and path.

    5. Choose Next: Register Targets.

  9. 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.

  10. On the Review page, choose Create. After the load balancer is created, choose Close.

  11. 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.

  12. On the Choose AMI page, select the My AMIs tab, and then select the AMI that you created in Prerequisites.

  13. On the Choose Instance Type page, select an instance type, and then choose Next: Configure details.

  14. On the Configure details page, do the following:

    1. For Name, type a name for your launch configuration (for example, my-launch-config).

    2. For IAM role, select the IAM role that you created in Prerequisites.

    3. (Optional) If you need to run a startup script, expand Advanced Details and type the script in User data.

    4. Choose Skip to review.

  15. 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.

  16. On the Review page, choose Create launch configuration.

  17. 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.

  18. 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.

  19. On the Configure Auto Scaling group details page, do the following:

    1. For Group name, type a name for the Auto Scaling group. For example, my-asg.

    2. 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.

    3. Select your VPC from Network and your two public subnets from Subnet.

    4. Under Advanced Details, select Receive traffic from one or more load balancers. Select your target group from Target Groups.

    5. Choose Next: Configure scaling policies.

  20. 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.

  21. On the Review page, choose Create Auto Scaling group.

  22. 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

  1. 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 are InService, they are ready for use.

  2. 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.

  3. From the Load Balancers page, select your load balancer.

  4. On the Description tab, locate the DNS name. This name has the following form:

    my-lb-xxxxxxxxxx.us-west-2.elb.amazonaws.com
  5. 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.