![](/img/trans.png)
[英]How to take the reference of multiple subnets in route_table_association in Terraform?
[英]Terraform AWS - route table association - add multiple subnet
我在我的 vpc 中创建了 4 个 su.net,其中 2 个是公共的,2 个是私有的。 我需要将 2 个公共 su.net 关联到一个路由表,将 2 个私有 su.net 关联到另一个路由表。 查看文档, aws_route_table_association
似乎只接受一个su.net_id
。
如何添加多个su.nets
,如图所示?
resource "aws_route_table_association" "public-test" {
subnet_id = -> I need to add 2 public subnets here
route_table_id = aws_route_table.public-test.id
}
resource "aws_route_table_association" "private-test" {
subnet_id = -> I need to add 2 private subnets here
route_table_id = aws_route_table.private-test.id
}
以下是 su.net 和路由:
resource "aws_subnet" "public-test-a" {
vpc_id = aws_vpc.vpc-test-02.id
cidr_block = "10.0.0.0/28"
map_public_ip_on_launch = true
availability_zone = var.AZ[0]
tags = {
Name = "public-test-a"
}
}
resource "aws_subnet" "public-test-b" {
vpc_id = aws_vpc.vpc-test-02.id
cidr_block = "10.0.0.16/28"
map_public_ip_on_launch = true
availability_zone = var.AZ[1]
tags = {
Name = "public-test-b"
}
}
resource "aws_subnet" "private-test-a" {
vpc_id = aws_vpc.vpc-test-02.id
cidr_block = "10.0.0.32/28"
availability_zone = var.AZ[0]
tags = {
Name = "private-test-a"
}
}
resource "aws_subnet" "private-test-b" {
vpc_id = aws_vpc.vpc-test-02.id
cidr_block = "10.0.0.48/28"
availability_zone = var.AZ[1]
tags = {
Name = "private-test-b"
}
}
resource "aws_route_table" "public-test" {
vpc_id = aws_vpc.vpc-test-02.id
route {
cidr_block = "10.0.0.0/26"
}
route {
cidr_block = "0.0.0.0/0"
gateway_id =aws_internet_gateway.myIG-test-02.id
}
tags = {
Name = "public-test"
}
}
resource "aws_route_table" "private-test" {
vpc_id = aws_vpc.vpc-test-02.id
route {
cidr_block = "10.0.0.0/26"
}
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_nat_gateway.myNat-test-02.id
}
}
您可以简单地声明两个路由表关联资源。
resource "aws_subnet" "public_test_a" {
vpc_id = aws_vpc.vpc-test-02.id
cidr_block = "10.0.0.0/28"
map_public_ip_on_launch = true
availability_zone = var.AZ[0]
tags = {
Name = "public-test-a"
}
}
resource "aws_subnet" "public-test-b" {
vpc_id = aws_vpc.vpc-test-02.id
cidr_block = "10.0.0.16/28"
map_public_ip_on_launch = true
availability_zone = var.AZ[1]
tags = {
Name = "public-test-b"
}
}
resource "aws_route_table_association" "public-test-a" {
subnet_id = aws_subnet.public-test-a.id # first subnet
route_table_id = aws_route_table.public-test.id
}
resource "aws_route_table_association" "public-test-b" {
subnet_id = aws_subnet.public-test-b.id # second subnet
route_table_id = aws_route_table.public-test.id
}
resource "aws_route_table" "public-test" {
vpc_id = aws_vpc.vpc-test-02.id
route {
cidr_block = "10.0.0.0/26"
}
route {
cidr_block = "0.0.0.0/0"
gateway_id =aws_internet_gateway.myIG-test-02.id
}
tags = {
Name = "public-test"
}
}
遵循命名约定也被认为是一种好的做法。 引用文档
在所有资源名称、数据源名称、变量名称、输出中使用 _(下划线)而不是 -(破折号)。 请注意,实际的云资源在其命名约定中有许多隐藏的限制。 有些不能包含破折号,有些必须是驼峰式的。 这些约定指的是 Terraform 名称本身。 仅使用小写字母和数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.