2
votes

I upgraded my Terraform AzureRM provider from 1.44 to 2.23. Terraform version is 0.13.0. Interestingly now that I try to run terraform plan I get multiple "Invalid resource instance data in state" errors.

They look like this:

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".

My state file is in Azure storage. When looking into the state file we see the problem:

    {
      "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",
      ....

The newest way to define the sku for keyvault is using the "sku_name" attribute. But the old way has stuck into the state file and is now causing problems.

What is the preferred way to upgrade the provider so that the state file is also cleared up? Or is this something that has to be done manually?

1
Do you have the sequence of commands you ran to get into this state? What version of terraform? I am unable to repo. I see the sku[{}] shows up in my state, but gets removed during the upgrade.Christian Pearce

1 Answers

2
votes

NOTE: I was unable to reproduce the issue. I went from 1.44.0 to 2.23.0. I saw the sku[{}] state information was in the state file. When I ran an apply with 2.23.0 and it was removed.

The safest thing to do is take a backup, remove the state and import it.

Remove the state and import it (inspecting state before upgrade yields the error)

▶ 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"