'The Terraform CLI get a Error: ID was missing the `slots` element

I am executing terraform.exe apply on windows and receive the error:

azurerm_subnet.subnet: Refreshing state... [id=<...>]
azurerm_app_service_plan.service_plan: Refreshing state... [id=<...>]
azurerm_app_service.app: Refreshing state... [id=<...>]
azurerm_app_service_virtual_network_swift_connection.test: Refreshing state... [id=<...>]
azurerm_app_service_slot.production: Refreshing state... [id=<...>]
azurerm_app_service_slot.staging: Refreshing state... [id=<...>]

Error: ID was missing the `slots` element

I am attempting to build an Azure WebApp with different slots and docker images with terraform. It should deploy an Azure WebApp with different slots based on Dockerfile images.

The first run is without an error. I receive the error when I refresh the resources.

I am using azurerm provider version 2.1.0 and the azurerm backend.

See the following terraform file:

terraform {
  backend "azurerm" {
    resource_group_name  = "..."
    storage_account_name = "..."
    container_name       = "..."
    key                  = "..."

    subscription_id      = "..."
    tenant_id            = "..."

    sas_token            = "...."
  }
}

provider "azurerm" {
  version = "~>2.1.0"
  features {}
}

variable "environment" {
  default = "production"
}
variable "resource_group" {}
variable "location" {
  default = "West Europe"
}
variable "app_name" {}
variable "network" {}
variable "subnet_prefix" {}

resource "azurerm_app_service_plan" "service_plan" {
  name                = var.app_name
  location            = var.location
  resource_group_name = var.resource_group

  kind = "Linux"
  reserved = true

  sku {
    tier = "Standard"
    size = "S1"
  }

  tags = {
    Environment = var.environment
    Cost        = "€0,081/Stunde"
  }
}

resource "azurerm_app_service" "app" {
  name                = var.app_name
  location            = var.location
  resource_group_name = var.resource_group
  app_service_plan_id = azurerm_app_service_plan.service_plan.id
  depends_on = [azurerm_app_service_plan.service_plan]

  site_config {
    linux_fx_version = "DOCKER|<...>.azurecr.io/<...>:0.0.1-95"
    always_on        = "true"
  }

  app_settings = {
    ...
  }

  storage_account {
    access_key = "..."
    account_name = "..."
    name = "certs"
    share_name = "certs"
    type = "AzureBlob"
    mount_path = "/var/certs"
  }

  tags = {
    Environment = var.environment
  }
}

resource "azurerm_app_service_slot" "production" {
  name                = var.app_name
  app_service_name    = azurerm_app_service.app.name
  location            = azurerm_app_service.app.location
  resource_group_name = var.resource_group
  app_service_plan_id = azurerm_app_service_plan.service_plan.id

  depends_on = [azurerm_app_service.app]

  site_config {
    linux_fx_version = "DOCKER|<...>.azurecr.io/<...>:0.0.1-95"
    always_on        = "true"

  }

  app_settings = {
    "SOME_KEY" = "some-value"
  }
}

resource "azurerm_app_service_slot" "staging" {
  name                = "staging"
  app_service_name    = azurerm_app_service.app.name
  location            = azurerm_app_service.app.location
  resource_group_name = var.resource_group
  app_service_plan_id = azurerm_app_service_plan.service_plan.id

  depends_on = [azurerm_app_service.app]

  site_config {
    linux_fx_version = "DOCKER|<...>.azurecr.io/<...>:latest"
    always_on        = "true"
  }
}

resource "azurerm_subnet" "subnet" {
  name                 = var.app_name
  resource_group_name  = var.resource_group
  virtual_network_name = var.network
  address_prefix       = var.subnet_prefix

  delegation {
    name = var.app_name

    service_delegation {
      name    = "Microsoft.Web/serverFarms"
      actions = [
        "Microsoft.Network/networkinterfaces/*",
        "Microsoft.Network/virtualNetworks/subnets/action",
        "Microsoft.Network/virtualNetworks/subnets/join/action",
        "Microsoft.Network/virtualNetworks/subnets/prepareNetworkPolicies/action",
        "Microsoft.Network/virtualNetworks/subnets/unprepareNetworkPolicies/action"
      ]
    }
  }
}

resource "azurerm_app_service_virtual_network_swift_connection" "test" {
  app_service_id = azurerm_app_service.app.id
  subnet_id      = azurerm_subnet.subnet.id
  depends_on = [
    azurerm_app_service.app,
    azurerm_subnet.subnet
  ]
}

What does a missing slots element mean in this context?



Solution 1:[1]

Terraform treates resource identifiers as case sensitive, but azure doesn't.

Somewhere inside your state file you probably have an id like /Slots/ instead of /slots/.

You can use terraform state pull and terraform state push to manually edit your state file I think. Usually not recommended, but TF validation insists on forcing case sensitivity while the azure portal itself will show you resource IDs with inconsistent capitalization :/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TeamDman