英文:
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=
参数在 plan
或 apply
过程中返回 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.<declared module name>
. 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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论