【问题标题】:Terraform project to create VPC/IGW, VPN, and private subnet with NATGW - Connectivity Analyzer says no route from NATGW to IGW使用 NATGW 创建 VPC/IGW、VPN 和私有子网的 Terraform 项目 - 连接分析器说没有从 NATGW 到 IGW 的路由
【发布时间】:2021-06-17 11:03:29
【问题描述】:

https://github.com/phillhocking/aws-network-vpn/tree/1000

我一直试图弄清楚这一点,我真的很难理解为什么这些组件不能相互通信。 VPC 有一个 IGW,这是 NATGW 的 EIP 所必需的,但是,此子网上的任何内容都无法访问公共互联网。在 VPN 链接上一切正常,但连接分析器表明 NATGW 和 IGW 之间没有连接,因为没有路由 - 我如何在没有 0.0.0.0/0 路由的情况下仅将 NATGW 流量路由到 IGW哪个已分配给此子网的 NATGW?

Connectivity Analyzer - Route Table has no route to IGW

我知道我只是错过了一些简单的愚蠢的东西。查看 repo 以了解整个过程是如何结合在一起的,但这里是 VPC 模块:

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

resource "aws_vpc" "main" {
  cidr_block            = var.cidr_block
  enable_dns_hostnames  = true
  enable_dns_support    = true

  tags = {
    Name = var.vpc_name
  }
}

resource "aws_subnet" "dev" {
  count = var.subnet_count
  # This line is necessary to ensure that we pick availabiltiy zones that can launch any size ec2 instance
  availability_zone = data.aws_availability_zones.available.names[0]

  vpc_id            = aws_vpc.main.id
  cidr_block        = cidrsubnet(var.cidr_block, 6, count.index * 2 + 1)


  tags = {
    Name = "dev-subnet-${count.index}"
  }
}

resource "aws_network_acl" "dev" {
  vpc_id     = aws_vpc.main.id
  subnet_ids = aws_subnet.dev[*].id

  ingress {
    protocol   = -1
    rule_no    = 1000
    action     = "allow"
    #cidr_block = var.prem_network_address_space
    cidr_block = "0.0.0.0/0"
    from_port  = 0
    to_port    = 0
  }

  egress {
    protocol   = -1
    rule_no    = 100
    action     = "allow"
    cidr_block = "0.0.0.0/0"
    from_port  = 0
    to_port    = 0
  }

  tags = {
    Name = "dev-acl"
  }
}

# Gateways

resource "aws_internet_gateway" "gw" {
  vpc_id = aws_vpc.main.id

  tags = {
    Name = "${var.vpc_name}-internet-gateway"
  }
}

resource "aws_eip" "nat-gw" {
  vpc = true

  tags = {
    Name = "nat-elastic-ip"
  }

  depends_on = [aws_internet_gateway.gw]
}

resource "aws_nat_gateway" "gw" {
  allocation_id = aws_eip.nat-gw.id
  subnet_id     = aws_subnet.dev[0].id

  tags = {
    Name = "${var.vpc_name}-nat-gateway-dev"
  }

  depends_on = [aws_internet_gateway.gw]
}

# VPC Route Table

resource "aws_default_route_table" "default" {
  default_route_table_id = aws_vpc.main.main_route_table_id

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

  tags = {
    Name = "${var.vpc_name}-public"
  }
  depends_on = [aws_internet_gateway.gw]
}

# dev Subnet Route Table

resource "aws_route_table" "dev" {
  vpc_id = aws_vpc.main.id

  tags = {
    Name = "dev-route-table"
  }
}

resource "aws_route_table_association" "dev_routes" {
  subnet_id      = aws_subnet.dev[0].id
  route_table_id = aws_route_table.dev.id

  depends_on = [aws_nat_gateway.gw]
}

resource "aws_route" "dev_nat" {
  route_table_id            = aws_route_table.dev.id
  destination_cidr_block    = "0.0.0.0/0"
  nat_gateway_id = aws_nat_gateway.gw.id

  depends_on = [aws_nat_gateway.gw]

【问题讨论】:

  • 看起来我犯的错误是我没有一个“公共”子网来启动 nat 网关。如果谁让我知道,那肯定会很棒。

标签: amazon-web-services terraform amazon-vpc terraform-provider-aws


【解决方案1】:

您将 NAT 放置在 aws_subnet.dev[0] 中,然后创建附加到子网的 aws_route_table.dev。更重要的是aws_route_table.dev 有一个路由aws_route.dev_nat 指向NAT。

所以基本上你是在做一些循环路由 - aws_subnet.dev[0] 中的所有流量都直接到同一子网中的 NAT,而后者又被再次定向到同一 NAT。

正如您在评论中指出的那样,NAT 应该在 公共子网 中,而将流量定向到 NAT 的子网应该是私有子网。

【讨论】:

    猜你喜欢
    • 2021-07-29
    • 2021-01-27
    • 2020-03-13
    • 1970-01-01
    • 2021-03-19
    • 2021-06-13
    • 2020-08-02
    • 1970-01-01
    • 2016-07-21
    相关资源
    最近更新 更多