簡體   English   中英

Terraform - 為 Azure 個位置創建一個列表(字符串)

[英]Terraform - creating a list(string) for Azure locations

我正在嘗試使用 Azure 中的 Terraform 將資源組部署到多個位置,但出現錯誤: “屬性“位置”的值不合適:需要字符串。”

我是 Terraform 的新手,所以我不確定為什么這不起作用。

我的代碼:

locals {
  webappsperloc = 4
}

resource "azurerm_resource_group" "rg" {
  count    = length(var.webapplocs)
  name     = "whatever${count.index}"
  location = var.webapplocs[count.index]
  tags     = var.tags

}

resource "random_string" "webapprnd" {
  length  = 8
  lower   = true
  number  = true
  upper   = false
  special = false
}

resource "azurerm_app_service_plan" "plan" {
  count               = length(var.webapplocs)
  name                = "whatever-${var.webapplocs[count.index]}"
  location            = var.webapplocs[count.index]
  resource_group_name = element(azurerm_resource_group.rg.*.name,count.index)
  tags                = var.tags

  sku {
    tier = "standard"
    size = "S1"
  }
}
resource "azurerm_app_service" "app" {
  count               = local.webappsperloc
  name                = format("webapp-%s-%02d-%s", random_string.webapprnd.result, count.index + 1, element(var.webapplocs, count.index))
  resource_group_name = element(azurerm_resource_group.rg.*.name,count.index)
  location            = element(var.webapplocs, count.index)
  app_service_plan_id = element(azurerm_app_service_plan.plan.*.id, count.index)
  https_only          = true

  site_config {
    dotnet_framework_version = "v4.0"
    always_on                = true
    ftps_state               = "Disabled"
    min_tls_version          = "1.2"
  }

  # tags = {
  #   environment = var.tags
  #   source      = "terraform"
  # }
}
variable "webapplocs" {
  default = ["northeurope", "westeurope", "eastus", "uksouth"]
}

#variable "loc" {
#  description = "Default Azure region"
#  default     = "northeurope"
#}

variable "tags" {
  default = {
    source = "Terraform"
    env    = "Dev"
  }
}
#terraform.tfvars


loc = "northeurope"

tags = {
  source = "terrafrom"
  env    = "Dev"
}

webapplocs = ["northeurope","westeurope","eastus","uksouth"]
terraform {
  required_providers {
    azure = {
      source  = "hashicorp/azurerm"
      version = "=2.76.0"
    }
  }
  backend "azurerm" {
    resource_group_name  = "xxxx"
    storage_account_name = "xxxx"
    container_name       = "xxxx"
    key                  = "xxxx"
  }
}

# azure provider
provider "azure" {
  features {}
}

如果您還需要什么,請告訴我。 我以前用過這個 function 沒有問題。

謝謝。

編輯:更新代碼以反映我設法開始工作的內容。

根據您收到的錯誤,我認為這與此行中的無效屬性引用有關:

location = var.location

azurerm_resource_group資源只需要一個位置值,並且您提供了一個字符串列表(基於location變量定義),因此您會收到錯誤消息:

“屬性“位置”的值不合適:需要字符串。”

由於您需要所有區域(即location )的資源組,我建議使用for_each [1] 而不是count

這樣您就可以在每個區域擁有每種資源之一。 這將如何工作(並且可能有更優雅的方式來做到這一點):

resource "azurerm_resource_group" "rg" {
  for_each = toset(var.location)
  name     = "${each.value}-${var.resource_group_name}"
  location = each.value

  tags = {
    environment = var.environment
    source      = "terraform"
  }
}

resource "azurerm_app_service_plan" "plan" {
  for_each            = toset(var.location)
  name                = "${each.value}-${var.app_service_plan}"
  location            = azurerm_resource_group.rg[each.value].location # or alternatively each.value
  resource_group_name = azurerm_resource_group.rg[each.value].name


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

resource "azurerm_app_service" "app" {
  for_each            = toset(var.location)
  name                = "${each.value}-${var.app_service}"
  resource_group_name = azurerm_resource_group.rg[each.value].name
  location            = azurerm_resource_group.rg[each.value].location # or alternatively each.value
  app_service_plan_id = azurerm_app_service_plan.plan[each.value].id
  https_only          = true

  site_config {
    dotnet_framework_version = "v4.0"
    always_on                = true
    ftps_state               = "Disabled"
    min_tls_version          = "1.2"
  }

  tags = {
    environment = var.environment
    source      = "terraform"
  }
}

請注意,我在這里使用toset [2] 將列表轉換為集合,以便能夠在其上使用for_each 此外,我正在使用這些值來使跨區域的資源名稱更加可區分,即,將each.value附加到您要用於名稱的變量。


[1] https://www.terraform.io/language/meta-arguments/for_each

[2] https://www.terraform.io/language/functions/toset

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM