Launching Amazon EC2 Instances Using Auto Scaling
Auto Scaling provides you with an option to enable Auto Scaling for one or more EC2 instances by attaching them to your existing Auto Scaling group. After the instances are attached, they become a part of the Auto Scaling group.
If you are aiming to use Auto Scaling, then certain important points must be acknowledged. This particular section helps you to gain the basic information regarding it.
Auto Scaling helps you maintain application availability. 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.
Auto Scaling provides you with an option to create Auto Scaling group by specifying an EC2 instance as well as attributes such as minimum, maximum or any chosen number, all having similar characteristics. The Auto Scaling group is the core value of the Auto Scaling service. You can associate Auto Scaling group with a load balancer. You can regulate these groups by assigning metadata to each group in the form of tags.
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 instance 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.
The instance that you want to attach must meet the following criteria:
When you attach instances, Auto Scaling increases the desired capacity of the group by the number of instances being attached. If the number of instances being attached plus the desired capacity exceeds the maximum size of the group, the request fails.
There are certain limitations while creating Auto Scaling group:
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:
For the purposes of this tutorial, we’ll set up an application running on Amazon EC2 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 must 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 an 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 Network Out 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 the load decreases.
If you haven’t already installed the Auto Scaling command line tools, you need to do that immediately. For information go to, ‘using the command line tools‘ in the Auto Scaling Developer Guide. You will use the command line tools to set up Auto Scaling.
Auto Scaling gives you an advantage to enable it for one or more instances. When you attach an instance, it increases the quantity of the group by the number of instances that you have added. You can control the added instances (scaling out) and removed instances (scaling in) manually from the network. There are certain instances like Spot instances which are economical compared to On-demand instances.
To set up an auto-scaled, load-balanced application running on Amazon EC2
You can associate your Auto Scaling group with a load balancer. The load balancer automatically distributes incoming traffic across the instances in the group.
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.
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-95ce1afc –instance-type t1. micro –group webappsecuritygroup –key mykeypair –monitoring-disabled
Auto Scaling returns output similar to the following example:
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 in 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.
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
Auto Scaling returns the following:
4. To create a policy to enlarge your fleet of instances, use the Auto Scaling as-put-scaling-policy command. This policy applies to your Auto Scaling group which was created in the previous step. Use the following parameters when defining your Auto Scaling policy.
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, thus saving you the money. To create a policy for terminating an instance, then start from the policy you have 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 C–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-1c In Service Healthy MyLC
INSTANCE i-xxxxxxxx us-east-1b In Service 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.
When You’re At
Here’s where you are while 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 tutorial, you’ll create an Amazon CloudWatch alarm to track the Auto Scaling policy you have just created.
Enroll for Instructor Led Live AMAZON WEB SERVICES TRAINING
Get Updates on Tech posts, Interview & Certification questions and training schedules