简体   繁体   English

如何强制 terraform 重新创建资源?

[英]How do I force terraform to recreate a resource?

I have two resources:我有两个资源:

resource "aws_lightsail_instance" "myserver-sig" {
  name              = "myserver-Sig"
  availability_zone = "eu-west-2a"
  blueprint_id      = "ubuntu_20_04"
  bundle_id         = "nano_2_0"
  key_pair_name     = "LightsailDefaultKeyPair"
}

and

resource "aws_lightsail_instance_public_ports" "myserver-sig-public-ports" {
  instance_name = aws_lightsail_instance.myserver-sig.name
  port_info {
    protocol  = "tcp"
    from_port = 443
    to_port   = 443
  }
  port_info {
    protocol  = "tcp"
    from_port = 80
    to_port   = 80
  }
  depends_on = [
    aws_lightsail_instance.myserver-sig,
  ]
}

When I first run terraform apply both resources are created.当我第一次运行terraform apply时,这两个资源都被创建了。

If I want to replace the aws_lightsail_instance with a new version then the aws_lightsail_instance will redeploy, but the aws_lightsail_instance_public_ports will not because the ports haven't changed.如果我想用新版本替换aws_lightsail_instance ,则aws_lightsail_instance将重新部署,但aws_lightsail_instance_public_ports不会,因为端口没有更改。

However as part of the deploy of aws_lightsail_instance it changes the public ports to close 443 and open 22. This means that the end state of the redeploy of the aws_lightsail_instance is that port 443 is closed.但是,作为aws_lightsail_instance部署的一部分,它会将公共端口更改为关闭 443 并打开 22。这意味着重新部署 aws_lightsail_instance 的结尾aws_lightsail_instance是端口 443 已关闭。

If I run terraform apply again then it will correctly replace aws_lightsail_instance_public_ports opening port 443如果我再次运行terraform apply那么它将正确替换aws_lightsail_instance_public_ports打开端口 443

How do I force a recreation of the aws_lightsail_instance_public_ports resource so that I only have to run terraform apply once?如何强制重新创建aws_lightsail_instance_public_ports资源,以便我只需运行一次terraform apply

You can force the recreation (delete/create or -/+) by using the -replace=ADDRESS argument with terraform plan or terraform apply :您可以通过使用带有terraform planterraform apply计划的-replace=ADDRESS参数来强制重新创建(删除/创建或 -/+):

terraform apply -replace=aws_lightsail_instance_public_ports.myserver-sig-public-ports

This replaces the former workflow of terraform taint <resource_address> followed by a plan and apply .这取代了之前的terraform taint <resource_address>的工作流程,然后是plan and apply If you are using an older version of Terraform, then you would need to use taint instead:如果您使用的是旧版本的 Terraform,那么您需要改用taint

terraform taint aws_lightsail_instance_public_ports.myserver-sig-public-ports

You can use the lifecycle replace_triggered_by attribute to do this.您可以使用生命周期replace_triggered_by属性来执行此操作。 This was introduced in Terraform 1.2.0 (released May 2022).这是在 Terraform 1.2.0(2022 年 5 月发布)中引入的。

In your case to trigger the replace of aws_lightsail_instance_public_ports.myserver-sig-public-ports whenever aws_lightsail_instance.myserver-sig is replaced, add the following code to the aws_lightsail_instance_public_ports.myserver-sig-public-ports configuration:如果要在替换 aws_lightsail_instance.myserver-sig 时触发aws_lightsail_instance.myserver-sig aws_lightsail_instance_public_ports.myserver-sig-public-ports的替换,请将以下代码添加到aws_lightsail_instance_public_ports.myserver-sig-public-ports配置:

resource "aws_lightsail_instance_public_ports" "myserver-sig-public-ports" {
  # ...

  lifecycle {
    replace_triggered_by = [
      aws_lightsail_instance.myserver-sig.id
    ]
  }
}

Thus, whenever the Lightsail instance is replaced, the public ports will automatically be triggered to be replaced.因此,每当替换 Lightsail 实例时,将自动触发公共端口进行替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM