In most real AWS environments, the servers that matter have no public IP. They sit in private subnets with no direct route from the internet, which is exactly what you want for security. It does raise a practical question, though: how do engineers reach them to deploy, debug or check logs?

One common answer is a VPN. In this project I set up an OpenVPN server in one VPC, peer that VPC with a separate production VPC, and then SSH from my laptop into a private EC2 instance using only its private IP. The private server is never exposed to the internet.

What we’re building

There are two VPCs. The VPN VPC (10.0.0.0/16) has a public subnet with an Ubuntu instance running OpenVPN. The production VPC (192.168.0.0/16) has a private subnet with an EC2 instance that has no public IP at all. Once the laptop is connected to the VPN and the two VPCs are peered, the laptop can reach the private server as if it were on the same network.

The two VPCs must use different CIDR ranges. If they overlap, peering won’t work, because AWS can’t tell which network an address belongs to. (There’s more on this in IP addressing fundamentals.)

Setting up the VPN server

The OpenVPN instance goes in the public subnet of the VPN VPC, and gets an Elastic IP. Without one, the public IP changes whenever the instance stops and starts, and every client profile would break.

Its security group needs two inbound rules:

PortPurpose
22SSH access
1194 UDPOpenVPN

OpenVPN uses UDP port 1194 by default. UDP generally performs better for VPN tunnels than TCP, because the tunnel doesn’t need TCP’s retransmission on top of the TCP connections running inside it. Restrict SSH on port 22 to your own IP rather than leaving it open to everyone.

For the installation, I used an automated OpenVPN install script on Ubuntu. It asks a few questions (protocol, port, DNS settings) and then generates the certificates and server configuration for you, which saves a lot of manual work with certificate authorities.

The client profile

At the end, the script generates a client profile, a .ovpn file (mine was madhu.ovpn). It contains the connection details and the certificates the client needs, so treat it like a private key. You can also protect it with a password. I imported it into the OpenVPN Connect app on my laptop and connected, which puts the laptop on the VPN network.

The private server

In the production VPC, I launched a second instance in a private subnet with no public IP. Its security group allows SSH only from the VPN VPC’s range (10.0.0.0/16), not from anywhere on the internet.

Peering the two VPCs

At this point the VPN works, but the laptop still can’t reach the production server, because the two VPCs don’t know about each other. VPC peering connects them privately over AWS’s own network, without traffic going over the public internet.

You create the peering connection from one VPC, accept it in the other, and then, the step most people forget, add routes in both directions:

  • In the VPN VPC’s route table: 192.168.0.0/16 → the peering connection
  • In the production VPC’s route table: 10.0.0.0/16 → the peering connection

Peering without routes does nothing. And peering isn’t transitive: if you later add a third VPC, it needs its own peering connection and routes.

Testing it

With the VPN connected, peering active and routes in place, I could ping the private instance and SSH into it on its private address:

ssh ubuntu@192.168.x.x

That’s the whole point of the setup. The server is reachable for the people who need it and invisible to everyone else.

When it doesn’t work

Almost every connectivity problem here comes down to routing or firewall rules. Check for overlapping CIDR blocks, a missing route on one side of the peering, a security group on the private server that doesn’t allow the VPN VPC’s range, UDP 1194 not being open on the VPN server, or a client profile pointing at an old public IP.

Cleaning up

Terminate both instances when you’re done, and release the Elastic IP. AWS charges for public IPv4 addresses, including Elastic IPs that are allocated but not in use. If you added NAT gateways while experimenting, delete those too, because they charge by the hour.

Watch the build

The video covers the VPC setup, installing OpenVPN, security groups, peering, route tables, the client setup and testing access to the private instance.

Where this pattern is used, and what’s next

Setups like this are used for engineer access to production, remote administration and connecting offices to cloud networks. In larger environments you’ll also see AWS Client VPN (the managed version of this), Site-to-Site VPN for connecting a data centre, Transit Gateway when there are too many VPCs to peer one by one, and bastion hosts or Systems Manager Session Manager as alternatives to a VPN. Once you’ve built this by hand, all of those are much easier to understand. The AWS Zero to Hero series covers the rest of the networking path.

Madhukar Reddy

DevOps engineer focused on AWS, Docker, Kubernetes, cloud infrastructure, and cyber security. Shares practical cloud and DevOps content based on hands-on deployments, infrastructure troubleshooting, and real-world projects.

$ This blog is currently running on AWS EC2 using Docker-based deployment.

Leave a response