使用 terraform,如何使用 list/count for azure 创建具有唯一且不同名称的多个相同类型的资源?
Using terraform, how to create multiple resources of same type with unique and unidentical names using list/count for azure?
这是我要实现的目标的基本示例。我有两个文件 (main.tf) 和 (variable.tf),我想创建两个资源组,变量文件中是我希望资源组占用的名称列表。第一个资源组的名字,以后也类似。
所以帮我看看如何实现它。我正在使用 terraform v0.13。
main.tf 文件:
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
count = 2
name = var.resource_group_name
location = var.location
}
variable.tf 文件:
variable "resource_group_name" {
description = "Default resource group name that the network will be created in."
type = list
default = ["asd-rg","asd2-rg"]
}
variable "location" {
description = "The location/region where the core network will be created.
default = "westus"
}
您可以使用for_each 语法来创建多个相似类型的资源。它需要一组(唯一值)来迭代,因此将变量 resource_group_name
转换为 set.
variable.tf
variable "resource_group_name" {
description = "Default resource group name that the network will be created in."
type = list(string)
default = ["asd-rg","asd2-rg"]
}
variable "location" {
description = "The location/region where the core network will be created.
default = "westus"
}
main.tf
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = each.value // value from iteration
location = var.location
for_each = toset(var.resource_group_name) // convert list to set and iterate over it
}
编辑:变量可以是 string
、list
或 map
类型。这需要转换为 set
才能与 for_each
一起使用
您只需要像这样更改资源组块:
resource "azurerm_resource_group" "test" {
count = 2
name = element(var.resource_group_name, count.index)
location = var.location
}
因为您可以使用长度和计数的组合来创建多个具有唯一且不同名称的相同类型的资源。
您只需要像这样更改资源组块:
resource "azurerm_resource_group" "test" {
count = length(var.resource_group_name)
name = element(concat(var.resource_group_name, [""]), count.index)
location = var.location
}
这是我要实现的目标的基本示例。我有两个文件 (main.tf) 和 (variable.tf),我想创建两个资源组,变量文件中是我希望资源组占用的名称列表。第一个资源组的名字,以后也类似。 所以帮我看看如何实现它。我正在使用 terraform v0.13。
main.tf 文件:
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
count = 2
name = var.resource_group_name
location = var.location
}
variable.tf 文件:
variable "resource_group_name" {
description = "Default resource group name that the network will be created in."
type = list
default = ["asd-rg","asd2-rg"]
}
variable "location" {
description = "The location/region where the core network will be created.
default = "westus"
}
您可以使用for_each 语法来创建多个相似类型的资源。它需要一组(唯一值)来迭代,因此将变量 resource_group_name
转换为 set.
variable.tf
variable "resource_group_name" {
description = "Default resource group name that the network will be created in."
type = list(string)
default = ["asd-rg","asd2-rg"]
}
variable "location" {
description = "The location/region where the core network will be created.
default = "westus"
}
main.tf
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = each.value // value from iteration
location = var.location
for_each = toset(var.resource_group_name) // convert list to set and iterate over it
}
编辑:变量可以是 string
、list
或 map
类型。这需要转换为 set
才能与 for_each
您只需要像这样更改资源组块:
resource "azurerm_resource_group" "test" {
count = 2
name = element(var.resource_group_name, count.index)
location = var.location
}
因为您可以使用长度和计数的组合来创建多个具有唯一且不同名称的相同类型的资源。
您只需要像这样更改资源组块:
resource "azurerm_resource_group" "test" {
count = length(var.resource_group_name)
name = element(concat(var.resource_group_name, [""]), count.index)
location = var.location
}