Terraform-provider-azurerm: terraform destroy for resource azurerm_resource_group_template_deployment fails

Created on 23 Oct 2020  ·  11Comments  ·  Source: terraform-providers/terraform-provider-azurerm

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform (and AzureRM Provider) Version

Terraform v0.13.4

Affected Resource(s)

  • azurerm v2.32.0

Terraform Configuration Files

resource "azurerm_resource_group_template_deployment" "virtual-directory" {
  deployment_mode     = "Incremental"
  name                = "virtual-directory"
  resource_group_name = azurerm_resource_group.webapp-rg.name  
  tags                = var.tags

  template_content = <<TEMPLATE
  {
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
          "webAppName": {
              "type": "String"
          },
        "webAppSlotName": {
              "type": "String"
          },
          "virtualApplications":{
          "type": "Array",
          "defaultValue":[
              {
              "virtualPath": "/",
              "physicalPath": "site\\wwwroot",
              "preloadEnabled": false,
              "virtualDirectories": null
              },
              {
              "virtualPath": "/sub",
              "physicalPath": "site\\wwwroot\\sub",
              "preloadEnabled": false,
              "virtualDirectories": null
              }
          ]
          }
    },
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.Web/sites/config",
            "name": "[concat(parameters('webAppName'), '/web')]",
            "apiVersion": "2016-08-01",
            "properties": {
                "virtualApplications": "[parameters('virtualApplications')]"
            },
            "dependsOn": []
        },
        {
            "type": "Microsoft.Web/sites/slots/config",
            "name": "[concat(parameters('webAppName'),'/', parameters('webAppSlotName'),'/web')]",
            "apiVersion": "2016-08-01",
            "properties": {
                "virtualApplications": "[parameters('virtualApplications')]"
            },
            "dependsOn": []
        }

    ]
  }
  TEMPLATE

  parameters_content = <<JSON
  {
    "webAppName": {
      "value": "${azurerm_app_service.appservice.name}"
    },
    "webAppSlotName": {
      "value": "${azurerm_app_service_slot.test-app-service-web-slot.name}"
    },
    "virtualApplications": {
      "value": [
        {
          "physicalPath": "site\\wwwroot",
          "preloadEnabled": false,
          "virtualDirectories": null,
          "virtualPath": "/"
        },
        {
          "physicalPath": "site\\wwwroot\\sub",
          "preloadEnabled": false,
          "virtualDirectories": null,
          "virtualPath": "/sub"
        }
      ]
    }
  }
  JSON

  depends_on = [
    azurerm_app_service.appservice,
    azurerm_app_service_slot.test-app-service-web-slot
  ]
}

Debug Output

https://gist.github.com/dhjensen/cf3bcdc83784ae476c5fcebb808f1d0a

Panic Output

Expected Behavior

It should destroy the resources originally created by azurerm_resource_group_template_deployment

Actual Behavior

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

azurerm_resource_group_template_deployment.virtual-directory: Destroying... [id=/subscriptions/f40698d5-0714-4aa1-8b9b-bcc4a86fe7d1/resourceGroups/webapp-rg/providers/Microsoft.Resources/deployments/virtual-directory]        

Error: removing items provisioned by this Template Deployment: determining API Versions for Resource Providers: unable to determine API version for Resource Type "sites/config" (Resource Provider "Microsoft.Web")

It complains that it cannot find api version for "Microsoft.Web/sites/config" which is true if you check the log output.
For this type there is no api version in the output.

The "template" is described here though: https://docs.microsoft.com/en-us/azure/templates/microsoft.web/2016-08-01/sites/config

Is it intended that It cannot try to destroy something where it cannot determine the API version?

Steps to Reproduce

  1. terraform destroy
  2. Kaboom

Important Factoids

References

  • #0000
bug servictemplate

Most helpful comment

I learned how to get around this until the maintainers fix the problem. I just remove it from the state.

https://stackoverflow.com/a/55271805/4236189

All 11 comments

Yep, I'm in this position too. I ended up renaming some resources in my main.tf file and the terraform apply wants to destroy in order to recreate, but I'm stuck here.

Any updates on this?

I learned how to get around this until the maintainers fix the problem. I just remove it from the state.

https://stackoverflow.com/a/55271805/4236189

I'm getting the same behaviour when trying to destroy azurerm_resource_group_template_deployment for Microsoft.DBforPostgreSQL/flexibleServers/firewallRules (sub-resorce)

{
  "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
      "serverName": {
          "type": "String"
      },
      "firewallRule": {
          "defaultValue": {},
          "type": "Object"
      }
  },
  "variables": {
      "api": "2020-02-14-privatepreview",
      "firewallRule": "[parameters('firewallRule')]"
  },
  "resources": [
    {
        "type": "Microsoft.DBforPostgreSQL/flexibleServers/firewallRules",
        "name": "[concat(parameters('serverName'),'/',variables('firewallRule').name)]",
        "apiVersion": "[variables('api')]",
        "properties": {
            "StartIpAddress": "[variables('firewallRule').startIPAddress]",
            "EndIpAddress": "[variables('firewallRule').endIPAddress]"
        }
    }
  ]
}

