
VPC Peering on AWS Explained: Connect Two VPCs Privately with the Console and CLI
Learn how VPC peering works and connect two VPCs privately on AWS, step by step with the Console and CLI
What is VPC peering?
A VPC is an isolated network, and by default two VPCs cannot talk to each other. VPC peering is a networking connection between two VPCs that lets them route traffic between each other using private IPv4 or IPv6 addresses, as if they were on the same network. The traffic stays on the AWS global network and never crosses the public internet.
You can peer VPCs in the same account or in different accounts, and in the same Region or in different Regions. Peering is not a gateway or a VPN, and it is not a separate piece of hardware. It is a direct, one-to-one link, which means there is no single point of failure and no bandwidth bottleneck created by the connection itself.
Common use cases:
- Letting an application VPC reach a shared services VPC (for example, a central database, logging, or Active Directory)
- Connecting VPCs owned by different teams or business units
- Giving a partner or customer account private access to specific resources
- Connecting a production VPC to a separate management or monitoring VPC
Key rules to know before you start
- CIDR blocks cannot overlap. If one VPC uses
10.0.0.0/16, the other cannot use10.0.0.0/16or any range that overlaps it. You cannot fix this after the VPCs are created, so plan your IP ranges early.
- Peering is not transitive. If VPC A is peered with VPC B, and VPC B is peered with VPC C, then A cannot reach C through B. You would need a direct A-to-C peering connection.
- No edge-to-edge routing. A peer VPC cannot use your internet gateway, NAT gateway, VPN connection, or Direct Connect connection to reach other networks.
- Routes are not automatic. Creating a peering connection does not change any route table. You must add a route in each VPC.
- Security rules still apply. Security groups and network ACLs must allow the traffic in both VPCs.
- One connection per pair. You cannot have more than one active peering connection between the same two VPCs.
How a peering connection works
A peering connection has two sides:
- Requester: the VPC that sends the peering request.
- Accepter: the VPC that receives the request and must accept it.
The connection moves through these states:
- initiating-request: AWS is creating the request.
- pending-acceptance: the request is waiting for the accepter to approve it. If nobody accepts it within 7 days, it expires.
- provisioning: AWS is setting up the connection.
- active: the connection is working and traffic can flow once routes and security rules allow it.
- rejected, expired, failed, or deleted: the connection is not usable.
When both VPCs are in the same account and Region, you can create and accept the request yourself in the same session.
VPC peering vs other connectivity options
- VPC peering: best for connecting a small number of VPCs with simple, low-cost, private connectivity. No hourly charge for the connection itself.
- AWS Transit Gateway: best when you have many VPCs or need transitive routing. It acts as a central hub, so you connect each VPC once instead of building a mesh of peering connections. It has hourly and per-GB charges.
- AWS PrivateLink: best when you want to expose one service (not a whole network) to other VPCs or accounts. It works even when the CIDR blocks overlap.
- Site-to-Site VPN or Direct Connect: best for connecting AWS to on-premises networks.
A rule of thumb: for two or three VPCs, use peering. As the number of VPCs grows, the number of connections needed for a full mesh grows quickly (n VPCs need n x (n - 1) / 2 connections), and Transit Gateway becomes easier to manage.
What you will build
- VPC A (
demo-a) with CIDR10.0.0.0/16and one public subnet (10.0.1.0/24)
- VPC B (
demo-b) with CIDR10.1.0.0/16and one private subnet (10.1.1.0/24)
- A peering connection between the two VPCs
- Route table entries in both VPCs that send traffic for the other VPC through the peering connection
- Security groups that allow SSH to the instance in VPC A and ping (ICMP) from VPC A to the instance in VPC B
- A test that connects to the instance in VPC A and pings the private instance in VPC B
The instance in VPC B has no public IP address and no internet route, so the only way to reach it is through the peering connection. That makes the test a clear proof that peering works.
Prerequisites
- An AWS account and permissions to manage VPC and EC2 resources
- AWS CLI v2 configured with
aws configure(for the CLI section)
- A Region of your choice (the examples use
us-east-1)
- An SSH client on your computer (for the CLI test)
Cost warning: the peering connection itself is free, but the two EC2 instances and the public IPv4 address on the instance in VPC A are billed while they exist. Delete everything when you finish (see the cleanup step).
Implementation with the AWS Management Console
Step 1: Create VPC A (public)
- Open the VPC console and choose Create VPC.
- Select VPC and more.
- Set Name tag auto-generation to
demo-a.
- Set IPv4 CIDR block to
10.0.0.0/16.
- Set Number of Availability Zones to
1.
- Set Number of public subnets to
1and Number of private subnets to0.
- Set NAT gateways to
Noneand VPC endpoints toNone.
- Under DNS options, enable both DNS hostnames and DNS resolution.
- Choose Create VPC.
Step 2: Create VPC B (private)
- Choose Create VPC again and select VPC and more.
- Set Name tag auto-generation to
demo-b.
- Set IPv4 CIDR block to
10.1.0.0/16.
- Set Number of Availability Zones to
1.
- Set Number of public subnets to
0and Number of private subnets to1.
- Set NAT gateways to
Noneand VPC endpoints toNone.
- Enable both DNS hostnames and DNS resolution, then choose Create VPC.
The CIDR blocks
10.0.0.0/16 and 10.1.0.0/16 do not overlap, so they can be peered.Step 3: Create the peering connection
- In the VPC console, go to Peering connections and choose Create peering connection.
- Set Name to
demo-a-to-b.
- Under Select a local VPC to peer with, choose
demo-a-vpcas the VPC ID (Requester).
- Under Select another VPC to peer with, set Account to My account and Region to This Region.
- Choose
demo-b-vpcas the VPC ID (Accepter).
- Choose Create peering connection. The status is now Pending acceptance.
Step 4: Accept the peering request
- Select the peering connection.
- Choose Actions, then Accept request.
- Confirm with Accept request. The status changes to Active.
Step 5: Add routes in VPC A
- Go to Route tables and select the public route table of
demo-a(for exampledemo-a-rtb-public).
- On the Routes tab, choose Edit routes, then Add route.
- Destination
10.1.0.0/16, target Peering Connection, and selectdemo-a-to-b.
- Choose Save changes.
Step 6: Add routes in VPC B
- Select the private route table of
demo-b(for exampledemo-b-rtb-private1-us-east-1a).
- Choose Edit routes, then Add route.
- Destination
10.0.0.0/16, target Peering Connection, and selectdemo-a-to-b.
- Choose Save changes.
The route is required on both sides. Without the return route in VPC B, the replies cannot find their way back and the connection appears to hang.
Step 7: Create the security groups
- Go to Security groups and choose Create security group.
- Name
demo-a-sg, VPCdemo-a-vpc. Add an inbound rule: type SSH, source My IP. Create it.
- Create a second group named
demo-b-sgindemo-b-vpc. Add an inbound rule: type All ICMP - IPv4, source10.0.0.0/16. Create it.
Security groups are stateful, so the ping replies are allowed automatically. For tighter control in the same Region, you can use the security group of the peer VPC as the source instead of a CIDR range.
Step 8: Launch the test instances
- Open the EC2 console and choose Launch instance.
- Instance A: name
demo-a-instance, Amazon Linux 2023, instance typet3.micro. In Network settings, choose Edit, selectdemo-a-vpcand the public subnet, set Auto-assign public IP to Enable, and select the security groupdemo-a-sg. Launch it. You can proceed without a key pair because you will connect with EC2 Instance Connect.
- Launch instance B: name
demo-b-instance, the same AMI and instance type. In Network settings, selectdemo-b-vpcand the private subnet, set Auto-assign public IP to Disable, and select the security groupdemo-b-sg.
- Wait until both instances show Running and 2/2 checks passed.
- Open
demo-b-instanceand copy its Private IPv4 address (it will look like10.1.1.x).
Step 9: Test the connection
- Select
demo-a-instance, choose Connect, and use the EC2 Instance Connect tab to open a terminal in your browser.
- Run the ping command, replacing the address with the private IP of instance B:
ping -c 4 10.1.1.25- You should see replies with low latency. This confirms traffic is flowing through the peering connection.
Step 10: Enable DNS resolution across the peering (optional)
By default, a public DNS hostname of an instance in the peer VPC resolves to its public IP address. To make it resolve to the private IP address across the peering connection:
- Select the peering connection and choose Actions, then Edit DNS settings.
- Enable Requester DNS resolution and Accepter DNS resolution.
- Choose Save changes.
Both VPCs must have DNS hostnames and DNS resolution enabled, which Steps 1 and 2 already did.
Step 11: Clean up
- In the EC2 console, select both instances and choose Instance state, then Terminate instance.
- In the VPC console, go to Peering connections, select
demo-a-to-b, and choose Actions, then Delete peering connection. Check the option to delete the related route table entries.
- Go to Security groups and delete
demo-a-sganddemo-b-sg.
- Go to Your VPCs, select
demo-a-vpc, and choose Actions, then Delete VPC. The console also deletes the subnets, route tables, and internet gateway. Repeat fordemo-b-vpc.
Implementation with the AWS CLI
All VPC and EC2 commands live under
aws ec2 . Set the variables first:export AWS_REGION=us-east-1export AZ=us-east-1aexport VPC_A_CIDR=10.0.0.0/16export VPC_B_CIDR=10.1.0.0/16Step 1: Create VPC A with a public subnet
VPC_A=$(aws ec2 create-vpc \ --cidr-block $VPC_A_CIDR \ --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=demo-a}]' \ --query Vpc.VpcId --output text)
aws ec2 modify-vpc-attribute --vpc-id $VPC_A --enable-dns-hostnames '{"Value":true}'
SUBNET_A=$(aws ec2 create-subnet \ --vpc-id $VPC_A \ --cidr-block 10.0.1.0/24 \ --availability-zone $AZ \ --tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=demo-a-public}]' \ --query Subnet.SubnetId --output text)
aws ec2 modify-subnet-attribute --subnet-id $SUBNET_A --map-public-ip-on-launch
IGW_A=$(aws ec2 create-internet-gateway \ --tag-specifications 'ResourceType=internet-gateway,Tags=[{Key=Name,Value=demo-a-igw}]' \ --query InternetGateway.InternetGatewayId --output text)
aws ec2 attach-internet-gateway --internet-gateway-id $IGW_A --vpc-id $VPC_A
RT_A=$(aws ec2 create-route-table \ --vpc-id $VPC_A \ --tag-specifications 'ResourceType=route-table,Tags=[{Key=Name,Value=demo-a-public-rt}]' \ --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id $RT_A --destination-cidr-block 0.0.0.0/0 --gateway-id $IGW_Aaws ec2 associate-route-table --route-table-id $RT_A --subnet-id $SUBNET_AStep 2: Create VPC B with a private subnet
VPC_B=$(aws ec2 create-vpc \ --cidr-block $VPC_B_CIDR \ --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=demo-b}]' \ --query Vpc.VpcId --output text)
aws ec2 modify-vpc-attribute --vpc-id $VPC_B --enable-dns-hostnames '{"Value":true}'
SUBNET_B=$(aws ec2 create-subnet \ --vpc-id $VPC_B \ --cidr-block 10.1.1.0/24 \ --availability-zone $AZ \ --tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=demo-b-private}]' \ --query Subnet.SubnetId --output text)
RT_B=$(aws ec2 describe-route-tables \ --filters Name=vpc-id,Values=$VPC_B Name=association.main,Values=true \ --query 'RouteTables[0].RouteTableId' --output text)VPC B uses its main route table, which has no internet route, so the subnet is private. New subnets use the main route table automatically.
Step 3: Create the peering connection
PCX_ID=$(aws ec2 create-vpc-peering-connection \ --vpc-id $VPC_A \ --peer-vpc-id $VPC_B \ --tag-specifications 'ResourceType=vpc-peering-connection,Tags=[{Key=Name,Value=demo-a-to-b}]' \ --query VpcPeeringConnection.VpcPeeringConnectionId --output text)
echo $PCX_IDStep 4: Accept the peering request
aws ec2 accept-vpc-peering-connection --vpc-peering-connection-id $PCX_ID
aws ec2 describe-vpc-peering-connections \ --vpc-peering-connection-ids $PCX_ID \ --query 'VpcPeeringConnections[0].Status.Code' --output textThe status should be
active . If it shows provisioning , run the second command again after a few seconds.Step 5: Add routes on both sides
aws ec2 create-route \ --route-table-id $RT_A \ --destination-cidr-block $VPC_B_CIDR \ --vpc-peering-connection-id $PCX_ID
aws ec2 create-route \ --route-table-id $RT_B \ --destination-cidr-block $VPC_A_CIDR \ --vpc-peering-connection-id $PCX_IDStep 6: Create the security groups
MY_IP=$(curl -s https://checkip.amazonaws.com)
SG_A=$(aws ec2 create-security-group \ --group-name demo-a-sg \ --description "SSH from my IP" \ --vpc-id $VPC_A \ --query GroupId --output text)
aws ec2 authorize-security-group-ingress \ --group-id $SG_A --protocol tcp --port 22 --cidr $MY_IP/32
SG_B=$(aws ec2 create-security-group \ --group-name demo-b-sg \ --description "ICMP from VPC A" \ --vpc-id $VPC_B \ --query GroupId --output text)
aws ec2 authorize-security-group-ingress \ --group-id $SG_B --protocol icmp --port -1 --cidr $VPC_A_CIDRThe
--port -1 value means all ICMP types, which includes ping.Step 7: Launch the test instances
Look up the latest Amazon Linux 2023 AMI:
AMI_ID=$(aws ssm get-parameter \ --name /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 \ --query Parameter.Value --output text)Launch instance A in the public subnet of VPC A:
INSTANCE_A=$(aws ec2 run-instances \ --image-id $AMI_ID \ --instance-type t3.micro \ --subnet-id $SUBNET_A \ --security-group-ids $SG_A \ --associate-public-ip-address \ --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=demo-a-instance}]' \ --query 'Instances[0].InstanceId' --output text)Launch instance B in the private subnet of VPC B, with no public IP:
INSTANCE_B=$(aws ec2 run-instances \ --image-id $AMI_ID \ --instance-type t3.micro \ --subnet-id $SUBNET_B \ --security-group-ids $SG_B \ --no-associate-public-ip-address \ --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=demo-b-instance}]' \ --query 'Instances[0].InstanceId' --output text)Wait until both are running and get the private IP of instance B:
aws ec2 wait instance-running --instance-ids $INSTANCE_A $INSTANCE_B
B_PRIVATE_IP=$(aws ec2 describe-instances \ --instance-ids $INSTANCE_B \ --query 'Reservations[0].Instances[0].PrivateIpAddress' --output text)
echo $B_PRIVATE_IPStep 8: Test the connection
Connect to instance A with EC2 Instance Connect (requires an OpenSSH client on your computer). Wait about a minute after the instance starts if the first attempt fails:
aws ec2-instance-connect ssh --instance-id $INSTANCE_A --os-user ec2-userInside the session, ping instance B. Use the value printed by
echo $B_PRIVATE_IP :ping -c 4 10.1.1.25You should see four replies. Type
exit to leave the session.Step 9: Enable DNS resolution across the peering (optional)
aws ec2 modify-vpc-peering-connection-options \ --vpc-peering-connection-id $PCX_ID \ --requester-peering-connection-options AllowDnsResolutionFromRemoteVpc=true \ --accepter-peering-connection-options AllowDnsResolutionFromRemoteVpc=trueStep 10: Peering across accounts or Regions (reference)
The steps are almost the same, with a few extra parameters.
Request a peering connection to a VPC in another account (and optionally another Region):
aws ec2 create-vpc-peering-connection \ --vpc-id $VPC_A \ --peer-vpc-id vpc-0123456789abcdef0 \ --peer-owner-id 111122223333 \ --peer-region us-west-2The owner of the other account must accept the request in the accepter Region:
aws ec2 accept-vpc-peering-connection \ --vpc-peering-connection-id pcx-0123456789abcdef0 \ --region us-west-2Both sides still need routes and security group rules. For inter-Region peering, you cannot reference a peer security group, so use CIDR ranges in the rules.
Step 11: Clean up
Delete in this order, because AWS blocks deleting a resource that others depend on:
aws ec2 terminate-instances --instance-ids $INSTANCE_A $INSTANCE_Baws ec2 wait instance-terminated --instance-ids $INSTANCE_A $INSTANCE_B
aws ec2 delete-vpc-peering-connection --vpc-peering-connection-id $PCX_ID
aws ec2 delete-security-group --group-id $SG_Aaws ec2 delete-security-group --group-id $SG_B
aws ec2 delete-subnet --subnet-id $SUBNET_Aaws ec2 delete-subnet --subnet-id $SUBNET_B
aws ec2 delete-route-table --route-table-id $RT_A
aws ec2 detach-internet-gateway --internet-gateway-id $IGW_A --vpc-id $VPC_Aaws ec2 delete-internet-gateway --internet-gateway-id $IGW_A
aws ec2 delete-vpc --vpc-id $VPC_Aaws ec2 delete-vpc --vpc-id $VPC_BThe main route table of VPC B is deleted automatically with the VPC.
Console or CLI: which should you use?
- Console: best for learning. The "VPC and more" wizard builds each VPC quickly, and the peering page shows the request status, the accept action, and the DNS options in one place.
- CLI: best for repeatable setup and scripting. You must create each piece, add the routes on both sides, and wire the IDs together yourself.
For production, define your VPCs, peering connections, and routes in AWS CloudFormation, AWS CDK, or Terraform so every environment is identical and changes are reviewed in version control.
Best practices
- Plan CIDR ranges before you create VPCs. Give every VPC in your organization a unique, non-overlapping range, because overlapping ranges can never be peered.
- Use the smallest routes you need. Route only the peer's CIDR (or even just a specific subnet) instead of broad ranges, to limit what the peering exposes.
- Apply least privilege in security groups. Allow only the ports your application needs, and in the same Region reference the peer's security group as the source instead of a wide CIDR block.
- Remember the return route. Always add routes in both VPCs, in every route table used by the subnets that need to communicate.
- Do not rely on transitive routing. If you need many VPCs to talk to each other, use AWS Transit Gateway instead of building a large mesh of peering connections.
- Use IAM to control who can create and accept peering connections. For cross-account peering, restrict permissions such as
ec2:CreateVpcPeeringConnectionandec2:AcceptVpcPeeringConnection.
- Enable DNS resolution over the peering if applications connect by hostname, so names resolve to private addresses.
- Turn on VPC Flow Logs. They show which traffic is crossing the peering and help you find rejected traffic.
- Watch data transfer costs. The connection is free, but data transfer across Availability Zones and across Regions is charged, while traffic within the same Availability Zone is free.
- Tag peering connections clearly. Use names such as
prod-to-shared-servicesso the purpose is obvious months later.
- Clean up unused connections. Delete peering connections you no longer need, and remove their routes so you do not leave blackhole entries behind.
Troubleshooting
- The peering connection is stuck in
pending-acceptance: the accepter has not approved it. Accept it from the accepter account or Region. A request that is not accepted within 7 days expires.
- Creating the peering fails because of overlapping CIDR blocks: the two VPCs share part of an IP range. You must use VPCs with non-overlapping ranges, or use AWS PrivateLink instead.
- Ping or connections time out even though the peering is active: check the route tables in both VPCs. Each route table used by the relevant subnets needs a route to the other VPC's CIDR through the peering connection.
- A route shows the status
blackhole: the peering connection was deleted or is no longer active. Delete the route or recreate the connection.
- Connections are refused or time out and the routes are correct: check the security groups on the target instance and the network ACLs on both subnets. ICMP, TCP, and UDP are separate rules.
- You cannot reach the internet or an on-premises network through the peer VPC: this is expected. Peering does not support edge-to-edge routing through the peer's internet gateway, NAT gateway, VPN, or Direct Connect.
- VPC A can reach VPC B, and VPC B can reach VPC C, but A cannot reach C: peering is not transitive. Create a direct peering connection between A and C, or use Transit Gateway.
- Hostnames of the peer instances resolve to public IPs: enable DNS resolution on the peering connection and make sure DNS hostnames and DNS resolution are enabled on both VPCs.
-
aws ec2-instance-connect sshfails: check that instance A has a public IP, that its subnet routes to an internet gateway, and that its security group allows SSH from your current IP address. Your IP may have changed since you created the rule.
- Cannot delete a VPC: a dependency still exists. Remove instances, the peering connection, endpoints, network interfaces, subnets, and the internet gateway first.
-
AccessDeniedorUnauthorizedOperationwhen creating or accepting the peering: your IAM user or role is missing the required EC2 permissions, or you are running the accept command in the wrong account or Region.
Conclusion
VPC peering is the simplest way to connect two VPCs privately on AWS. The workflow is always the same: create the request, accept it, add routes in both VPCs, and allow the traffic in your security groups. Remember the three rules that cause most problems: CIDR blocks cannot overlap, peering is not transitive, and routes are never added automatically. Start with two VPCs and a simple ping test, then apply the same pattern across accounts and Regions, and move to AWS Transit Gateway when the number of VPCs grows.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article