Terraform apply with ‘-replace’ parameter doesn’t force replace the object.

huangapple go评论48阅读模式
英文:

Terraform apply with '-replace' parameter doesn't force replace the object

问题

在我的 Terraform 定义中,我有已定义的 EC2 实例:

resource "aws_instance" "eventhub_instance" {
  ami                         = var.eh_instance_ami
  instance_type               = var.eh_instance_type
  count                       = 2
......

这些实例已经在运行。现在,我想运行 terraform apply 并强制重新创建这些实例。我理解我可以使用 -replace=ADDRESS 来强制执行这个操作,所以我尝试了以下命令:

terraform apply -auto-approve -var-file=vars/dev.terraform.tfvars -replace=aws_instance.eventhub_instance[0] -replace=aws_instance.eventhub_instance[1]

Terraform 运行并以以下输出完成:

No changes. Your infrastructure matches the configuration.

为什么它不替换/重新创建 EC2 实例?

英文:

In my terraform definitions, I have EC2 defined:

resource "aws_instance" "eventhub_instance" {
  ami                         = var.eh_instance_ami
  instance_type               = var.eh_instance_type
  count                       = 2
......

Those instances are up and running. Now, I want to run terraform apply and force recreation of those instances. My understanding that I can use -replace=ADDRESS to force that, so I tried:

terraform apply -auto-approve -var-file=vars/dev.terraform.tfvars -replace=aws_instance.eventhub_instance[0] -replace=aws_instance.eventhub_instance[1]

Terraform runs and finishes with

No changes. Your infrastructure matches the configuration.

Why doesn't it replace/recreate the EC2 instances?

答案1

得分: 1

-replace= 参数在 planapply 过程中返回 No changes 时,可能是引用资源的命名空间不正确。您可以使用已弃用的 terraform taint 命令来验证这一点。

模块内资源的命名约定需要使用命名空间前缀 module.<声明的模块名称>。在您的情况中,声明的模块名称为 all,那么应该是:

terraform apply -auto-approve -var-file=vars/dev.terraform.tfvars -replace=module.all.aws_instance.eventhub_instance[0] -replace=module.all.aws_instance.eventhub_instance[1]
英文:

When a -replace= argument returns No changes during a plan or apply, then it is possible the namespace of the referenced resource is incorrect. You can doublecheck this with the deprecated terraform taint command.

The nomenclature of a resource within a module requires the namespace prefix module.&lt;declared module name&gt;. In your situation where the declared module name is all, that would then be:

terraform apply -auto-approve -var-file=vars/dev.terraform.tfvars -replace=module.all.aws_instance.eventhub_instance[0] -replace=module.all.aws_instance.eventhub_instance[1]

huangapple
  • 本文由 发表于 2023年4月7日 00:00:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定