> For the complete documentation index, see [llms.txt](https://ysfang82.gitbook.io/development-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ysfang82.gitbook.io/development-notes/aws-notes/service/compute-and-load-balancing/auto-scaling-group.md).

# Auto Scaling Group

**Scaling Policies**

* Simple / step scaling: increase or decrease based on CloudWatch alarms.
* Target tracking: select a metric and a target value, ASG will smartly adjust it.
  * keep average CPU at 40%
  * keep request count per target at 1000
* To scale based on RAM, must use a custom CloudWatch metric.

**Feature**

* [Spot Fleet (mix on Spot and On-demand instances) supports 3 types of ASG](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-fleet-automatic-scaling.html)
* **Launch configuration once created cannot be changed**
* To upgrade an AMI, must re-associate the launch configuration / template.
  * must terminate instances manually.
  * CloudFormation can help with the step.
* Enable CloudWatch detailed monitoring which is disabled in creation
  * ​Create a new Launch Config with detail monitoring enabled
  * Associate it to the Auto Scaling Group
* Scheduled scaling actions:
  * Modify the ASG settings (min / max / desired) at pre-defined time
  * Helpful when patterns are known in advance
* Lifecycle Hooks:
  * Perform actions before an instance is in service, or before it is terminated.
  * Ex. cleanup, log extraction, special health checks.
* AZ Rebalance
  * Be mindful of unfinished tasks

**Scaling Processes**

* Launch: Add a new EC2 to the group, increasing the capacity.
* Terminate: Removes an EC2 instance from the group, decreasing its capacity.
* HealthCheck: Checks the health of the instances.
* ReplaceUnhealthty: Terminate unhealthy instances and re-create them.
* AZRebalance: Balance the number of EC2 instances across AZs.
* AlarmNotification: Accepts notification from CloudWatch.
* ScheduledActions: Performs scheduled actions that you create.
* AddToLoadBalancer: Adds instances to the load balancer or target group.

**Health Checks**

* Types:
  * EC2 status checks (default)
    * The health of EC2 instance doesn't equal to the health of ELB. Change health check targets to terminate instances correctly.
  * ELB health checks (HTTP)
* ASG will launch a new instance after terminating an unhealthy one.
  * With Grace Period: 300s to wait for the health of new instance
  * With Cooldown Period: 300s to wait for another scaling / termination

**Comparison of Auto Scaling update stratigies**

* Use the same ASG with new EC2 Launch Template
  * No chance to test the service in new version.
  * requests are handled by different version of service.
* Create another ASG, EC2 Launch Template but use the same ALB.
  * Can send small traffic from ALB to new Scaling Group to verify the service in new version.
* Create another ALB, ASG, EC2 Launch Template, and use Route53 to change the routing.
  * Relying customers on good behavior about DNS queries and respecting TTL.
  * Can test service in new version with a different ALB before update the Route53.
