Implementing Cloud Design Patterns for AWS(Second Edition)
上QQ阅读APP看书,第一时间看更新

Auto scaling

If you've applied the patterns and followed the practices enumerated here, you are on the road to success. Unfortunately, success often comes more swiftly than you expect. A mention on Hacker News, Reddit, or Twitter can send waves of traffic your way. Let's take a look at how Amazon's auto scaling feature can help us to meet the demand in a cost effective way. First, we will define a launch configuration for our product—this should go into the instanceAZ1c.tf file:

resource "aws_launch_configuration" "asg_conf" {
name = "book-asg-config"
image_id = "ami-001e1c1159ccfe992"
instance_type = "t2.micro"
}

Then, add an autoscaling group to the same file, which encompasses all AZs, so that the workload gets distributed evenly:

resource "aws_autoscaling_group" "book_group" {
availability_zones = ["us-east-1a","us-east-1b","us-east-1c"]
name = "book-group-asg"
max_size = 5
min_size = 2
health_check_grace_period = 300
health_check_type = "ELB"
force_delete = true
launch_configuration = "${aws_launch_configuration.asg_conf.name}"
}

And add a policy that defines the configuration for capacity, causes, adjustment, and cool down, which in this case puts the policy to sleep for an arbitrary five minutes:

resource "aws_autoscaling_policy" "bat" {
name = "book-policy-ASG"
scaling_adjustment = 4
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = "${aws_autoscaling_group.book_group.name}"
}