Auto-scaling groups are not updated when launch configuration changes

I have an AWS auto-scaling group, startup configuration, and auto-scaling group policy defined in Terraform as follows:

resource "aws_autoscaling_group" "default" { name = "..." health_check_type = "EC2" vpc_zone_identifier = ["${...}"] min_size = "${var.asg_capacity}" max_size = "${var.asg_capacity * 2}" desired_capacity = "${var.asg_capacity}" launch_configuration = "${aws_launch_configuration.default.id}" termination_policies = ["OldestInstance"] } resource "aws_autoscaling_policy" "default" { name = "..." autoscaling_group_name = "${aws_autoscaling_group.default.name}" scaling_adjustment = "${var.asg_capacity}" adjustment_type = "ChangeInCapacity" cooldown = 300 } resource "aws_launch_configuration" "default" { name_prefix = "..._" image_id = "${var.coreos_ami_id}" instance_type = "${var.ec2_instance_type}" iam_instance_profile = "${aws_iam_instance_profile.default.arn}" key_name = "..." security_groups = ["${aws_security_group.default.id}"] user_data = "${data.template_file.cloud_init.rendered}" lifecycle { create_before_destroy = true } } 

When I change my user data, a new launch configuration is created and then joined to the autoscale group. I would suggest that this would cause the var.asg_capacity group to grow by var.asg_capacity instances, wait 300 seconds, and then demolish the old values ​​by OldestInstance .

When I did similar things in CloudFormation, I used the following configuration options :

 ASG: Type: AWS::AutoScaling::AutoScalingGroup UpdatePolicy: AutoScaleRollingUpdate: # during a scale, 6 instances in service MaxBatchSize: 3 MinInstancesInService: 3 PauseTime: PT5M Properties: ... 

Is there an analogue for this in Terraform? I would really like my autoscale groups to change when the launch configuration changes.

+5
source share
1 answer

I would suggest that this would cause the autoscale group to expand with var.asg_capacity instances, wait 300 seconds, and then tear down the old files by OldestInstance.

This assumption, unfortunately, is not true. When you change the launch configuration, there is only one new launch configuration in your AWS account and is associated with an automatic scaling group (ASG). This means that all future instances of this ASG will be launched with a new launch configuration. However, simply changing the launch configuration does not start the launch of any instances, so you will not see the changes.

To get new instances to start, you need to do a few things:

  • Configuring the name ASG parameter directly depends on the launch configuration name. Thus, every time you change the launch configuration (what will happen when updating the AMI or user data) Terraform will try to replace the ASG.
  • Set the create_before_destroy parameter for ASG to true , so every time Terraform tries to replace it, it will create a replacement before destroying the original.
  • Set the min_elb_capacity parameter for the ASG for the min_size cluster so Terraform min_size at least many servers from the new ASG to register with the ELB before it starts destroying the original ASG.

Here's a rough idea of ​​what the Terraform code looks like:

 resource "aws_launch_configuration" "example" { image_id = "${var.ami}" instance_type = "${var.instance_type}" user_data = "${data.template_file.user_data.rendered}" lifecycle { create_before_destroy = true } } resource "aws_autoscaling_group" "example" { name = "${var.cluster_name}-${aws_launch_configuration.example.name}" launch_configuration = "${aws_launch_configuration.example.id}" availability_zones = ["${data.aws_availability_zones.all.names}"] min_size = "${var.min_size}" max_size = "${var.max_size}" min_elb_capacity = "${var.min_size}" lifecycle { create_before_destroy = true } } 

For a full-featured example, check out the zero-downtime deployment code from Terraform: Up and Running .

+11
source

Source: https://habr.com/ru/post/1260839/


All Articles