Getting Started with AWS
Computing Basics for Windows
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Step 9: Launch Amazon EC2 Instances Using Auto Scaling

Auto Scaling launches and terminates Amazon EC2 instances automatically according to user-defined policies, schedules, and alarms. You can use Auto Scaling to maintain a fleet of Amazon EC2 instances that can adjust to any presented load. You can also use Auto Scaling to bring up multiple instances in a group at one time.

As the name implies, Auto Scaling responds automatically to changing conditions. All you need to do is specify how it should respond to those changes. For example, you can instruct Auto Scaling to launch an additional instance whenever CPU usage on one or more existing instances exceeds 60 percent for ten minutes, or you could tell Auto Scaling to terminate half of your website’s instances over the weekend, when you expect traffic to be low.

Auto Scaling can ensure that the instances in your fleet are performing optimally so that your applications continue to run efficiently. Auto Scaling groups can even work across multiple Availability Zones, so that if an Availability Zone becomes unavailable, Auto Scaling will automatically redistribute traffic to applications in a different Availability Zone. With Auto Scaling, you can ensure that you always have at least one healthy instance running. For more information, see Auto Scaling.

In this example, you will set up the basic infrastructure that must be in place to get Auto Scaling started for most applications. You'll do the following:

  • Create a launch configuration.

  • Create an Auto Scaling group.

  • Create a policy for your Auto Scaling group.

For the purposes of this tutorial, we'll set up an Amazon EC2 application to be load-balanced and auto-scaled with a minimum number of two instances and maximum number of two instances. By setting the minimum and maximum number to be the same, you can ensure that you always have the desired number of instances even if one instance fails. When you create your actual website, as a best practice you should launch sufficient instances across Availability Zones to survive the loss of any one Availability Zone. Additionally, the maximum number of instances must be greater than the minimum to make use of the Auto Scaling feature.

You can control how big your fleet gets by specifying a maximum number of instances. In this example, Auto Scaling is configured to add one instance when there is a increase in load. We will define the policy in this topic, and in the next section we will create a CloudWatch alarm to take action on the policy when the average NetworkOut exceeds a threshold of 6,000,000 bytes for 5 minutes. Auto Scaling and Amazon CloudWatch work together to launch or terminate instances according to the policies you create. To save time, we will create just one policy; however, you can create more policies, such as a policy to terminate instances when load decreases.

If you haven't already installed the Auto Scaling command line tools, you need to do that now. To install the Auto Scaling command line tools to your local computer go to Auto Scaling Command Line Tool. For instructions on configuring your Auto Scaling tools, go to Install the Command Interface in the Auto Scaling Developer Guide.

