升级Terraform提供程序会导致“状态中的资源实例数据无效”错误

时间:2020-08-14 11:02:23

标签: terraform terraform-provider-azure terraform0.12+

我将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",
      ....

为keyvault定义sku的最新方法是使用“ sku_name”属性。但是以前的方法一直停留在状态文件中,现在引起了问题。

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

1 个答案:

答案 0 :(得分:2)

注意:我无法重现该问题。我从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"