deploying works fine but destroying messes up finding the Resource Provider somehow:

...
TestPRV_PgFlexServer 2020-11-11T13:07:12Z retry.go:80: Returning due to fatal error: FatalError{Underlying: error while running command: exit status 1; 
Error: removing items provisioned by this Template Deployment: determining API Versions for Resource Providers: unable to determine API version for Resource Type "flexibleServers/firewallRules" (Resource Provider "Microsoft.DBforPostgreSQL")
...

This has been released in version 2.37.0 of the provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. As an example:

provider "azurerm" {
    version = "~> 2.37.0"
}
# ... other configuration ...

I'm testing this out using the new azurerm provider 2.3.7.0, but now getting this error when destroying resources created by azurerm_resource_group_template_deployment

Error: removing items provisioned by this Template Deployment: deleting Nested Resource "/subscriptions/aaa-aaa-aaa-aaa/resourceGroups/qa-southcentralus-appsvcs-rg/providers/Microsoft.Web/sites/qa-southcentralus-appsvc-app/config/web": pollingTrackerBase#updateRawBody: failed to unmarshal response body: StatusCode=0 -- Original Error: invalid character '<' looking for beginning of value

invalid character '<' looking for beginning of value

this is a misleading error for the value <nil>, which happens in the go-sdk when the apiVersion is not found in the resources list.
See: https://management.azure.com/providers/Microsoft.Web?api-version=2016-08-01

@ajklotz the "apiVersion": "2016-08-01" is invalid for Microsoft.Web resources, please use 2016-06-01

@evertonmc I'm actually using 2020-06-01

Shouldn't that be valid? Here is the code I'm using:

resource "azurerm_resource_group_template_deployment" "api_virtual_directory" {
  name                = "api_virtual_directory"
  resource_group_name = azurerm_resource_group.resourceGroup.name
  deployment_mode     = "Incremental"
  template_content    = <<TEMPLATE
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "webAppName": {
        "type": "String",
        "defaultValue": "${module.webapp["api"].name}"
    },
    "virtualApplications":{
      "type": "Array",
      "defaultValue":[
        {
        "virtualPath": "/",
        "physicalPath": "site\\wwwroot",
        "preloadEnabled": false,
        "virtualDirectories": null
        },
        {
        "virtualPath": "/v2",
        "physicalPath": "site\\wwwroot\\v2",
        "preloadEnabled": false,
        "virtualDirectories": null
        },
        {
        "virtualPath": "/appversions",
        "physicalPath": "site\\wwwroot\\test",
        "preloadEnabled": false,
        "virtualDirectories": null
        }
      ]
    }
  },
  "resources": [
    {
      "type": "Microsoft.Web/sites/config",
      "name": "[concat(parameters('webAppName'), '/web')]",
      "apiVersion": "2020-06-01",
      "properties": {
          "virtualApplications": "[parameters('virtualApplications')]"
      }
    }
  ]
}
TEMPLATE
  depends_on          = [module.webapp["api"]]
}

resource "azurerm_resource_group_template_deployment" "api_virtual_directory_slot" {
  count               = var.deploymentEnvironment == "CI" ? 0 : 1
  name                = "api_virtual_directory_slot"
  resource_group_name = azurerm_resource_group.resourceGroup.name
  deployment_mode     = "Incremental"
  template_content    = <<TEMPLATE
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "webAppName": {
        "type": "String",
        "defaultValue": "${module.webapp["api"].name}"
    },
    "virtualApplications":{
      "type": "Array",
      "defaultValue":[
        {
        "virtualPath": "/",
        "physicalPath": "site\\wwwroot",
        "preloadEnabled": false,
        "virtualDirectories": null
        },
        {
        "virtualPath": "/v2",
        "physicalPath": "site\\wwwroot\\v2",
        "preloadEnabled": false,
        "virtualDirectories": null
        },
        {
        "virtualPath": "/appversions",
        "physicalPath": "site\\wwwroot\\test",
        "preloadEnabled": false,
        "virtualDirectories": null
        }
      ]
    }
  },
  "resources": [
    {
      "type": "Microsoft.Web/sites/slots/config",
      "name": "[concat(parameters('webAppName'), '/staging/web')]",
      "apiVersion": "2020-06-01",
      "properties": {
          "virtualApplications": "[parameters('virtualApplications')]"
      }
    }
  ]
}
TEMPLATE
  depends_on          = [module.webapp["api"]]
}

I am not able to check this right now, but you can check the Resource provider API by yourself with the link above.

Well, its a valid version so I don't know what the limitation is here. As far as I see, this issue isn't resolved. The api version I'm using is definitely listed

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

Was this page helpful?
0 / 5 - 0 ratings