Changing user-data for launch configuration does not re-create the instance in the autoscaling group

305 views
Skip to first unread message

Igor Cicimov

unread,
Mar 15, 2017, 11:13:08 PM3/15/17
to Terraform
Hi all,

When I change the user-data of launch configuration resource and run the plan I get:

-/+ module.module1.aws_launch_configuration.host
    associate_public_ip_address: "true" => "true"
    ebs_block_device.#:          "0" => "<computed>"
    ebs_optimized:               "false" => "<computed>"
    enable_monitoring:           "true" => "true"
    [...]
    user_data:                   "75d8daae5d70b48778bb3fa14e47f829d234040a" => "aadb60447e52f52a1e0dfc132ea5a5a1c2a251de" (forces new resource)

so obviously new LC resource will get created. That's fine and I can see the LC in the AWS console changing the name suffix and user-data being updated. I can also see a change for the ASG i.e. new ASG gets created with reference to the new LC. However after applying the changes nothing happens to the currently running instance in the ASG, it keeps running instead being replaced with new one.

My resource definition is:

resource "aws_autoscaling_group" "host" {
    name                      = "${var.tag}-host-asg"
    availability_zones        = ["${var.zones}"]
    vpc_zone_identifier       = ["${var.subnet_ids}"]
    max_size                  = 1
    min_size                  = 1
    health_check_grace_period = 60
    default_cooldown          = 60
    health_check_type         = "EC2"
    desired_capacity          = 1
    force_delete              = true
    launch_configuration      = "${aws_launch_configuration.host.name}"
    lifecycle {
      create_before_destroy = true
    }
}

resource "aws_launch_configuration" "host" {
    name_prefix                 = "${var.tag}-host-lc-"
    image_id                    = "${var.image}"
    instance_type               = "${var.instance_type}"
    iam_instance_profile        = "${aws_iam_instance_profile.host.name}"
    key_name                    = "${var.key_name}"
    security_groups             = ["${aws_security_group.host.id}"]
    associate_public_ip_address = true
    user_data                   = "${data.template_file.host.rendered}"
    lifecycle {
      create_before_destroy = true
    }
}

Any idea why is this happening?

Thanks,
Igor

Derek Helmick

unread,
Mar 23, 2017, 2:26:12 PM3/23/17
to Terraform
Though you're switching out the Launch Config on the ASG, the ASG continues to have instances from the the prior config that it's happy with. It will only launch instances with the new config when dictated by changes in ASG's target-size or needing to replace old instances. 

This is fundamentally how AWS's ASGs work and allows you to choose what strategy you want in replacing instances -- be it all at once, in stages, or one at a time. 

Here's a interesting way to do blue-green deploys with ASGs; though it is from several versions back and there may be better methods for doing this: https://github.com/phinze/tf-bluegreen-asg-deployment/
Reply all
Reply to author
Forward
0 new messages