Auto scaling groups along with Launch Configurations enable you to create a logical group of EC2 instances that are always available.
ASGs optionally work with ELBs but ELB is not needed for ASG to work.
What is Launch Configuration?
Used by auto scaling group to launch EC2 instances without user intervention.
Is a recipe to create an EC2 instance. Steps involved are exactly same as creating an EC2 except at the end you will create the recipe as opposed to launching the actual instance.
Create Launch Configuration
Choose AMI
Choose Instance Type
Name the launch config
Choose spot instances or not
Choose if you need detailed monitoring on cloud watch
Choose IAM roles if any
Input the User data (bootstrapping code) if any
Any ip address related parameters
Add storage details
Security Group details (default or your own)
Input your key pair name
Click save the launch configuration
Create Auto Scaling Group. (This will launch the EC2 instances)
Name of the auto scaling group
Choose VPC
Choose Group size (starting number of instances)
Choose subnets (one or more. Its a multi select. Better to choose two or more AZs). The instances from above step will be distributed evenly among the chosen AZs
Choose Launch Configuration
Choose optional Load Balancer
Choose which health check to use (ELB or EC2)
Input health check grace period (300 sec default). This is the amount of time the ASG waits before it checks the health of a newly started instance. If you have major bootstrapping script, increase this parameter.
Scaling Policies
Scaling policies enable you to either keep the group size constant at th initial size or
Scale between min and max instances (min and max are not equal to initial size)
Increase Group Size Rule
Execute policy when (Points to an Alarm. Alarm has following)
Name of the alarm
Whenever: Average/Min/Max of CPU/RAM >/</>=/<= XXXX percent.
At least: N number of consecutive periods of T sec/min
Optional SNS notification
Take the action:
Add/Substract
N instances
Warm up time (300 sec)
Similarly Create Decrease Group Size Rule
Provide optional email to be sent when launching/termination/fail to launch/fail to terminate instances
Finally click to “Create ASG”. This will immediately launch the EC2 instances as per the rules of the ASG
Remember: Security Group is input for the launch configuration whereas VPC/subnets/AZs are parameters of the auto scaling group.
The default termination policy is designed to help ensure that your network architecture spans Availability Zones evenly.
Auto Scaling determines whether there are instances in multiple Availability Zones. If so, it selects the Availability Zone with the most instances and at least one instance that is not protected from scale in.
If there is more than one Availability Zone with this number of instances, Auto Scaling selects the Availability Zone with the instances that use the oldest launch configuration.
If you suspend AddToLoadBalancer, Auto Scaling launches the instances but does not add them to the load balancer or target group. If you resume the AddToLoadBalancer process, Auto Scaling resumes adding instances to the load balancer or target group when they are launched. However, Auto Scaling does not add the instances that were launched while this process was suspended. You must register those instances manually.
Auto Scaling periodically performs health checks on the instances in your Auto Scaling group and identifies any instances that are unhealthy. You can configure Auto Scaling to determine the health status of an instance using Amazon EC2 status checks, Elastic Load Balancing health checks, or custom health checks
The thumb rule is, if any instance reported unhealthy either by Auto scaling health check or any ELB instance check (In case of multiple ELB), Auto scaling will consider worst case scenario and always mark instance unhealthy and remove it.