如何在aws_route_table中设置nat_gateway_id



main.tf

module "vpc" {
source = "../modules/aws/vpc"
env_prefix  = "prod"
environment = "production"
env_name    = "test"
vpc_cidr    = "10.1.0.0/16"
public_subnet_cidrs = {
test-prod-nat = {
subnet = "10.1.15.0/24"
name   = "test-prod-nat"
az     = "ap-northeast-1a"
}
}
}

nat.tf

resource "aws_nat_gateway" "private" {
for_each = var.public_subnet_cidrs
allocation_id = aws_eip.nat_gateway.id
subnet_id     = aws_subnet.public[each.key].id
tags = merge(
local.tags,
{
Name = format("%s_%s_%s", var.env_prefix, var.env_name, "nat-gateway")
}
)
lifecycle {
prevent_destroy = false
}
}

路线_表格.tf

/**
* for private subnet
*/
resource "aws_route_table" "private" {
vpc_id = aws_vpc.dandori.id
tags = merge(
local.tags,
{
Name = format("%s_%s", var.env_prefix, var.env_name)
}
)
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = [
for v in aws_nat_gateway.private : v.id
]
}

lifecycle {
prevent_destroy = false
}
}

当我在创建上述tf文件后运行地形计划时,我得到以下错误

【错误】

╷
│ Error: Incorrect attribute value type
│ 
│   on ../modules/aws/vpc/route_table.tf line 55, in resource "aws_route_table" "private":
│   55:     nat_gateway_id = [
│   56:       for v in aws_nat_gateway.private : v.id
│   57:     ]
│     ├────────────────
│     │ aws_nat_gateway.private is object with 1 attribute "test-prod-nat"
│ 
│ Inappropriate value for attribute "nat_gateway_id": string required.

route_table.tf和nat.tf将是模块中的文件我试图使用for循环方法在route_table.tf中设置nat_gateway_id,但我无法正确设置它,如错误消息中所示。

我该怎么办才能解决这个问题?请给我一些建议。

如果您想为每个aws_nat_gateway.private创建一个路由表,那么它应该是:

resource "aws_route_table" "private" {

for_each = aws_nat_gateway.private

vpc_id = aws_vpc.dandori.id
tags = merge(
local.tags,
{
Name = format("%s_%s", var.env_prefix, var.env_name)
}
)
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = each.value["id"]
}

lifecycle {
prevent_destroy = false
}
}

相关内容

  • 没有找到相关文章

最新更新