如何避免使用 terraform 重新创建 ec2 实例?

时间:2021-04-18 03:04:47

标签: amazon-web-services amazon-ec2 terraform

在为 IaC 做版本升级时,为了这个 terraform 任务

module "ec2_instance" {
  source  = "terraform-aws-modules/ec2-instance/aws"
  version = "2.17.0"

  name          = local.name
  ami           = data.ami.id
  instance_type = local.type

  lifecycle {
    ignore_changes = ["ami"]
  }
}

当运行 tflint 时,得到

The block type name "lifecycle" is reserved for use by Terraform in a future version.

这是因为 lifecycle 不能在模块中使用。但是如果想避免更改创建的 ec2 实例销毁并再次重新创建。如何忽略它的 ami 状态监控以通过它?

运行后terraform plan

+ create
~ update in-place
-/+ destroy and then create replacement
+/- create replacement and then destroy

# module.ec2_instance.aws_instance.this[0] must be replaced
-/+ resource "aws_instance" "this" {
      ~ ami = "ami-xxxxxxxxx" -> "ami-yyyyyyyyy" # forces replacement
      ...

Plan: 1 to add, 1 to change, 1 to destroy.

如果将 ec_instance 模块更改为 aws_instace,可能会导致同样的问题。将重新创建实例。

0 个答案:

没有答案