繁体   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