Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rolling update of ASG using launch template

When I update the AMI associated with a aws_launch_template, Terraform creates a new version of the launch template as expected and also updates the aws_autoscaling_group to point to the new version of the launch template.

However, no "rolling update" is performed to switch out the existing instances with new instances based on the new AMI, I have to manually terminate the existing instances and then the ASG brings up new instances using the new AMI.

What changes do I have to make to my config to get Terraform to perform a rolling update?

Existing code is as follows:

resource "aws_launch_template" "this" {

  name_prefix = "my-launch-template-"
  image_id = var.ami_id
  instance_type = "t3.small"
  key_name = "testing"

  vpc_security_group_ids = [ aws_security_group.this.id ]

  lifecycle {
    create_before_destroy = true
  }
}


resource "aws_autoscaling_group" "this" {

  name_prefix = "my-asg-"
  vpc_zone_identifier = var.subnet_ids
  target_group_arns = var.target_group_arns

  health_check_type = "ELB"
  health_check_grace_period = 300
  default_cooldown = 10

  min_size = 4
  max_size = 4
  desired_capacity = 4

  launch_template {
    id = aws_launch_template.this.id
    version = aws_launch_template.this.latest_version
  }

  lifecycle {
    create_before_destroy = true
  }
}
like image 796
smilin_stan Avatar asked Feb 25 '20 09:02

smilin_stan


People also ask

How do I update my ASG launch template?

On the Details tab, choose Launch configuration, Edit. For Launch configuration, select the new launch configuration. When you have finished, choose Update.

Can we update launch template?

once you updated your launch template, fo to your autoscaling group edit it and under launch template choose the update version of template. in case of launch configuration if you want to update application you need to recreate launch configuration every single time.

How can I make sure my Auto Scaling group is updated the right way?

You can use the AutoScalingRollingUpdate policy to control how AWS CloudFormation handles rolling updates for an Auto Scaling group. This common approach keeps the same Auto Scaling group, and then replaces the old instances based on the parameters that you set.

How can you ensure that your Auto Scaling group uses the new AMI to launch new instances?

After automation completes, in the Amazon EC2 console, choose Auto Scaling, and then choose Launch Templates. Verify that you see the new launch template, and that it uses the new AMI. Choose Auto Scaling, and then choose Auto Scaling Groups. Verify that the Auto Scaling group uses the new launch template.


2 Answers

I recently worked on that exact same scenario.

We used the random_pet resource to generate a human readable random name that links with the AMI changes.

resource "random_pet" "ami_random_name" {
  keepers = {
    # Generate a new pet name every time we change the AMI
    ami_id = var.ami_id
  }
}

You can then use that random_pet name id on a variable that would force the recreation of your autoscaling group.

For example with name_prefix:

resource "aws_autoscaling_group" "this" {

  name_prefix = "my-asg-${random_pet.ami_random_name.id}"
  vpc_zone_identifier = var.subnet_ids
  target_group_arns = var.target_group_arns

  health_check_type = "ELB"
  health_check_grace_period = 300
  default_cooldown = 10

  min_size = 4
  max_size = 4
  desired_capacity = 4

  launch_template {
    id = aws_launch_template.this.id
    version = aws_launch_template.this.latest_version
  }

  lifecycle {
    create_before_destroy = true
  }
}
like image 161
jphuynh Avatar answered Oct 05 '22 22:10

jphuynh


ASG instance refresh is also an option that replaces all old instances with newer instances as per the newest version in the launch template ( make sure to set LaunchTemplateVersion = $Latest in ASG settings). other benefits are:

  1. Set a time to warm up instances before receiving traffic ( if you have time taking bootstrap installations)
  2. You can specify percentage of how many instances to replace within ASG parallelly to speed things up.

Below is terraform code block. More about the feature here

 instance_refresh {
    strategy = "Rolling"
    preferences {
      min_healthy_percentage = 50
    }
    triggers = ["tag"]
  }
like image 30
Surya Prakash Patel Avatar answered Oct 05 '22 20:10

Surya Prakash Patel