代码之家  ›  专栏  ›  技术社区  ›  sdgfsdh

无法通过VPC对等连接到另一个VPC中的RDS

  •  1
  • sdgfsdh  · 技术社区  · 4 年前

    我有两个VPC:

    • VPC A
      • RDS实例
    • VPC B
      • EC2实例

    也有一些子网:

    • VPC A
      • 私人A
      • 私人B
      • 同行A
    • VPC B
      • 私人A
      • 私人B
      • 同行A

    RDS位于VPC A的私有A、私有B、对等A中。

    EC2位于VPC B的对等端A。

    我想从EC2连接到RDS实例。

    我创建了一个对等:

    resource "aws_vpc_peering_connection" "a_to_b" {
      vpc_id      = aws_vpc.a.id
      peer_vpc_id = aws_vpc.b.id
      auto_accept = true
    
      accepter {
        allow_remote_vpc_dns_resolution = true
      }
    
      requester {
        allow_remote_vpc_dns_resolution = true
      }
    }
    
    resource "aws_vpc_peering_connection_accepter" "a_to_b" {
      vpc_peering_connection_id = aws_vpc_peering_connection.a_to_b.id
      auto_accept               = true
    }
    

    我还有整个CIDR块的路由表,如下所示:

    resource "aws_route_table" "a_peer" {
      vpc_id = aws_vpc.a.id
    }
    
    resource "aws_route_table_association" "a_peer" {
      route_table_id = aws_route_table.a_peer.id
      subnet_id      = aws_subnet.a_peer.id
    }
    
    resource "aws_route" "a_peer_b" {
      route_table_id            = aws_route_table.a_peer.id
      destination_cidr_block    = aws_subnet.b_peer.cidr_block
      vpc_peering_connection_id = aws_vpc_peering_connection.a_to_b.id
    }
    
    resource "aws_route_table" "b_peer" {
      vpc_id = aws_vpc.b.id
    }
    
    resource "aws_route_table_association" "b_peer" {
      route_table_id = aws_route_table.b_peer.id
      subnet_id      = aws_subnet.b_peer.id
    }
    
    resource "aws_route" "b_peer_a" {
      route_table_id            = aws_route_table.b_peer.id
      destination_cidr_block    = aws_subnet.a_peer.cidr_block
      vpc_peering_connection_id = aws_vpc_peering_connection.a_to_b.id
    }
    

    我还从以下位置创建了安全组 ingress egress 将RDS实例上的数据发送到EC2安全组。

    当我SSH到EC2时,我可以获得DNS:

    $ nslookup rds.xxxxxxxxxxx.eu-west-2.rds.amazonaws.com
    Server:     192.16.0.2
    Address:    192.16.0.2#53
    
    Non-authoritative answer:
    Name:   rds.xxxxxxxxxxx.eu-west-2.rds.amazonaws.com
    Address: 10.16.192.135
    

    然而, curl 无法连接:

    $ curl rds.xxxxxxxxxxx.eu-west-2.rds.amazonaws.com:5432
    

    预期的反应是:

    $ curl rds.xxxxxxxxxxx.eu-west-2.rds.amazonaws.com:5432
    curl: (52) Empty reply from server
    

    VPC对等连接处于“活动”状态,路由表与Terraform匹配。

    我怎样才能连接到这个?

    0 回复  |  直到 4 年前
        1
  •  4
  •   Marcin    4 年前

    我自己做了一些测试,我很确定这个问题是由你的 路线 ,假设VPC中的其他所有内容都是正确的,因为未显示VPC和子网定义。

    具体来说,您写道“RDS位于VPC A的私有A、私有B、对等A中”。这意味着RDS主机可能处于 这些子网中的任何一个子网 。您无法控制它,因为由RDS选择使用哪个子网。创建RDS时,您只能通过选择AZ来部分控制它。随后,对等路由表应覆盖所有这三个子网。实现这一目标的最简单方法是使用 VPC CIDR 范围:

    # Route from instance in VPC B to any subnet in VPC A which
    # hosts your RDS in all its subnets
    resource "aws_route" "b_peer_a" {
      route_table_id            = aws_route_table.b_peer.id
      destination_cidr_block    = aws_vpc.a.cidr_block
      vpc_peering_connection_id = aws_vpc_peering_connection.a_to_b.id
    }
    

    然后你还需要有一个 VPC A中的路由表 与所有子网的对等连接相关联:

    resource "aws_route_table" "a_peer" {
      vpc_id = aws_vpc.a.id
    }
    
    resource "aws_route_table_association" "a_peer" {
      route_table_id = aws_route_table.a_peer.id
      subnet_id      = aws_subnet.a_peer.id
    }
    
    resource "aws_route_table_association" "a_private1" {
      route_table_id = aws_route_table.a_peer.id
      subnet_id      = aws_subnet.a_private1.id
    }
    
    resource "aws_route_table_association" "a_private2" {
      route_table_id = aws_route_table.a_peer.id
      subnet_id      = aws_subnet.a_private2.id
    }
    
    resource "aws_route" "a_peer_b" {
      route_table_id            = aws_route_table.a_peer.id
      destination_cidr_block    = aws_subnet.b_peer.cidr_block
      vpc_peering_connection_id = aws_vpc_peering_connection.a_to_b.id
    }