Terraform CLI 出现错误:ID 缺少“slots”元素

问题描述 投票:0回答:1

我正在 Windows 上执行

terraform.exe apply
并收到错误:

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

我正在尝试使用 terraform 构建具有不同插槽和 docker 图像的 Azure WebApp。它应该根据 Dockerfile 映像部署具有不同插槽的 Azure WebApp。

第一次运行没有错误。我刷新资源时收到错误。

我正在使用 azurerm 提供程序版本 2.1.0 和 azurerm 后端。

请参阅以下 terraform 文件:

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
  ]
}

在这种情况下,缺少槽元素意味着什么?

azure runtime-error terraform azure-web-app-service terraform-provider-azure
1个回答
5
投票

Terraform 将资源标识符视为区分大小写,但 azure 则不然。

在状态文件中的某个位置,您可能有一个类似

/Slots/
的 id,而不是
/slots/

我认为您可以使用

terraform state pull
terraform state push
手动编辑状态文件。通常不推荐,但 TF 验证坚持强制区分大小写,而 azure 门户本身会向您显示大小写不一致的资源 ID :/

© www.soinside.com 2019 - 2024. All rights reserved.