DEV Community

Sherifdeen Adebayo
Sherifdeen Adebayo

Posted on

Building Your Own VPC on Linux: A DevOps Love Story ๐Ÿ’˜๐Ÿง

Or: How I Learned to Stop Worrying and Love Network Namespaces

Introduction: The Plot Twist Nobody Saw Coming ๐ŸŽฌ

Warning: No AWS bills were harmed in the making of this project! ๐Ÿ’ธ

Hey! I'm Sherifdeen Adebayo, and buckle up because I'm about to tell you how I accidentally became best friends with Linux networking (didn't see THAT coming in 2025! ๐Ÿ˜…).

So there I was, staring at the HNG13 Stage 4 DevOps challenge like it was jollof rice without chicken ๐Ÿ˜ญ. The task? Build a complete Virtual Private Cloud system on Linux. My networking knowledge at the time? "IP address goes brrr" ๐Ÿคทโ€โ™‚๏ธ

Spoiler alert: It's not magic. It's just clever use of Linux tools that have been chilling since before TikTok was a thing! (Yes, I'm old enough to remember when we had to ping people to know if they were online ๐Ÿ‘ด)

What I Built (While Consuming Unhealthy Amounts of Coffee โ˜•๏ธ)

I created vpcctl - basically AWS VPC's younger, cooler cousin who lives in Lagos and knows all the shortcuts:

  • Create isolated virtual networks faster than you can say "subnet mask" ๐ŸŽฏ
  • Provision public and private subnets (like VIP and regular sections at Detty December ๐ŸŽ‰)
  • Deploy applications without breaking a sweat ๐Ÿ’ช
  • Control connectivity like a network traffic warden ๐Ÿšฆ
  • Apply firewall rules (because we don't trust anybody, not even ourselves ๐Ÿ”)

Basically, I rebuilt a mini AWS VPC from scratch. And honestly? It was like learning to ride a bike - painful at first, then suddenly you're doing wheelies! ๐Ÿšดโ€โ™‚๏ธ

Shopping List: What You'll Need ๐Ÿ›’

Before we dive into this beautiful mess, grab these:

  • A Linux machine (Ubuntu 20.04+ - or as I call it, "The Reliable Uncle") ๐Ÿ’ป
  • Root/sudo access (because we're about to do DANGEROUS things... safely ๐Ÿ˜Ž)
  • Basic networking knowledge (if you know what an IP address is, you're 80% there!)
  • Command line skills (copy-paste counts as skills, right? ๐Ÿ‘€)
  • Patience (LOTS of it - this took me more coffee breaks than I care to admit โ˜•๏ธโ˜•๏ธโ˜•๏ธ)

The Secret Sauce: Linux Networking Unveiled ๐ŸŽฉโœจ

Like suya and yaji, these concepts are better together!

Network Namespaces: The Private Apartments ๐Ÿ 

Network namespaces are like giving each subnet its own apartment - complete with:

  • Its own network interfaces (like having your own Wi-Fi ๐Ÿ“ก)
  • Personal IP addresses (no sharing with siblings!)
  • Private routing tables (your business is YOUR business ๐Ÿคซ)
  • Custom firewall rules (because boundaries matter!)

It's like having multiple computers on one machine, but without the electricity bill! ๐Ÿ’ก

veth Pairs: The Virtual Cables ๐Ÿ”Œ

Think of veth pairs as virtual LAN cables, but cooler:

  • They come in pairs (like AirPods, but they never get lost!)
  • What goes in one end, comes out the other (magic? Nope, just Linux! โœจ)
  • Perfect for connecting namespaces to bridges
  • No tangling required (looking at you, earphones! ๐Ÿ˜ค)

Pro tip: These are like WhatsApp groups - messages go in, chaos comes out! ๐Ÿ“ฑ

Linux Bridges: The Virtual Switch ๐ŸŽ›๏ธ

Bridges are basically the Dangote of networking - they connect EVERYTHING:

  • Act as virtual switches (think of them as traffic controllers ๐Ÿšฅ)
  • Forward packets between interfaces (like a very efficient delivery service ๐Ÿ“ฆ)
  • In our VPC, this bad boy is the central router
  • More reliable than NEPA (okay, that's not saying much ๐Ÿ˜…)

NAT: The Internet Passport ๐Ÿ›‚

Network Address Translation is like having a bouncer who lets your private IPs into the internet club:

  • Translates private IPs to public IPs
  • Keeps your internal network safe (like a digital bodyguard ๐Ÿ’ช)
  • Makes the internet think everything is coming from one place
  • Basically the VPN your parents wish they understood ๐Ÿ˜‚

The Blueprint: What We're Actually Building ๐Ÿ“

Host System (aka "The Mothership" ๐Ÿš€)
โ”œโ”€โ”€ VPC (10.0.0.0/16) - The whole village ๐Ÿ˜๏ธ
โ”‚   โ”œโ”€โ”€ Bridge (br-my-vpc) - The town square ๐Ÿ›๏ธ
โ”‚   โ”œโ”€โ”€ Public Subnet (10.0.1.0/24) - The market ๐Ÿช
โ”‚   โ”‚   โ”œโ”€โ”€ Namespace (ns-my-vpc-public) - Individual shops
โ”‚   โ”‚   โ”œโ”€โ”€ NAT Gateway (iptables) - The security guard ๐Ÿ‘ฎ
โ”‚   โ”‚   โ””โ”€โ”€ Application - The goods ๐Ÿ“ฆ
โ”‚   โ””โ”€โ”€ Private Subnet (10.0.2.0/24) - The warehouse ๐Ÿญ
โ”‚       โ”œโ”€โ”€ Namespace (ns-my-vpc-private) - Storage rooms
โ”‚       โ””โ”€โ”€ Application - The secret stash ๐Ÿค
โ””โ”€โ”€ Internet Connection - The outside world ๐ŸŒ
Enter fullscreen mode Exit fullscreen mode

Let's Get Our Hands Dirty: Building This Beast ๐Ÿ’ช

Fun fact: This section was written at 2 AM with questionable life choices being made!

Step 1: Project Setup (The Boring Part We Must Endure) ๐Ÿ˜ด

First, let's create our project home:

mkdir vpc-control  # AKA "The Beginning of Something Beautiful"
cd vpc-control
mkdir -p lib policies tests logs  # Creating our empire, one folder at a time
Enter fullscreen mode Exit fullscreen mode

Step 2: The Brain - Core CLI Tool ๐Ÿง 

Create the main vpcctl script. This is our Jarvis, our Friday, our... you get it:

#!/usr/bin/env python3
"""
vpcctl - Virtual Private Cloud Control CLI
AKA "The Magic Wand of Networking" ๐Ÿช„
"""
import argparse
import sys
import os

# This bad boy handles:
# - create-vpc (Birth of a network ๐Ÿฃ)
# - create-subnet (Subdivision time! ๐Ÿ˜๏ธ)
# - deploy-app (App goes brrr ๐Ÿš€)
# - peer-vpcs (Making friends across borders ๐Ÿค)
# - apply-policy (Because we need rules, apparently ๐Ÿ“œ)
# And more cool stuff that'll make your DevOps heart sing! ๐ŸŽต
Enter fullscreen mode Exit fullscreen mode

The full implementation includes these MVPs:

  • vpc_manager.py - The VPC whisperer ๐Ÿ—ฃ๏ธ
  • subnet_manager.py - Subnet sensei ๐Ÿฅ‹
  • nat_manager.py - Internet access enabler (the real MVP) ๐ŸŒ
  • peering_manager.py - The matchmaker ๐Ÿ’‘
  • firewall_manager.py - The bouncer ๐Ÿšช

Step 3: Birth of a VPC (It's Alive! โšก๏ธ)

Here's what happens when you unleash the beast:

sudo ./vpcctl create-vpc --name my-vpc --cidr 10.0.0.0/16
# Translation: "Let there be network!" ๐ŸŒŸ
Enter fullscreen mode Exit fullscreen mode

Under the hood (this is where the magic happens โœจ):

  1. Creates a Linux bridge: ip link add br-my-vpc type bridge
    • Like building a roundabout in Lagos (but this one actually works!)
  2. Brings it up: ip link set br-my-vpc up
    • "Hello world!" but for networks
  3. Stores config in state file
    • Because elephants aren't the only ones who remember

The bridge is basically your VPC's brain ๐Ÿง . Treat it well!

Step 4: Subnet Mania (Where Things Get Spicy ๐ŸŒถ๏ธ)

Subnets = Network namespaces = Mini isolated networks. Mind = Blown ๐Ÿคฏ

sudo ./vpcctl create-subnet --vpc my-vpc --name public --cidr 10.0.1.0/24 --type public
# AKA "Creating the VIP section" ๐Ÿ˜Ž
Enter fullscreen mode Exit fullscreen mode

Behind the curtains (drum roll please ๐Ÿฅ):

  1. Create namespace: ip netns add ns-my-vpc-public
    • Apartment building, meet your new tenant!
  2. Create veth pair: ip link add veth-public type veth peer name eth0
    • Virtual cables, assemble!
  3. Move one end to namespace: ip link set eth0 netns ns-my-vpc-public
    • "Welcome to your new home!" ๐Ÿ 
  4. Attach other end to bridge: ip link set veth-public master br-my-vpc
    • "Now kiss!" (the bridge and the interface, not you and your computer)
  5. Configure IP address
    • Every subnet needs an identity!
  6. Set up routing
    • GPS for packets ๐Ÿ—บ๏ธ

For public subnets, we add the secret sauce (NAT):

# Enable IP forwarding (because sharing is caring ๐Ÿ’)
sysctl -w net.ipv4.ip_forward=1

# Add NAT rule (the bouncer at the internet club ๐ŸŽญ)
iptables -t nat -A POSTROUTING -s 10.0.1.0/24 -o eth0 -j MASQUERADE
Enter fullscreen mode Exit fullscreen mode

Pro tip: This took me 2 hours and 3 cups of coffee to debug. Save yourself the pain - copy paste is not just a skill, it's a lifestyle! โ˜•๏ธ๐Ÿ˜…

Step 5: Deploy ALL The Apps! ๐Ÿš€

Time to make your subnet actually DO something:

sudo ./vpcctl deploy-app --vpc my-vpc --subnet public --port 8080
# Translation: "Let's get this party started!" ๐ŸŽŠ
Enter fullscreen mode Exit fullscreen mode

This creates a Python HTTP server that's more reliable than your ISP:

import http.server
import socketserver

PORT = 8080  # The magic number (or any port you fancy)
# Serving web pages like it's hot! ๐Ÿ”ฅ
with socketserver.TCPServer(("", PORT), http.server.SimpleHTTPRequestHandler) as httpd:
    httpd.serve_forever()  # Forever ever? Forever ever!
Enter fullscreen mode Exit fullscreen mode

Step 6: VPC Isolation (Trust Issues Much? ๐Ÿšง)

By default, VPCs don't talk to each other. It's like having separate WhatsApp groups for work and family - necessary for sanity! ๐Ÿ˜Œ

sudo ./vpcctl create-vpc --name dev-vpc --cidr 10.1.0.0/16
sudo ./vpcctl create-subnet --vpc dev-vpc --name public --cidr 10.1.1.0/24 --type public
# Creating parallel universes, one VPC at a time! ๐ŸŒŒ
Enter fullscreen mode Exit fullscreen mode

Try to ping from one VPC to another - DENIED! ๐Ÿšซ This is isolation working as intended. Your subnets are safer than money in your mom's purse! ๐Ÿ’ฐ

Step 7: VPC Peering (Now They're BFFs! ๐Ÿ‘ฏโ€โ™€๏ธ)

Want your VPCs to be friends? Let's introduce them properly:

sudo ./vpcctl peer-vpcs --vpc1 my-vpc --vpc2 dev-vpc
# "Meet cute" but for networks! ๐Ÿ’•
Enter fullscreen mode Exit fullscreen mode

What this does (it's actually pretty cool):

  1. Creates a veth pair connecting both bridges
    • Building bridges, literally!
  2. Adds routes so they can find each other
    • Like sharing locations on WhatsApp ๐Ÿ“

Now they can communicate! It's like when your crush finally texts back! ๐Ÿ“ฑโœจ

Step 8: Firewall Policies (The Rulebook ๐Ÿ“–)

Create a JSON policy file (fancy way of saying "The Law"):

{
  "subnet": "10.0.1.0/24",
  "ingress": [
    {
      "port": 80,
      "protocol": "tcp",
      "action": "allow" // Come on in, HTTP! ๐Ÿšช
    },
    {
      "port": 22,
      "protocol": "tcp",
      "action": "deny" // SSH? We don't know her! ๐Ÿ™…โ€โ™‚๏ธ
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Apply it like a boss:

sudo ./vpcctl apply-policy --vpc my-vpc --subnet public --policy policy.json
# "These are the rules, and yes, I made them up!" ๐Ÿ˜ค
Enter fullscreen mode Exit fullscreen mode

This translates to iptables magic (don't worry about understanding it, even I Google it sometimes ๐Ÿคซ):

iptables -A INPUT -p tcp --dport 80 -j ACCEPT  # Welcome!
iptables -A INPUT -p tcp --dport 22 -j DROP    # Nope!
Enter fullscreen mode Exit fullscreen mode

Testing Your Masterpiece ๐Ÿงช

If you're not testing, you're just hoping. And hope is NOT a strategy!

Test 1: Intra-VPC Love Connection ๐Ÿ’˜

Subnets within the same VPC should talk like old friends:

sudo ./vpcctl test-connectivity --vpc my-vpc --from-subnet public --to-subnet private
# Expected: โœ“ Connectivity test PASSED
# If it fails: Time for more coffee! โ˜•๏ธ
Enter fullscreen mode Exit fullscreen mode

Test 2: VPC Cold Shoulder ๐Ÿฅถ

Different VPCs without peering should ignore each other like exes at a party:

# Get the deets
NS1="ns-my-vpc-public"
IP2="10.1.1.2"  # IP from dev-vpc (the stranger danger)

# Try to ping (spoiler: it won't work)
sudo ip netns exec $NS1 ping -c 2 $IP2
# Result: Network unreachable โœ“
# Translation: "New phone, who dis?" ๐Ÿ“ฑ
Enter fullscreen mode Exit fullscreen mode

Test 3: Internet Access Check ๐ŸŒ

Public subnets flexing their internet access:

sudo ip netns exec ns-my-vpc-public ping -c 3 8.8.8.8
# Result: Success โœ“ (Google DNS says hello!)
Enter fullscreen mode Exit fullscreen mode

Private subnets staying humble and offline:

sudo ip netns exec ns-my-vpc-private ping -c 3 8.8.8.8
# Result: Network unreachable โœ“
# They're focused on the grind, no distractions! ๐Ÿ’ช
Enter fullscreen mode Exit fullscreen mode

Test 4: Peering Power-Up ๐Ÿ”Œ

After peering, VPCs become besties:

sudo ./vpcctl peer-vpcs --vpc1 my-vpc --vpc2 dev-vpc
sudo ip netns exec ns-my-vpc-public ping -c 2 10.1.1.2
# Result: Success โœ“
# They're texting now! ๐Ÿ“ฑ๐Ÿ’•
Enter fullscreen mode Exit fullscreen mode

When Things Go Wrong (And They Will! ๐Ÿ˜…)

Debugging is like being a detective in a crime movie where you're also the murderer!

Issue 1: "Operation not permitted" ๐Ÿšซ

Problem: Forgot to use sudo (we've ALL been there!)

Solution: Channel your inner admin:

sudo ./vpcctl create-vpc --name test --cidr 10.0.0.0/16
# Remember: With great power comes great sudo! ๐Ÿฆธโ€โ™‚๏ธ
Enter fullscreen mode Exit fullscreen mode

Issue 2: "Bridge already exists" ๐ŸŒ‰

Problem: You tried to create what already exists (philosophers hate this trick!)

Solution: Clear the slate:

sudo ./cleanup.sh
# When in doubt, nuke it out! ๐Ÿ’ฅ
Enter fullscreen mode Exit fullscreen mode

Issue 3: "No internet connectivity" ๐Ÿ“ก

Problem: This one haunted my dreams for 2 hours! ๐Ÿ˜ญ

Solution: This got me! I spent like 30 minutes debugging before realizing IP forwarding was disabled. Check it:

sysctl net.ipv4.ip_forward
# If it's 0, you need to flip that switch!
sudo sysctl -w net.ipv4.ip_forward=1  # The magic command โœจ
Enter fullscreen mode Exit fullscreen mode

Other things to check (because I'm nice like that):

  • Interface name might be ens33 instead of eth0 (Linux is creative with naming!)
  • iptables rules might be blocking you (check with sudo iptables -t nat -L)
  • Your coffee might be empty (this is CRITICAL! โ˜•๏ธ)

Cleanup: Leaving No Trace ๐Ÿงน

Always clean up after yourself (your mom taught you this!):

# Delete specific VPC (the gentle approach)
sudo ./vpcctl delete-vpc --name my-vpc

# Delete EVERYTHING (the "I need a fresh start" approach)
sudo ./vpcctl cleanup-all

# Or use the cleanup script (the "I trust this more" approach)
sudo ./cleanup.sh
# Thanos would be proud! ๐Ÿซฐโœจ
Enter fullscreen mode Exit fullscreen mode

This removes (THE COMPLETE PURGE):

  • All network namespaces ๐Ÿ—‘๏ธ
  • All bridges ๐ŸŒ‰
  • All veth pairs ๐Ÿ”Œ
  • All iptables rules ๐Ÿ”ฅ
  • State files ๐Ÿ“
  • Your mistakes (we all need this sometimes!) ๐Ÿ˜Œ

The Victory Lap: Full Test Suite ๐Ÿ

The project includes tests more comprehensive than your mom's questions when you get home late:

sudo make test
# Sit back, relax, and watch the magic happen! โœจ
Enter fullscreen mode Exit fullscreen mode

This validates (EVERYTHING):

  • โœ… VPC creation and deletion (Birth and... well, you know)
  • โœ… Subnet management (The subdivision saga)
  • โœ… Application deployment (App goes live!)
  • โœ… Connectivity (Can we talk?)
  • โœ… Isolation (Stay in your lane!)
  • โœ… Peering (Making friends!)
  • โœ… Firewall policies (The rules of engagement)
  • โœ… NAT gateway (Internet access for all!)
  • โœ… Cleanup (Leaving it better than we found it)

What You Just Became an Expert In ๐ŸŽ“

Congratulations! You now understand:

  1. Linux Network Namespaces: Like apartments for network stacks ๐Ÿ 
  2. Virtual Networking: The art of making cables out of thin air ๐ŸŽจ
  3. Routing: GPS for packets, but actually reliable ๐Ÿ—บ๏ธ
  4. NAT: The passport office of networking ๐Ÿ›‚
  5. iptables: The bouncer who decides who gets in ๐Ÿ’ช
  6. VPC Architecture: How AWS does it (but now YOU can do it too!) ๐Ÿ˜Ž
  7. Infrastructure as Code: Because clicking buttons is so 2010 ๐Ÿ–ฑ๏ธ

Where This Knowledge Pays Rent ๐Ÿ’ฐ

These concepts show up EVERYWHERE:

  • Docker: Uses network namespaces (you're basically a Docker expert now! ๐Ÿณ)
  • Kubernetes: Pod networking is just fancy namespace usage (k8s who? We know them! โ˜ธ๏ธ)
  • Cloud VPCs: AWS VPC, Azure VNet, Google VPC (all doing what you just did!) โ˜๏ธ
  • SD-WAN: Software-defined networking (fancy name, same concepts) ๐ŸŒ
  • Job Interviews: "So tell me about network namespaces..." (You: cracks knuckles ๐Ÿ˜)

Next Level Moves (When You're Feeling Adventurous) ๐Ÿš€

Want to take this further? Here are some ideas:

  1. Add custom DHCP (automatic IP assignment, fancy!)
  2. Implement load balancing (spread the load like butter ๐Ÿงˆ)
  3. Add VPN gateway (secure connections ftw! ๐Ÿ”)
  4. Create a web dashboard (because CLI is cool but GUI is prettier ๐ŸŽจ)
  5. Support IPv6 (future-proofing like a boss! ๐Ÿ”ฎ)
  6. Container runtime integration (Docker + Your VPC = ๐Ÿ’•)

The Grand Finale: What Did We Learn Today? ๐ŸŽฌ

So that's it! I went from "network namespace what?" to building a full VPC system that would make AWS nervous (okay, maybe not nervous, but they'd def give a nod of respect ๐Ÿซก).

Key takeaways (write these down, there might be a test):

  1. VPCs aren't magical - they're just namespaces + bridges + routing (and A LOT of coffee โ˜•๏ธ)
  2. The onlink flag is a LIFESAVER (seriously, I almost named my firstborn "Onlink")
  3. Always enable IP forwarding BEFORE testing NAT (learn from my pain!)
  4. Interface names have a 15-character limit (learned THAT one the expensive way - 2 hours I'll never get back! โฐ)
  5. When in doubt, check the logs (they never lie, unlike my "5 minutes left" estimates)
  6. Google is your friend (so is Stack Overflow, so is that random blog from 2012)

The complete code is available in the repository. Try it out, break it (you will!), fix it (you can!), and most importantly - understand how every piece works together like a well-oiled machine! ๐Ÿ› ๏ธ

Additional Resources (For the Overachievers ๐Ÿ“š)

Want to go deeper? Check these out:

  • Linux Network Namespaces: man ip-netns (bedtime reading? ๐Ÿ˜ด)
  • iptables Documentation: man iptables (thriller novel alternative!)
  • Linux Bridge: man bridge (surprisingly less boring than expected!)
  • iproute2 Documentation: man ip (for when Netflix isn't enough!)
  • My GitHub: hng13-stage4-devops (shameless plug! ๐Ÿ˜Ž)

Installation Speedrun ๐Ÿƒโ€โ™‚๏ธ๐Ÿ’จ

git clone https://github.com/herdeybayor/hng13-stage4-devops
cd hng13-stage4-devops
make install  # One command to rule them all!
sudo ./vpcctl --help  # Your journey begins here! ๐Ÿ—บ๏ธ
Enter fullscreen mode Exit fullscreen mode

Quick Start (For the Impatient Ones) โšก๏ธ

# Create a VPC (you're basically a cloud provider now!)
sudo ./vpcctl create-vpc --name demo --cidr 10.0.0.0/16

# Add a public subnet (VIP section activated!)
sudo ./vpcctl create-subnet --vpc demo --name web --cidr 10.0.1.0/24 --type public

# Deploy an app (hello world, but make it network-y!)
sudo ./vpcctl deploy-app --vpc demo --subnet web --port 8080

# View your empire
sudo ./vpcctl list-vpcs  # Look at what you built! ๐Ÿฐ

# Clean up (responsible developer energy!)
sudo ./vpcctl delete-vpc --name demo
Enter fullscreen mode Exit fullscreen mode

Connect With Me! ๐Ÿค

If you're working on something similar, or if this helped you, or if you just want to chat about networking over virtual jollof rice, hit me up!

  • ๐ŸŒ Portfolio: sherifdeenadebayo.com (check out my other projects!)
  • ๐Ÿ’ผ LinkedIn: @herdeybayor (let's connect professionally!)
  • ๐Ÿ’ป GitHub: @herdeybayor (where the code lives!)
  • โœ๏ธ DEV.to: Where I write stuff like this when caffeine levels are optimal โ˜•๏ธ
  • ๐Ÿ“ง Questions? Create an issue! I promise I read them (eventually ๐Ÿ˜…)

Sherifdeen Adebayo

DevOps Engineer | Professional Coffee Consumer | Network Namespace Whisperer

Built for HNG13 Stage 4 Challenge - November 2025

Powered by determination, late nights, and an unhealthy amount of Stack Overflow! ๐Ÿ’ป


P.S. - If you're doing the HNG challenge too, you got this! ๐Ÿ’ช This stage kicked my butt multiple times, but look - we both made it! If I can build this while Googling "what is a network namespace" every 5 minutes, you can too!

P.P.S. - Special shoutout to coffee, without which this project would still be "TODO: Figure out networking" ๐Ÿ˜‚

P.P.P.S. - Yes, the interface naming bug took me 2 hours. Yes, I cried a little. Yes, it was just a 15-character limit. We don't talk about it. ๐Ÿ™ˆ

Remember: In software development, like in life, sometimes you just gotta sudo your way through! ๐Ÿš€โœจ


Written while simultaneously debugging, eating chin-chin, and questioning my career choices (in the best way possible!) ๐ŸŒŸ

If this code works on your machine, you're welcome! If it doesn't... did you try turning it off and on again? ๐Ÿ˜‰

Top comments (0)