Help Center> NAT Gateway> Best Practices> Using NAT Gateway and VPC Peering to Enable Communication Between VPCs and Internet

Using NAT Gateway and VPC Peering to Enable Communication Between VPCs and Internet

Scenarios

VPC A and VPC B are in the same region. A NAT gateway is configured for subnet A in VPC A and you can add SNAT and DNAT rules to enable communication with the Internet. Subnet B connects to the subnet A through a VPC peering connection and uses the NAT gateway of subnet A to communicate with the Internet.

Solution Advantages

Only one NAT gateway needs to be configured. Servers in the two VPCs can communicate with the Internet through the same NAT gateway, saving resources.

Typical Topology

The CIDR block of VPC A is 192.168.0.0/16 and that of subnet A is 192.168.1.0/24. The CIDR block of VPC B is 192.168.0.0/16 and that of subnet B is 192.168.2.0/24.

Implementation methods:

  1. Use a VPC peering connection to connect subnet A in VPC A to subnet B in VPC B.
  2. Create a NAT gateway in VPC A. Then subnet B can use the NAT gateway to communicate the Internet.
    Figure 1 Network topology

Prerequisites

  • If VPCs connected by a VPC peering connection have overlapping CIDR blocks, the connection can only enable communication between specific (non-overlapping) subnets in the VPCs.
  • The subnets of the two VPCs do not overlap with each other.

Procedure

  1. Create VPC A, VPC B, subnet A, and subnet B.

    For details, see Creating a VPC.

  2. Create a VPC peering connection.

    Create a VPC peering connection between subnet A and subnet B. For details, see Creating a VPC Peering Connection with Another VPC in Your Account.

    The local VPC is VPC A, and the peer VPC is VPC B.

    In addition to the local and peer routes, you need to add another peer route with destination set to 0.0.0.0/0.

  3. Buy a public NAT gateway.

    Buy a public network NAT gateway with VPC set to VPC A. For details, see Buying a Public NAT Gateway.
    Figure 2 Buy Public NAT Gateway

  4. Add an SNAT rule.

    1. Select VPC for Scenario and subnet A for Subnet. For details, see Adding an SNAT Rule.
      Figure 3 Add SNAT Rule
    2. Add an SNAT rule for subnet B. Set Scenario to Direct Connect/Cloud Connect and enter the CIDR block of subnet B.
      Figure 4 Add SNAT Rule

  5. Add a DNAT rule.

    1. Add a DNAT rule for subnet A. Set Scenario to VPC and enter the IP address of the server in subnet A for Private IP Address. For details, see Adding a DNAT Rule.
      Figure 5 Add DNAT Rule
    2. Add a DNAT rule for subnet B. Set Scenario to Direct Connect/Cloud Connect and enter the CIDR block of subnet B for Private IP Address.
      Figure 6 Add DNAT Rule

Verification

After the configuration is complete, test the network connectivity.

Log in to a server in subnet B to ping a public IP address.

Log in to a server that does not belong to VPC A and VPC B. Use the curl command to check whether subnet B can communicate with the Internet through the DNAT rule with EIP associated.