[英]Terraform - creating a list(string) for Azure locations
I'm trying to deploy a resource group to several locations using Terraform in Azure but get the error: "Inappropriate value for attribute "location": string required."我正在尝试使用 Azure 中的 Terraform 将资源组部署到多个位置,但出现错误: “属性“位置”的值不合适:需要字符串。”
I'm very new to Terraform so I'm not sure why this doesn't work.我是 Terraform 的新手,所以我不确定为什么这不起作用。
My code:我的代码:
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 {}
}
If you need anything else please let me know.如果您还需要什么,请告诉我。 I've used this function before without issue.我以前用过这个 function 没有问题。
Thanks.谢谢。
edit: code updated to reflect what I managed to get working.编辑:更新代码以反映我设法开始工作的内容。
Based on the error you are getting, I think this is related to invalid attribute reference on this line:根据您收到的错误,我认为这与此行中的无效属性引用有关:
location = var.location
The azurerm_resource_group
resource requires only a single value for location and you are providing a list of strings (based on the location
variable definition), so you get the error: azurerm_resource_group
资源只需要一个位置值,并且您提供了一个字符串列表(基于location
变量定义),因此您会收到错误消息:
"Inappropriate value for attribute "location": string required." “属性“位置”的值不合适:需要字符串。”
Since you want resource groups in all regions (ie, location
), I would suggest using for_each
[1] instead of count
.由于您需要所有区域(即location
)的资源组,我建议使用for_each
[1] 而不是count
。
That way you will be able to have one of each resources per region.这样您就可以在每个区域拥有每种资源之一。 How this would work (and there might be more elegant ways of doing it):这将如何工作(并且可能有更优雅的方式来做到这一点):
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"
}
}
Note that I am using toset
[2] here to convert the list to a set in order to be able to use for_each
on it.请注意,我在这里使用toset
[2] 将列表转换为集合,以便能够在其上使用for_each
。 Also I am using the values to make names of resources across regions more distinguishable, ie, appending each.value
to the variables you want to use for names.此外,我正在使用这些值来使跨区域的资源名称更加可区分,即,将each.value
附加到您要用于名称的变量。
[1] https://www.terraform.io/language/meta-arguments/for_each [1] https://www.terraform.io/language/meta-arguments/for_each
[2] https://www.terraform.io/language/functions/toset [2] https://www.terraform.io/language/functions/toset
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.