Azure terraform import does not import all attributes in terraform state file after terraform provider version upgrade from 2.x to 3.x

I upgraded deprecated resource 'azurerm_function_app' to 'azurerm_linux_web_app' and 'azurerm_windows_function_app'. Removed resources from terraform state file with 'terraform state rm command' successfully. terraform import adds all attributes in portal but state file does not add some app setting attributes after terraform import and apply.

~ resource "azurerm_linux_web_app" "mcp-frontend" {
      ~ app_settings                       = {
          + "WEBSITE_VNET_ROUTE_ALL"                          = "1"
~ resource "azurerm_windows_function_app" "api" {
      ~ app_settings                       = {
          + "APPINSIGHTS_INSTRUMENTATIONKEY"     = (sensitive)
          + "WEBSITE_VNET_ROUTE_ALL"             = "1"
1

There are 1 best solutions below

0
On BEST ANSWER

When Provider upgrades are made , they often introduce changes in behavior and attributes in the latest resources.

Code:

resource "azurerm_function_app" "example" {
  name                       = "testazure-functions"
 location              = data.azurerm_resource_group.example.location
  resource_group_name   = data.azurerm_resource_group.example.name
   app_service_plan_id       = azurerm_app_service_plan.example.id
  storage_connection_string = azurerm_storage_account.example.primary_connection_string
}

enter image description here

Check the terraform registry :

for azurerm_linux_web_app or azurerm_windows_function_app.

When we compare the attribute names and values in azurerm_function_app resource which is deprecated . Some values are changes and not necessary to provide or some values might be missed.

enter image description here

These values are imported automatically and So in the new configurations check which values are missed and its better to add imported resource if required using resource block to reflect changes.

terraform import azurerm_linux_web_app.name /subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.Web/sites/<function_app_name>

Add required attributes:

resource "azurerm_linux_web_app" "example" {
  name                = "examplehfhf"
  location              = data.azurerm_resource_group.example.location
  resource_group_name   = data.azurerm_resource_group.example.name
  service_plan_id     = azurerm_service_plan.example.id  
   …
  site_config {}
}

enter image description here

Reference: Azure Provider: Migrating to a renamed resource | Guides | hashicorp/azurerm | Terraform Registry