需要后端初始化,请运行"terraform init"



我的后端有以下Terraform配置,它工作了一段时间,

terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
}
}
backend "azurerm" {
resource_group_name  = "my-rg"
storage_account_name = "my-sa"
####!!!!! BELOW USED TO WORK !!!!###
provider             = azurerm.mysub  
key                  = "terraform.tfstate"
}
}
provider "azurerm" {
skip_provider_registration = true
subscription_id            = "xxxxxx-xxxxx-xxxxx-xxxxx"  
alias                      = "mysub"
features {
}
}

然而,在升级之后,它说这个块不允许提供程序(我不记得确切的错误信息)。所以我把它改成了

terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
}
}
backend "azurerm" {
resource_group_name  = "my-rg"
storage_account_name = "my-sa"
###!!!! Direct Reference to Subscription ID !!!!###
subscription_id      = "xxxxxx-xxxxx-xxxxx-xxxxx"  
key                  = "terraform.tfstate"
}
}

但是现在它说,

│ Error: Backend initialization required, please run "terraform init"
│
│ Reason: Backend configuration changed for "azurerm"
│
│ The "backend" is the interface that Terraform uses to store state,
│ perform operations, etc. If this message is showing up, it means that the
│ Terraform configuration you're using is using a custom configuration for
│ the Terraform backend.
│
│ Changes to backend configurations require reinitialization. This allows
│ Terraform to set up the new configuration, copy existing state, etc. Please run
│ "terraform init" with either the "-reconfigure" or "-migrate-state" flags to
│ use the current configuration.
│
│ If the change reason above is incorrect, please verify your configuration
│ hasn't changed and try again. At this point, no changes to your existing
│ configuration or state have been made.

如果我想让它记住状态,以便它知道以后如何销毁相同的状态,我应该使用什么命令?我已经在这个RG中使用地形部署了几次资源迭代,并希望保持它的完整性。

起程拓殖init,

terraform init -reconfigure,

terraform init - migration -state??

由于后端位置没有改变,我想继续保持原样,但只是让它忽略后端块更新使用"provider"到使用"subscription_id"。我该使用哪个命令?

提前感谢!

terraform init文档对这种情况有如下说明:

使用已经初始化的后端重新运行init将更新工作目录以使用新的后端设置。必须提供-reconfigure-migrate-state来更新后端配置。

-migrate-state选项将尝试将现有状态复制到新的后端,并且根据更改的内容,可能会导致确认工作区状态迁移的交互式提示。-force-copy选项抑制这些提示和回答"是";移民问题。这意味着-migrate-state

-reconfigure选项忽略任何现有的配置,阻止任何现有状态的迁移。

这里的决策点是您是否希望Terraform采取显式操作来尝试将状态复制到新位置(-migrate-state),或者您是否希望Terraform完全忘记旧设置并直接使用新设置。

你说物理位置是不变的,相反,你只是用不同的方式写了相同的信息,所以-reconfigure是匹配这种情况的选项:这里不需要任何显式迁移,因为状态已经在"new";位置(功能上与旧位置相同,但Terraform无法知道)。


请注意,将后端配置与提供者相关联从来都是无效的,所以无论你之前工作的是什么,都不会以你想象的方式工作。

azurerm后端有寻找ARM_SUBSCRIPTION_ID环境变量的行为,如果你没有显式地设置subscription_id在其配置中,所以我猜你以前在环境变量设置的上下文中运行Terraform,因此后端能够找到适当的订阅ID来使用,即使你没有显式地设置它。

为什么后端没有拒绝无效参数provider我不清楚。这表明在Terraform或后端本身存在错误,该错误已被修复,因此Terraform现在正确地报告在该后端配置模式中没有声明参数provider

我添加了另一个答案,因为第一个没有解决我的问题。

运行:

terragrunt run-all plan --terragrunt-source-update

对我有用

从这里

:

在堆栈所在的位置对堆栈运行提供的terraform命令一个terragrunt模块树。该命令将递归查找在当前目录树中找到Terragrunt模块,然后运行terraform命令按依赖顺序排列(除非命令是destroy,其中如果命令以反向依赖顺序运行)。

如果您没有预料到这个错误,也可能是先前的状态被保留了-因此"rm -rf .terraform "然后再进行地形初始化可能是解决方案。

我不确定这是否是正确的方法,但通过更新本地地形,我能够在没有初始化消息的情况下解决这个问题。tfstate文件。

改变这一行

"subscription_id": null,

"subscription_id": "xxxx-xxxx-xxxx-xxxx",

最新更新