[英]Terraform "vpc" module "private_subnets" value after "apply"
[英]AWS VPC module public and private subnets - Terraform
我正在嘗試使用我使用“template_file”創建的公共和私有子網創建 AWS vpc 環境,但是,我在 VPC 模塊內定義公共和私有子網時遇到了問題。
我使用以下方法生成了我的子網:
data "template_file" "cidrsubnets" {
count = var.subnet_count[terraform.workspace]
template = "$${cidrsubnet(vpc_cidr,8,current_count)}"
vars = {
vpc_cidr = var.network_address_space[terraform.workspace]
current_count = count.index
}
}
在 VPC 模塊中,上面的代碼將我的所有子網添加到公共子網中(有效):
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "${local.prefix}-vpc"
cidr = var.network_address_space[terraform.workspace]
azs = slice(data.aws_availability_zones.available.names, 0, var.subnet_count[terraform.workspace])
public_subnets = data.template_file.cidrsubnets[*].rendered
private_subnets = []
create_database_subnet_group = false
enable_dns_hostnames = true
enable_nat_gateway = true
enable_s3_endpoint = true
enable_dynamodb_endpoint = true
tags = local.common_tags
}
但是,我想將列表中的偶數子網添加到公共子網,將列表中的奇數子網添加到私有子網。 我仍然沒有自動將子網拆分為私有和公共(這是我需要的),但嘗試在列表中添加特定元素並得到上面的錯誤:
你能幫忙嗎? 感謝您的答復。
在這里使用template_file
數據源似乎過於復雜。 我想您想要實現的目標類似於以下內容(使用locals
以獲得更好的可讀性)[未經測試]:
locals {
cidr = var.network_address_space[terraform.workspace]
netnums = range(var.subnet_count[terraform.workspace])
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
....
cidr = local.cidr
public_subnets = [for i in local.netnums : cidrsubnet(local.cidr, 8, i)]
....
}
netnums
將是一個從0
開始的數字數組 .. 請參閱range() terraform function 了解如何從不同的數字開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.