DEV Community

Eastern Dev
Eastern Dev

Posted on

How I Automated VPN Deployment: A Step-by-Step Guide

Why Automate VPN Deployment?

Manual VPN setup takes 2-3 hours per server. With clients needing servers across multiple regions, that is unsustainable.

I spent 2 weeks building an automated pipeline. Now I deploy a new VPN server in 5 minutes.

The Stack

  • WireGuard - Modern, fast, secure
  • Ansible - Configuration management
  • Cloud-init - Server provisioning

Step 1: Infrastructure as Code

# wireguard-server.yml
- hosts: all
  become: yes
  tasks:
    - name: Install WireGuard
      apt:
        name: wireguard
        state: present
Enter fullscreen mode Exit fullscreen mode

Step 2: Auto-Generate Client Configs

#!/usr/bin/env python3
import subprocess

def create_client(name):
    private = subprocess.check_output(["wg", "genkey"]).decode().strip()
    public = subprocess.check_output(["wg", "pubkey"], input=private.encode()).decode().strip()

    config = f"""[Interface]
PrivateKey = {private}
Address = 10.0.0.2/32
DNS = 1.1.1.1

[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = vpn.example.com:51820
AllowedIPs = 0.0.0.0/0
"""

    with open(f"clients/{name}.conf", "w") as f:
        f.write(config)
    return public
Enter fullscreen mode Exit fullscreen mode

Step 3: One-Command Deploy

./deploy.py --region sg --clients 5
Enter fullscreen mode Exit fullscreen mode

Spins up VPS, installs WireGuard, generates configs, sends download links.

Step 4: Auto-Delivery via Webhook

@app.route("/webhook")
def handle_payment():
    deploy_async(region=get_region(), count=get_count())
    return "OK"
Enter fullscreen mode Exit fullscreen mode

Customer receives config within 60 seconds of payment.

Results

Metric Before After
Setup time 2 hours 5 minutes
Manual work 100% 0%
Wait time 24 hours 60 seconds

Try It

Pre-configured VPN services:

👉 https://eastern-shop.surge.sh/

Instant delivery. BTC accepted.

Top comments (0)