To set up an auto-scaled, load-balanced Amazon EC2 application

  1. Open a command prompt window: From a local Windows computer, click Start. In the Search box, type cmd and then press Enter.

  2. The launch configuration is a template for the instances you launch in your Auto Scaling group. To define the launch configuration for this example, we will use the as-create-launch-config command. The following parameters define your launch configuration.

    • image-id is the AMI ID. Use the custom AMI ID that you created in Step 6: Create a Custom AMI.

    • instance-type contains basic information, such as operating system, memory, and local storage, about the instance that you will launch. For this example, use the same instance type that you used when your first launched your instance.

    • key is the key pair used to connect to your instances. Use the same key pair that you created when you first launched your instance.

    • group is the security group where you defined the access rules for your instance. Use the same security group that you created when you first launched your instance.

    • monitoring-disabled specifies that you want to use basic monitoring instead of detailed monitoring. By default, detailed monitoring is enabled. For more information about basic and detailed monitoring, go to Amazon CloudWatch.

    We will not specify a region, because we want to use the default region, US East (Virginia). At the command prompt, type the following, and then press Enter:

    PROMPT>as-create-launch-config MyLC --image-id ami-191dc970 --instance-type m1.large --group webappsecuritygroup --key mykeypair --monitoring-disabled

    Auto Scaling returns the following:

    OK-Created launch config

    Note

    You can copy the commands from this document and paste them in the Command Prompt window. To paste the contents in the command line window, right-click in the Command Prompt window, and then click Paste. If you have trouble getting the commands to work, ensure that the command was entered correctly.

    You have now created your launch configuration.

  3. To create an Auto Scaling group in which you can launch multiple Amazon EC2 instances, you will use the as-create-auto-scaling-group command. Use the following parameters to define your Auto Scaling group.

    • launch-configuration is the name of the launch configuration that you created in the previous step.

    • availability-zones specifies the Availability Zones where the Amazon EC2 instances in the Auto Scaling group will be launched. In this example, you will specify two Availability Zones.

      Specifying multiple Availability Zones is a good practice for building fault-tolerant applications. If one Availability Zone experiences an outage, traffic will be routed to another Availability Zone. The number of instances that are launched in the Auto Scaling group will be evenly distributed across the Availability Zones.

    • min-size and max-size set the minimum and maximum number of Amazon EC2 instances in the Auto Scaling group. By setting the minimum and maximum number to be the same, you can fix the number of instances in your group. In this example, set both the minimum and maximum number to 2.

    • load-balancer is the name of the load balancer that is used to route traffic to the Auto Scaling group.

    At the command prompt, type the following, and then press Enter.

    PROMPT>as-create-auto-scaling-group MyAutoScalingGroup --launch-configuration MyLC --availability-zones us-east-1b, us-east-1c  --min-size 2 --max-size 2 --load-balancers MyLB

    Note

    If you do not have permission to launch instances in us-east-1b, then try us-east-1d.

    Auto Scaling returns output similar to the following example:

    OK-Created AutoScalingGroup
  4. To create a policy to enlarge your fleet of instances, we will use the as-put-scaling-policy command. This policy applies to the Auto Scaling group you created in the previous step. Use the following parameters when defining your Auto Scaling policy.

    • auto-scaling-group is the name of the Auto Scaling group that you want to apply the policy to. Use the Auto Scaling group name that you created in the previous step.

    • adjustment is the number of instances you want to increment or decrement. For this example, use 1.

    • type is the type of policy you want to create. For this example, use ChangeInCapacity to change the fleet size of your instances.

    • cooldown is the time, in seconds, after an action before Auto Scaling should evaluate conditions again.

    At the command prompt, type the following, and then press Enter:

    PROMPT>as-put-scaling-policy MyScaleUpPolicy --auto-scaling-group MyAutoScalingGroup --adjustment=1 --type ChangeInCapacity  --cooldown 300

    Auto Scaling returns output similar to the following example:

    POLICY-ARN arn:aws:autoscaling:us-east-1:012345678901:scalingPolicy:cbe7da4e-5d00-4882-900a-2f8113431e30:autoScalingGroupName/MyAutoScalingGroup:policyName/MyScaleUpPolicy

    Note

    To save time, we created only a policy to add an instance. In most cases, you would also create a policy to terminate one or more instances when traffic declines. Auto Scaling can decrease the number of instances when your application doesn't need the resources, saving you money. To create a policy for terminating an instance, start from the policy you just created, change the policy name, and then change the value of adjustment from 1 to -1. You use "--adjustment=-1" on a Windows machine.

    At the command prompt, type the following, and then press Enter:

    PROMPT>as-put-scaling-policy MyScaleDownPolicy --auto-scaling-group MyAutoScalingGroup "--adjustment=-1" --type ChangeInCapacity --cooldown 300
  5. To verify that your Auto Scaling group exists, we’ll use the as-describe-auto-scaling-groups command. At the command prompt, type the following, and then press Enter:

    PROMPT>as-describe-auto-scaling-groups MyAutoScalingGroup --headers

    Auto Scaling returns the following:

    AUTO-SCALING-GROUP  GROUP-NAME          LAUNCH-CONFIG  AVAILABILITY-ZONES     MIN-SIZE  MAX-SIZE  DESIRED-CAPACITY
    AUTO-SCALING-GROUP  MyAutoScalingGroup  MyLC           us-east-1b,us-east-1c  2         2         2
    INSTANCE  INSTANCE-ID  AVAILABILITY-ZONE  STATE      STATUS   LAUNCH-CONFIG
    INSTANCE  i-xxxxxxxx   us-east-1b         InService  Healthy  MyLC
    INSTANCE  i-xxxxxxxx   us-east-1c         InService  Healthy  MyLC
    

Your Amazon EC2 application has been launched as an auto-scaled and load-balanced application.

For more information about Auto Scaling, see the Auto Scaling Documentation.

Caution

You will continue to incur costs as long as your Amazon EC2 instances are running. If at any time you want to terminate these instances, see Terminate Your Amazon EC2 Instances in Your Auto Scaling Group.

Where You're At

Here's where you are in building your architecture.

Now that you have created your Auto Scaling group and your Amazon EC2 instance is up and running, you'll want a way to monitor the health of your instance. In the next step, you'll create an Amazon CloudWatch alarm to track the Auto Scaling policy you just created.