升级 Terraform 提供程序会导致 "invalid resource instance data in state" 错误

Upgrading terraform provider results in "invalid resource instance data in state" errors

我将我的 Terraform AzureRM 提供程序从 1.44 升级到 2.23。 Terraform 版本为 0.13.0。有趣的是,现在我尝试 运行 terraform plan 我收到多个“状态中的无效资源实例数据”错误。

它们看起来像这样:

Error: Invalid resource instance data in state

  on .terraform\modules\redacted\terraform\modules\keyvault\main.tf line 1:
   1: resource "azurerm_key_vault" "keyvault" {

Instance module.redacted.module.keyvault.azurerm_key_vault.keyvault data could
not be decoded from the state: unsupported attribute "sku".

我的状态文件在 Azure 存储中。查看状态文件时,我们看到了问题:

    {
      "module": "module.redacted.module.keyvault",
      "mode": "managed",
      "type": "azurerm_key_vault",
      "name": "keyvault",
      "provider": "module.redacted.provider.azurerm",
      "instances": [
        {
       ...
            "sku": [
              {
                "name": "standard"
              }
            ],
            "sku_name": "standard",
      ....

为密钥保管库定义 sku 的最新方法是使用“sku_name”属性。但是旧方法已经进入状态文件,现在导致问题。

升级提供程序以便清除状态文件的首选方法是什么?或者这是必须手动完成的事情吗?

注意:我无法重现该问题。我从 1.44.0 升级到 2.23.0。我看到 sku[{}] 状态信息在状态文件中。当我 运行 应用 2.23.0 时它被删除了。

最安全的做法是备份,删除状态并导入。

删除状态并导入它(升级前检查状态产生错误)

▶ terraform state show azurerm_key_vault.example 
unsupported attribute "sku"
# azurerm_key_vault.example:
resource "azurerm_key_vault" "example" {

▶ terraform state rm azurerm_key_vault.example
Removed azurerm_key_vault.example
Successfully removed 1 resource instance(s).
▶ terraform import azurerm_key_vault.example /subscriptions/redacted/resourceGroups/example-resources/providers/Microsoft.KeyVault/vaults/pearcec
azurerm_key_vault.example: Importing from ID "/subscriptions/redacted/resourceGroups/example-resources/providers/Microsoft.KeyVault/vaults/pearcec"...
azurerm_key_vault.example: Import prepared!
  Prepared azurerm_key_vault for import
azurerm_key_vault.example: Refreshing state... [id=/subscriptions/redacted/resourceGroups/example-resources/providers/Microsoft.KeyVault/vaults/pearcec]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.


▶ terraform state show azurerm_key_vault.example | Select-String "sku"
InputStream:12:    sku_name                        = "standard"