将多个子网分配给路由表aws

时间:2020-09-20 21:02:07

标签: amazon-web-services terraform

我认为有多个人提出了相同的问题,但我的情况有所不同。我也从用户那里获取vpc区域,cidr值,甚至公共子网段的输入。我必须将所有公共子网附加到默认路由表,将私有子网附加到diff路由表。您能帮助我如何附加它们吗?

provider "aws" {
    region = var.region
}

resource "aws_vpc" "app_vpc" {
  cidr_block           = var.vpc_cidr
  enable_dns_support   = true
  enable_dns_hostnames = true
  tags = {
    Name = var.vpc_name
  }
}

# create igw
resource "aws_internet_gateway" "app_igw" {
  vpc_id = aws_vpc.app_vpc.id
}

data "aws_availability_zones" "available" {
  state = "available"
}

#provision public subnet
resource "aws_subnet" "public_subnet_01" {
  vpc_id     = aws_vpc.app_vpc.id
  cidr_block = var.public_subnet_01
  availability_zone = data.aws_availability_zones.available.names[0]
  tags = {
    Name = "public_subnet_01"
  }
  depends_on = [aws_vpc_dhcp_options_association.dns_resolver]
}
resource "aws_subnet" "public_subnet_02" {
  vpc_id     = aws_vpc.app_vpc.id
  cidr_block = var.public_subnet_02
  availability_zone = data.aws_availability_zones.available.names[1]
  tags = {
    Name = "public_subnet_02"
  }
  depends_on = [aws_vpc_dhcp_options_association.dns_resolver]
}
resource "aws_subnet" "public_subnet_03" {
  vpc_id     = aws_vpc.app_vpc.id
  cidr_block = var.public_subnet_03
  availability_zone = data.aws_availability_zones.available.names[2]
  tags = {
    Name = "public_subnet_03"
  }
  depends_on = [aws_vpc_dhcp_options_association.dns_resolver]
}

#default route table 
resource "aws_default_route_table" "default" {
  default_route_table_id = aws_vpc.app_vpc.default_route_table_id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.app_igw.id
  }
}

resource "aws_route_table_association" "default_association_01" {
  subnet_id      = [aws_subnet.public_subnet_01.id, aws_subnet.public_subnet_02.id, aws_subnet.public_subnet_03.id]
  route_table_id = aws_vpc.app_vpc.default_route_table_id
}

添加多个子网时出现错误,请您在这里帮忙:)

1 个答案:

答案 0 :(得分:1)

aws_route_table_association仅将一个子网作为输入,而不是子网列表。

如果要使用列表创建关联,则可以使用for_each

"The request matched multiple endpoints."

以上假设其他所有条件都是正确的。您的代码中可能仍然存在一些尚不明显的错误。