| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
Auto Scaling is designed to launch or terminate Amazon EC2 instances automatically based on user-defined policies, schedules, and alarms. You can use Auto Scaling to maintain a fleet of Amazon EC2 instances that can handle any presented load. As its 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 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. You can also use Auto Scaling to 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—distinct physical locations for the hosted Amazon EC2 instances—so that if an Availability Zone becomes unavailable, Auto Scaling will automatically redistribute applications to 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, we will set up the basic infrastructure that must be in place to get Auto Scaling started for most applications. We'll set up an Amazon EC2 application to be load-balanced and auto-scaled with a minimum number of one instance and maximum number of one instance so you are only charged for one instance. However, when you create your actual website you should follow the best practice of having sufficient instances across Availability Zones to survive the loss of any one Availability Zone. Additionally, increase your maximum number of instances to be greater than your minimum to make use of the Auto Scaling feature. You can also specify the maximum number of instances to control your fleet size. Auto Scaling in this example is configured to scale out by one when there is a change in capacity. We define the policy in this topic and then create a CloudWatch alarm in the next section to take action on the policy when the average CPU usage exceeds a threshold of 60 percent for 10 minutes. Auto Scaling and Amazon CloudWatch work together to launch or terminate instances based on the policies you create. To save time, we will create just one policy, however, you can create more policies, such as a scale-in policy.
If you haven't already installed the Auto Scaling command line tools, you need to do that now at Using the Command Line Tools in the Auto Scaling DeveloperGuide. We will use the command line tools to set up Auto Scaling.
To set up an auto-scaled, load-balanced Amazon EC2 application
Open a command prompt window. In Microsoft Windows, start the Command Prompt application (from the Start menu, click Programs, click Accessories, and then click Command Prompt).
Use the Auto Scaling as-create-launch-config command.
In this example, we use a publicly available Windows AMI running Microsoft Windows Server 2008 and Microsoft Internet Information Services (IIS). We use an t1.micro instance type, and use our security group and our key pair we created in the previous steps. In this example, the key pair file is located in the directory in which we are creating our Auto Scaling group. We will not specify a region because we want to use the default region, US East (Virginia).
Note
The AMI that is used in this example is part of the AWS Free Usage Tier. If you are eligible for the free tier, then you will not be charged for launching the Amazon EC2 instance. If you are not eligible for the AWS Free Usage Tier, the charges in this example are minimal. For more information about Amazon EC2 pricing, see the Amazon Elastic Compute Cloud (Amazon EC2) details page.
PROMPT>as-create-launch-config MyLC --image-id ami-79fb6f49 --instance-type t1.micro --group webappsecuritygroup --key mykeypairAuto Scaling returns the following:
OK-Created launch config
Note
You can copy and paste the commands from the document into the command line window. To paste the contents in the command line window, use right-click. If you have trouble getting the commands to work, make sure the command was pasted correctly.
Use the Auto Scaling as-create-auto-scaling-group command. In this
example, we use two Availability Zones. This 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.
PROMPT>as-create-auto-scaling-group MyAutoScalingGroup --launch-configuration MyLC --availability-zones us-east-1b, us-east-1c --min-size 1 --max-size 1 --load-balancers MyLBAuto Scaling returns the following:
OK-Created AutoScalingGroup
Use the Auto Scaling as-put-scaling-policy command to create a policy to enlarge your fleet of instances.
PROMPT>as-put-scaling-policy MyScaleUpPolicy --auto-scaling-group MyAutoScalingGroup --adjustment=1 --type ChangeInCapacity --cooldown 300Auto Scaling returns output similar to the following example output:
POLICY-ARN arn:aws:autoscaling:us-east-1:012345678901:scalingPolicy:cbe7da4e-5d00-4882-900a-2f8113431e30:autoScalingGroupName/MyAutoScalingGroup:policyName/MyScaleUpPolicy
Note
To save time, we only created a scale-out policy. However, you typically would want to create a scale-in policy as well. Auto Scaling decreases the number of instances when your application doesn't need the resources, saving you money. To create a scale-in policy, change the policy name and change the adjustment from 1 to -1.
Verify that your Auto Scaling group exists by using the as-describe-auto-scaling-groups command.
PROMPT>as-describe-auto-scaling-groups MyAutoScalingGroup --headersAuto 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 1 1 1 INSTANCE INSTANCE-ID AVAILABILITY-ZONE STATE STATUS LAUNCH-CONFIG 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. 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.
Here's where you are at 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, we'll create an Amazon CloudWatch alarm so we can track the Auto Scaling policy you just created.