在 terraform 的网络模块中添加服务端点作为子网变量的一部分
Adding service endpoints as part of subnet variables in network module in terraform
我创建了一个网络模块。下面是它的代码
variables.tf
variable "resource_group_name" {
description = "Name of the resource group to be imported."
type = string
}
variable "location" {
description = "The location of the vnet to create. Defaults to the location of the resource group."
type = string
default = null
}
variable "vnets" {
type = map(object({
address_space = string
subnets = list(object({
subnet_name = string
subnet_address = string
service_endpoints = list(string)
}))
}))
default = {
"bupavnet1" = {
address_space = "192.168.0.0/16",
subnets = []
},
"bupavnet2" = {
address_space = "10.0.0.0/16",
subnets = [
{
subnet_name = "subnet1_bupavnet1"
subnet_address = "10.0.2.0/24"
service_endpoints = []
},
{
subnet_name = "subnet2_bupavnet1"
subnet_address = "10.0.0.0/24"
service_endpoints = ["Microsoft.AzureCosmosDB","Microsoft.ContainerRegistry"]
}
]
},
"bupavnet3" = {
address_space = "10.80.0.0/16"
subnets = [
{
subnet_name = "subnet1_bupavnet3"
subnet_address = "10.80.2.0/24"
service_endpoints = ["Microsoft.AzureCosmosDB","Microsoft.ContainerRegistry"]
},
{
subnet_name = "subnet2_bupavnet3"
subnet_address = "10.80.1.0/24"
service_endpoints = ["Microsoft.AzureCosmosDB","Microsoft.ContainerRegistry"]
},
{
subnet_name = "subnet3_bupavnet3"
subnet_address = "10.80.0.0/24"
service_endpoints = ["Microsoft.AzureCosmosDB","Microsoft.ContainerRegistry"]
},
]
}
}
}
output.tf
output "vnet_names" {
description = "The name of the virtual networks"
value = tomap({for k, v in azurerm_virtual_network.vnets: k => v.name})
}
output "vnet_addresses" {
description = "The name of the virtual networks"
value = tomap({for k, v in azurerm_virtual_network.vnets: k => v.address_space})
}
output "subnet_names" {
description = "The name of the subnets"
value = tomap({for k, v in azurerm_subnet.subnets: k => v.name})
}
output "subnet_addresses" {
description = "The name of the subnet addresses"
value = {for k, v in azurerm_subnet.subnets: k => v.address_prefixes}
}
output "subnet_ids" {
description = "The name of the subnet addresses"
value = tomap({for k, v in azurerm_subnet.subnets: k => v.id})
}
main.tf
data "azurerm_resource_group" "network" {
name = var.resource_group_name
}
resource "azurerm_virtual_network" "vnets" {
for_each = var.vnets
name = each.key
resource_group_name = data.azurerm_resource_group.network.name
location = data.azurerm_resource_group.network.location
address_space = [each.value.address_space]
}
resource "azurerm_subnet" "subnets" {
for_each = local.subnets
name = each.value.subnet_name
resource_group_name = data.azurerm_resource_group.network.name
virtual_network_name = azurerm_virtual_network.vnets[each.value.vnet_name].name
address_prefixes = [each.value.subnet_address]
service_endpoints = [each.value.service_endpoints]
}
locals.tf
locals {
subnets_flatlist = flatten([for key, val in var.vnets : [
for subnet in val.subnets : {
vnet_name = key
subnet_name = subnet.subnet_name
subnet_address = subnet.subnet_address
service_endpoints = subnet.service_endpoints
}
]
])
subnets = { for subnet in local.subnets_flatlist : subnet.subnet_name => subnet }
}
main.tf
resource "azurerm_resource_group" "rg2" {
name = "rg2"
location = "Australia East"
}
module "network" {
source = "./network_resources"
resource_group_name = azurerm_resource_group.rg2.name
location = azurerm_resource_group.rg2.location
}
我在执行 terraform plan 时遇到以下错误
│错误:属性值类型不正确
│
│ 在 network_resources\main.tf 第 21 行,在资源“azurerm_subnet”“子网”中:
│ 21: service_endpoints = [each.value.service_endpoints]
│ ├──────────────────
│ │ each.value.service_endpoints 是包含 2 个元素的字符串列表
│
│ 属性“service_endpoints”的值不合适:元素 0:需要字符串。
请告诉我如何将 service_endpoints 添加为变量 vnets 的一部分,以便模块开始正常工作
您收到此错误的原因是您的 service_endpoints
已经是一个列表,但您将其包装在 []
中,这正在创建一个列表列表。从 service_endpoints
中删除 []
:
resource "azurerm_subnet" "subnets" {
for_each = local.subnets
name = each.value.subnet_name
resource_group_name = data.azurerm_resource_group.network.name
virtual_network_name = azurerm_virtual_network.vnets[each.value.vnet_name].name
address_prefixes = [each.value.subnet_address]
service_endpoints = each.value.service_endpoints
}
我创建了一个网络模块。下面是它的代码
variables.tf
variable "resource_group_name" {
description = "Name of the resource group to be imported."
type = string
}
variable "location" {
description = "The location of the vnet to create. Defaults to the location of the resource group."
type = string
default = null
}
variable "vnets" {
type = map(object({
address_space = string
subnets = list(object({
subnet_name = string
subnet_address = string
service_endpoints = list(string)
}))
}))
default = {
"bupavnet1" = {
address_space = "192.168.0.0/16",
subnets = []
},
"bupavnet2" = {
address_space = "10.0.0.0/16",
subnets = [
{
subnet_name = "subnet1_bupavnet1"
subnet_address = "10.0.2.0/24"
service_endpoints = []
},
{
subnet_name = "subnet2_bupavnet1"
subnet_address = "10.0.0.0/24"
service_endpoints = ["Microsoft.AzureCosmosDB","Microsoft.ContainerRegistry"]
}
]
},
"bupavnet3" = {
address_space = "10.80.0.0/16"
subnets = [
{
subnet_name = "subnet1_bupavnet3"
subnet_address = "10.80.2.0/24"
service_endpoints = ["Microsoft.AzureCosmosDB","Microsoft.ContainerRegistry"]
},
{
subnet_name = "subnet2_bupavnet3"
subnet_address = "10.80.1.0/24"
service_endpoints = ["Microsoft.AzureCosmosDB","Microsoft.ContainerRegistry"]
},
{
subnet_name = "subnet3_bupavnet3"
subnet_address = "10.80.0.0/24"
service_endpoints = ["Microsoft.AzureCosmosDB","Microsoft.ContainerRegistry"]
},
]
}
}
}
output.tf
output "vnet_names" {
description = "The name of the virtual networks"
value = tomap({for k, v in azurerm_virtual_network.vnets: k => v.name})
}
output "vnet_addresses" {
description = "The name of the virtual networks"
value = tomap({for k, v in azurerm_virtual_network.vnets: k => v.address_space})
}
output "subnet_names" {
description = "The name of the subnets"
value = tomap({for k, v in azurerm_subnet.subnets: k => v.name})
}
output "subnet_addresses" {
description = "The name of the subnet addresses"
value = {for k, v in azurerm_subnet.subnets: k => v.address_prefixes}
}
output "subnet_ids" {
description = "The name of the subnet addresses"
value = tomap({for k, v in azurerm_subnet.subnets: k => v.id})
}
main.tf
data "azurerm_resource_group" "network" {
name = var.resource_group_name
}
resource "azurerm_virtual_network" "vnets" {
for_each = var.vnets
name = each.key
resource_group_name = data.azurerm_resource_group.network.name
location = data.azurerm_resource_group.network.location
address_space = [each.value.address_space]
}
resource "azurerm_subnet" "subnets" {
for_each = local.subnets
name = each.value.subnet_name
resource_group_name = data.azurerm_resource_group.network.name
virtual_network_name = azurerm_virtual_network.vnets[each.value.vnet_name].name
address_prefixes = [each.value.subnet_address]
service_endpoints = [each.value.service_endpoints]
}
locals.tf
locals {
subnets_flatlist = flatten([for key, val in var.vnets : [
for subnet in val.subnets : {
vnet_name = key
subnet_name = subnet.subnet_name
subnet_address = subnet.subnet_address
service_endpoints = subnet.service_endpoints
}
]
])
subnets = { for subnet in local.subnets_flatlist : subnet.subnet_name => subnet }
}
main.tf
resource "azurerm_resource_group" "rg2" {
name = "rg2"
location = "Australia East"
}
module "network" {
source = "./network_resources"
resource_group_name = azurerm_resource_group.rg2.name
location = azurerm_resource_group.rg2.location
}
我在执行 terraform plan 时遇到以下错误
│错误:属性值类型不正确 │ │ 在 network_resources\main.tf 第 21 行,在资源“azurerm_subnet”“子网”中: │ 21: service_endpoints = [each.value.service_endpoints] │ ├────────────────── │ │ each.value.service_endpoints 是包含 2 个元素的字符串列表 │ │ 属性“service_endpoints”的值不合适:元素 0:需要字符串。
请告诉我如何将 service_endpoints 添加为变量 vnets 的一部分,以便模块开始正常工作
您收到此错误的原因是您的 service_endpoints
已经是一个列表,但您将其包装在 []
中,这正在创建一个列表列表。从 service_endpoints
中删除 []
:
resource "azurerm_subnet" "subnets" {
for_each = local.subnets
name = each.value.subnet_name
resource_group_name = data.azurerm_resource_group.network.name
virtual_network_name = azurerm_virtual_network.vnets[each.value.vnet_name].name
address_prefixes = [each.value.subnet_address]
service_endpoints = each.value.service_endpoints
}