Terraform override文件未覆盖location字段的原因咨询
Terraform Override文件中Location未生效的原因
你的配置文件内容如下:
main.tf:
resource "azurerm_resource_group" "rg" { name = "bar" location = "westeurope" }
override.tf:
resource "azurerm_resource_group" "rg" { name = "foo" location = "northeurope" }
Terraform版本配置:
terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "3.51.0" } } }
问题原因
核心原因是Azure资源组的location属性是不可变更属性:Azure平台限制资源组创建后无法修改其部署区域,Terraform会严格遵循云服务商的资源规则。
如果之前你已经通过main.tf执行过terraform apply,那么Terraform的状态文件(terraform.tfstate)中已经记录了该资源组的location为westeurope。后续添加override.tf试图修改location时,Terraform检测到该属性不支持变更,会自动忽略override.tf中的location配置,因此terraform plan结果仍然显示原有的westeurope。
解决方法
如果确实需要将资源组部署到northeurope,可以选择以下两种方式:
- 销毁现有资源组后重建:执行
terraform destroy删除当前已部署的资源组,再执行terraform apply,此时会使用override.tf中的配置创建新的资源组。 - 修改资源标识:将
main.tf和override.tf中的资源名称改为新值(比如azurerm_resource_group "rg_ne"),Terraform会将其识别为全新资源,使用override.tf中的location参数进行部署。
内容的提问来源于stack exchange,提问作者Frédéric De Lène Mirouze
相关产品推荐
相关产品推荐

