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

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

问题描述

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

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

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

此处的常规操作是使用 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.

在使用create_before_destroy时,Terraform将创建新的LC和ASG,并等待新的ASG达到所需容量(可以通过运行状况检查对其进行配置),然后再销毁旧的ASG,然后再销毁旧的LC./p>

这将立即翻转ASG中的所有实例.因此,如果您在ASG中的最小容量为2,则这将再创建2个实例,并且一旦这两个实例均通过运行状况检查,则这2个旧实例将被销毁.如果您将ELB与ASG一起使用,则它将把2个新实例加入到ELB中,因此暂时将所有4个实例都投入使用,然后销毁较旧的2个实例.

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

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?

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?

解决方案

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
    }
}

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.

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.

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天全站免登陆