如何使用 terraform 重新创建自动缩放组的 EC2 实例? [英] How to recreate EC2 instances of an autoscaling group with terraform?

查看:33
本文介绍了如何使用 terraform 重新创建自动缩放组的 EC2 实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景: 我正在运行 AWS 自动缩放组 (ASG),并且在 terraform 应用期间更改了关联的启动配置.ASG 不受影响.

Scenario: I am running an AWS autoscaling group (ASG), and I have changed the associated launch configuration during terraform apply. The ASG stays unaffected.

我现在如何根据更改/新的启动配置重新创建该 ASG 中的实例(即,逐个替换它们以进行滚动替换)?

How do I recreate now the instances in that ASG (i.e., replace them one-by-one to do a rolling replace), which then is based on the changed/new launch configuration?

我尝试过的: 使用 terraform taint 可以在下一次申请期间标记要销毁和重新创建的资源.但是,我不想污染自动缩放组(这是一种资源,在这种情况下不是单个实例),而是其中的单个实例.有没有办法污染单个实例或者我的想法是错误的?

What I've tried: With terraform taint one can mark resources to be destroyed and recreated during the next apply. However, I don't want to taint the autoscaling group (which is a resource, and single instances are not in this case), but single instances in it. Is there a way to taint single instances or am I thinking in the wrong direction?

推荐答案

这里正常的做法是使用Terraform 的生命周期管理 强制它在销毁旧资源之前创建新资源.

The normal thing to do here is to use Terraform's lifecycle management to force it to create new resources before destroying the old ones.

在这种情况下,您可以像这样设置启动配置和自动缩放组:

In this case you might set your launch configuration and autoscaling group up something like this:

resource "aws_launch_configuration" "as_conf" {
    name_prefix = "terraform-lc-example-"
    image_id = "${var.ami_id}"
    instance_type = "t1.micro"

    lifecycle {
      create_before_destroy = true
    }
}

resource "aws_autoscaling_group" "bar" {
    name = "terraform-asg-example-${aws_launch_configuration.as_conf.name}"
    launch_configuration = "${aws_launch_configuration.as_conf.name}"

    lifecycle {
      create_before_destroy = true
    }
}

然后,如果您更改 ami_id 变量以使用另一个 AMI,Terraform 将意识到它必须更改启动配置,因此在销毁旧配置之前创建一个新配置.然后将新 LC 生成的新名称插入到 ASG 名称中,从而强制重建新的 ASG.

Then if you change the ami_id variable to use another AMI Terraform will realise it has to change the launch configuration and so create a new one before destroying the old one. The new name generated by the new LC is then interpolated in the ASG name forcing a new ASG to be rebuilt.

当您使用 create_before_destroy 时,Terraform 将创建新的 LC 和 ASG,并等待新 ASG 达到所需容量(可以配置运行状况检查),然后再销毁旧 ASG,然后旧的 LC.

As you are using create_before_destroy Terraform will create the new LC and ASG and wait for the new ASG to reach the desired capacity (which can be configured with health checks) before destroying the old ASG and then the old LC.

这将一次性翻转 ASG 中的所有实例.因此,如果您在 ASG 中的最小容量为 2,那么这将再创建 2 个实例,并且一旦这两个实例都通过了健康检查,那么 2 个较旧的实例将被销毁.如果您将 ELB 与 ASG 一起使用,那么它会将 2 个新实例加入 ELB,因此,在销毁旧的 2 个之前,您将暂时让所有 4 个实例处于服务状态.

This will flip all the instances in the ASG at once. So if you had a minimum capacity of 2 in the ASG then this will create 2 more instances and as soon as both of those pass health checks then the 2 older instances will be destroyed. In the event you are using an ELB with the ASG then it will join the 2 new instances to the ELB so, temporarily, you will have all 4 instances in service before then destroying the older 2.

这篇关于如何使用 terraform 重新创建自动缩放组的 EC2 实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