【问题标题】:Terraform (provider AWS) - Auto Scaling group doesn't take effect on a launch template changeTerraform(提供者 AWS)- Auto Scaling 组不会在启动模板更改时生效
【发布时间】:2018-12-17 10:56:57
【问题描述】:

在使用启动模板时无法使启动模板与 ASG 一起使用,它可以通过一个小技巧与启动配置一起使用,即通过在 ASG 资源中插入启动配置名称,但它不适用于启动模板。
ASG 使用最新版本来启动新实例,但不会对预运行实例进行任何更改,尽管启动模板发生了变化。

我知道这是意料之中的,但我们是否有任何解决方法可以使启动模板与 ASG 一起使用,或者我们需要坚持启动配置本身?

TF代码sn-p -

resource "aws_launch_template" "lc_ec2" {
  image_id = "${var.ami_id}"
  instance_type = "${var.app_instance_type}"
  key_name = "${var.orgname}_${var.environ}_kp"
  vpc_security_group_ids = ["${aws_security_group.sg_ec2.id}"]
  user_data = "${base64encode(var.userdata)}"
  block_device_mappings {
    device_name = "/dev/xvdv"
    ebs {
      volume_size = 15
    }
  }
  iam_instance_profile {
    name = "${var.orgname}_${var.environ}_profile"
  }
  lifecycle {
    create_before_destroy = true
  }

  tag_specifications {
    resource_type = "instance"
    tags = "${merge(map("Name", format("%s-%s-lc-ec2", var.orgname, var.environ)), var.tags)}"
    } 
  tag_specifications {
    resource_type = "volume"
   tags = "${merge(map("Name", format("%s-%s-lc-ec2-volume", var.orgname, var.environ)), var.tags)}"
    }
  tags = "${merge(map("Name", format("%s-%s-lc-ec2", var.orgname, var.environ)), var.tags)}"
}

resource "aws_autoscaling_group" "asg_ec2" {
    name = "${var.orgname}-${var.environ}-asg-ec2-${aws_launch_template.lc_ec2.name}"

    vpc_zone_identifier = ["${data.aws_subnet.private.*.id}"]
    min_size  = 1
    desired_capacity  = 1
    max_size  = 1
    target_group_arns = ["${aws_lb_target_group.alb_tg.arn}"]
    default_cooldown= 100
    health_check_grace_period = 100
    termination_policies = ["ClosestToNextInstanceHour", "NewestInstance"]
    health_check_type="ELB"
    launch_template = {
      id = "${aws_launch_template.lc_ec2.id}"
      version = "$$Latest"
   }  
      lifecycle {
    create_before_destroy = true
  }

  tags = [
    {
      key                 = "Name"
      value               = "${var.orgname}"
      propagate_at_launch = true
    },
    {
      key                 = "Environ"
      value               = "${var.environ}"
      propagate_at_launch = true
    }
  ]
}

【问题讨论】:

  • 它不会改变asg中正在运行的实例,您需要将它们一个一个取下来。新实例启动后,它们将获得最新设置。当您在 prod 环境中工作时,它会很有用。
  • @BMW 使用 create before destroy 对 ASG 进行蓝绿色替换是使用启动配置时非常常见的模式,但不幸的是它不能直接与启动模板一起使用,因为它们不是不可变的因此更改启动模板不会强制创建新资源。您是否有不想在此处使用启动配置的原因?
  • 我还没有找到原因,但我开始并发现启动模板是新的,因此尝试使用 terraform 与 A​​SG 集成,但没有成功。它和 LC 不一样,我喜欢模板中的版本功能。

标签: terraform terraform-provider-aws


【解决方案1】:

有一个技巧可以实现这一点。

AWS CloudFormation 支持自动缩放组的滚动更新。 由于 Terraform 支持 cloudformation 堆栈资源,因此您可以将 ASG 定义为具有更新策略的 cloudformation 堆栈。但是,CloudFormation 不支持启动模板版本的 $$Latest 标签,因此您必须参数化版本并从 terraform 配置文件中创建的启动模板资源的 latest_version 属性中获取输入值。

【讨论】:

    猜你喜欢
    • 2017-04-20
    • 2013-10-13
    • 2018-03-25
    • 1970-01-01
    • 2021-07-16
    • 2020-10-05
    • 2017-07-24
    • 2021-07-16
    • 1970-01-01
    相关资源
    最近更新 更多