Home  >  Blog  >   AWS  > 

How to Launch Amazon EC2 Instances Using Auto Scaling

Rating: 5
  
 
6852
  1. Share:
AWS Articles

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.

To gain in-depth knowledge and be on par with practical experience, then explore AWS SysOps Training Course.

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:

  • The instance is in the running state.
  • The AMI used to launch the instance must still exist.
  • The instance is not a member of another Auto Scaling group.
  • The instance is in the same Availability Zone as the Auto Scaling group.
  • If the Auto Scaling group is associated with a load balancer, the instance and the load balancer must both be in EC2-Classic or the same VPC.

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:

  • If identified instances have any tags, these are not copied to the attributes of the new group.
  • If the identified instances have load balancers, those are also not copied to the attributes of the new 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:

  • Create a launch configuration.
  • Create an Auto Scaling group.
  • Create a policy for your Auto Scaling group.

Checkout Amazon EC2 Interview Questions

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.

MindMajix YouTube Channel

Checkout AWS IoT Tutorial

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.

  • 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 you first launched your instance.
  • The 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.
  • The 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-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.

Amazon EC2 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 of 2.
  • The 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

Explore AWS Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Auto Scaling returns the following:

OK-Created AutoScalingGroup

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.

  • An 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.
  • The 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, 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

For an in-depth understanding and practical experience, Explore AWS Architect Training.

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.

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

If you interested to learn AWS and build a career in Cloud Computing?  Then check out our AWS Certification Training Course at your near Cities

AWS Training in Ahmedabad, AWS Training in Bangalore,  AWS Training in ChennaiAWS Training in Delhi,  AWS Training in Dallas, AWS Training in Hyderabad, AWS Training in London, AWS Training in Mumbai, AWS Training in NewYorkAWS Training in Pune

These courses are incorporated with Live instructor-led training, Industry Use cases, and hands-on live projects. This training program will make you an expert in AWS and help you to achieve your dream job.

Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
AWS TrainingAug 05 to Aug 20
AWS TrainingAug 08 to Aug 23
AWS TrainingAug 12 to Aug 27
AWS TrainingAug 15 to Aug 30
Last updated: 04 August 2023
About Author
Remy Sharp
Prasanthi

Prasanthi is an expert writer in MongoDB, and has written for various reputable online and print publications. At present, she is working for MindMajix, and writes content not only on MongoDB, but also on Sharepoint, Uipath, and AWS.

Recommended Courses

1 /15