Auto Scaling is a service that allows us to scale our Amazon EC2 capacity automatically by scaling out and scaling in according to criteria that we define.With Auto Scaling,we can ensure that the number of running Amazon EC2 instances increases during demand spikes or peak demand periods to maintain application performance and decreases automatically during demand lulls or troughs to minimize costs
A launch configuration is the template that Auto Scaling uses to create new instances,and it is composed of the configuration name,Amazon Machine Image(AMI),Amazon EC2 instance type, security group, and instance key pair. Each Auto Scaling group can have only one launch configuration at a time.
Auto Scaling Group
An Auto Scaling group is a collection of Amazon EC2 instances managed by the Auto Scaling service. Each Auto Scaling group contains configuration options that control when Auto Scaling should launch new instances and terminate existing instances.An Auto Scaling group must contain a name and a minimum and maximum number of instances that can be in the group.You can optionally specify desired capacity,which is the number of instances that the group must have at all times.If you don’t specify a desired capacity, the default desired capacity is the minimum number of instances that you specify.
With your Launch Configuration created,the final step left is to create one or more scaling plans. Scaling Plans describe how the Auto Scaling Group should actually scale.
Manual scaling: Here is specify a new desired number of instances value or change the minimum or maximum number of instances in an Auto Scaling Group and the rest is taken care of by the Auto Scaling service itself.
Scheduled scaling:We can scale resources based on a particular time and date.
Dynamic scaling: Dynamic scaling,or scaling on demand is used when the predictability of your application’s performance is unknown.
Auto scaling group creation involves with two steps.First one is Creating a Launch Configuration and second is Creating Auto Scaling group.
Creating the Launch Configuration steps
- Go to EC2 Management Dashboard option,select the Auto Scaling Groups option from the navigation pane.This will bring up the Auto Scaling Groups dashboard.Next,select the Create Auto Scaling group option to bring up the Auto Scaling set up wizard.
2. If you have any custom AMIs you can select here.
3.Choose Instance configuration,Storage options,security groups,tags and keypairs and select Create Launch Configuration to complete the process
Step 2: Creating the Auto Scaling Group
An Auto Scaling Group is nothing more than a logical grouping of instances that share some common scaling characteristics between them.Each group has its own set of criteria specified which includes the minimum and maximum number of instances that the group should have along with the desired number of instances which the group must have at all times.
4. When we completes with creating launch configuration,it will take us to Step 2,Here we have to give a name for the Group,We can select the Group size and VPC.
Each instance in this Auto Scaling Group will be provided with a public IP address.
5. We can expand Advanced details option to configure.
Load Balancing:These are optional settings that you can configure to work with your Auto Scaling Group.Since we have already created and configured our ELB,we will be using that it self to balance out incoming traffic for our instances.Select the Receive traffic from Elastic Load Balancer option.
Health Check Type:You can use either your EC2 instances or even your ELB as a health check mechanism to make sure that your instances are in a healthy state and performing optimally.By default, Auto Scaling will check your EC2 instances periodically for their health status. If an un healthy instance is found,Auto Scaling will immediately replace that with a healthy one.
Health Check Grace Period:Enter the health check’s grace period in seconds.By default,this value is set to 300 seconds.
6. Step 2 of ASG creation is Configure scaling policies:This is the important part of creating any Auto Scaling Group is defining its scaling policies
7. Selecting the scaling policies option.
Name: Provide a suitable name for your scale-out policy.
Execute policy when:Here we have to select a pre-configured alarm using which the policy will get triggered.Since this is our first time configuring,select the Add new alarm option.This will popup the Create Alarm dialog,
Creating the alarm is a very simple process;for example,we want our Auto Scaling Group to be monitored based on the CPU Utilization metric for an interval of 5 minutes.If the average CPU Utilization is greater than or equal to 90 percent for atleast one consecutive period,then send a notification mail to the specified SNS Topic.click on Create Alarm.
Take the action:Now we can define the policy what action it has to take if the particular threshold is breached.Select Add from the drop down list and provide a suitable number of instances that you wish to add when a certain condition matches.
Instances need:The final field is the Cool down period.By default,this value is set to 300 seconds and can be changed as per your requirements.A Cool down period is like a grace period that we assign to the Auto Scaling Group to ensure that we don’t launch or terminate any more resources before the effects of previous scaling activities are completed.
8.By the same way we can configure policies for Decrease Group Size also
9. Select the Next:Configure Notifications to proceed with the next steps
10.You can select Add Notification button and select an existing SNS topic or create a new
11. Select the review option and Click on Create Auto Scaling option to finish the process.
Default Termination Policy for Auto Scaling Group:
If there are instances in multiple Availability Zones,select the Availability Zone with the most instances and atleast one instance that is not protected from scale in.If there is more than one Availability Zone with this number of instances,select the Availability Zone with the instances that use the oldest launch configuration.
Determine which unprotected instances in the selected Availability Zone use the oldest launch configuration.If there is one such instance,terminate it.
If there are multiple instances that use the oldest launch configuration,determine which unprotected instances are closest to the next billing hour.If there is one such instance, terminate it.
If there is more than one unprotected instance closest to the next billing hour,select one of these instances at random.
Here is a diagram that shows how the default termination policy works for ASG.
When you launch an instance in Amazon EC2,you have the option of passing user data to the instance that can be used to perform common automated configuration tasks and even run scripts after the instance starts.
You can pass two types of user data to Amazon EC2: shell scripts and cloud-init directives.You can also pass this data into the launch wizard as plain text,as a file(this is useful for launching instances using the command line tools),or as base 64-encoded text.
Here is a simple User Data script to use with Linux EC2 instances to make as a simple web server with a simple index.html page.
#!/bin/bash yum update -y
yum install httpd -y
echo”Hi This is a Bootstrap script generated webpage”>/var/www/html/index.html service httpd start
chk config httpd on
“yum update”for updating the Operating system with latest security patches.
“Yum install httpd”for installing Apache to make this instance as a web server
By Using echo command generating a string and copying the generated string to a filenamed “index.html”and saving the file under“/var/www/html”directory.
“Service httpd start” to start the apache service
“Chkconfig httpd on” starting and turning the service on / startup service
- While launching instance I’ve entered the bootstrap scripting
2. Then launching the instance and entering the public IP in the web browser without connecting to my instance.(Make sure port 80 open in the Security groups)
3. We got the output without log into the instance.
For EC2 Config or EC2 Launch to execute user data scripts,you must enclose the lines of the specified script with in one of the following special tags:
Example: <script>dir> c:\test.log</script>
- Here we have run very simple script get directory information to a log file.New doc is created with all the information of the given directory.
More Information :https://www.fgrade.com/amazon-web-services/