Bottom line: Routing a workload’s outbound traffic through a flat-rate WireGuard VPS instead of a metered cloud NAT gateway cuts per-GB egress from roughly $0.09 to under $0.01, at the cost of one extra hop and a single point of failure.

The mechanism is boring and old: a Linux box with IP forwarding on, a nat table MASQUERADE rule, and a WireGuard tunnel carrying the workload’s default route. What makes it worth writing down is the arithmetic. Managed NAT gateways bill per gigabyte processed plus an hourly charge, and that per-GB number is where the money goes once a workload starts pulling container images, shipping logs, or talking to third-party APIs. A small VPS with a flat monthly bandwidth allowance turns that variable cost into a fixed one. The trade is real: you now own a router, and when it dies, everything behind it loses the internet.

The cost model, plainly

Take a workload that pushes 3 TB/month outbound. At $0.09/GB that is roughly $276 in egress alone, before the hourly NAT gateway charge. The same 3 TB through a VPS with a flat rate and an included bandwidth allowance costs the monthly fee and nothing more, so long as you stay under the cap. Even a mid-size instance with a generous allowance lands well under $0.01/GB effective.

Path Per-GB egress Fixed monthly 3 TB/month total Extra hop Failure domain
Cloud NAT gateway ~$0.09 hourly charge ~$276 + hourly none provider-managed
WireGuard VPS, flat rate ~$0.005–0.01 effective VPS fee VPS fee only one your VPS
Direct public IP on workload ~$0.09 none ~$276 none workload itself

The numbers that matter are the ones you measure yourself. Pull your actual egress from the provider’s billing console for the last full month, multiply by the per-GB rate, and compare against the VPS fee plus whatever overage the allowance implies. If the workload is under a few hundred GB/month, the VPS is not worth the operational overhead — the managed gateway is cheaper than your time. The crossover is somewhere in the low hundreds of GB, depending on the VPS price.

Provisioning the gateway

Pick a provider with a flat bandwidth allowance and a location close to the workload, because every byte now takes an extra hop and latency is additive. A 1 vCPU / 1 GB instance is enough for a NAT gateway handling a few hundred Mbit; WireGuard’s crypto is cheap and the kernel does the forwarding.

Install WireGuard from the distribution packages, generate keys, and bring the interface up. The quickstart flow is the same on both ends:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# On the gateway
umask 077
wg genkey | tee /etc/wireguard/privatekey | wg pubkey > /etc/wireguard/publickey

cat > /etc/wireguard/wg0.conf <<'EOF'
[Interface]
Address = 10.66.0.1/24
ListenPort = 51820
PrivateKey = <gateway-private-key>

[Peer]
# workload
PublicKey = <workload-public-key>
AllowedIPs = 10.66.0.2/32
EOF

systemctl enable --now wg-quick@wg0

On the workload side, the peer config points at the gateway’s public IP and routes everything through the tunnel:

1
2
3
4
5
6
7
8
9
10
11
12
13
cat > /etc/wireguard/wg0.conf <<'EOF'
[Interface]
Address = 10.66.0.2/24
PrivateKey = <workload-private-key>

[Peer]
PublicKey = <gateway-public-key>
Endpoint = 203.0.113.10:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOF

systemctl enable --now wg-quick@wg0

AllowedIPs = 0.0.0.0/0 is what installs the default route into the tunnel. PersistentKeepalive = 25 matters when the workload sits behind NAT or a stateful firewall and needs to keep the mapping open; the WireGuard documentation recommends 25 seconds as a value that survives most firewalls. If the workload has a public address and no NAT in front of it, leave keepalive off — it only adds chatter.

Making the gateway actually forward

A tunnel that terminates is not a gateway. The VPS needs IP forwarding and a MASQUERADE rule on its public interface, or return traffic will never find its way back.

1
2
3
4
5
6
7
8
9
10
11
12
# Enable forwarding persistently
cat > /etc/sysctl.d/99-wg-gateway.conf <<'EOF'
net.ipv4.ip_forward = 1
EOF
sysctl --system

# NAT the tunnel subnet out the public interface
iptables -t nat -A POSTROUTING -s 10.66.0.0/24 -o eth0 -j MASQUERADE

# Allow forwarded traffic from the tunnel
iptables -A FORWARD -i wg0 -o eth0 -j ACCEPT
iptables -A FORWARD -i eth0 -o wg0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

The nat table is consulted when a new connection is created, and POSTROUTING is the chain where packets are altered as they leave — that is exactly where source NAT belongs. The FORWARD rules in the filter table are the part people forget: the default policy on many distributions is DROP, and without an explicit accept the tunnel comes up, pings the gateway, and goes nowhere else.

Persist the rules with whatever your distribution ships — iptables-persistent, netfilter-persistent, or a systemd unit that runs iptables-restore from a saved file. Rules that vanish on reboot are the most common cause of “it worked yesterday.”

Hardening the box

This VPS is now a router on the public internet with a tunnel endpoint. Treat it accordingly.

  • SSH: keys only, no root login, and a firewall rule that allows 22/tcp from your management addresses rather than the world.
  • WireGuard port: allow 51820/udp. Nothing else inbound is needed.
  • Keep the peer list tight. One [Peer] block per workload, each with a /32 in AllowedIPs. Do not hand out a shared key across workloads; per-peer keys let you revoke one without touching the rest.
  • Disable anything the image ships that you did not ask for. A minimal image is less to patch.
  • Unattended upgrades for security patches, and a calendar reminder to reboot into new kernels.

The gateway’s wg0.conf should list peers explicitly. A peer with AllowedIPs = 0.0.0.0/0 on the gateway side would let that workload route to other workloads’ tunnel addresses, which is not what you want.

Pitfalls and how to diagnose them

Tunnel is up, no internet behind it. Check sysctl net.ipv4.ip_forward returns 1, then check the FORWARD chain policy and counters with iptables -L FORWARD -v -n. If the counters on your accept rules are zero, traffic is not reaching them.

Asymmetric routing. The workload has two default routes — one from its cloud interface, one from wg0 — and replies leave the wrong way. Confirm with ip route get 1.1.1.1 on the workload; it should name wg0. If the cloud DHCP client keeps reinstalling its default route, use a routing table and ip rule to pin the tunnel route, or set the interface metric.

MTU black holes. WireGuard adds overhead, so the tunnel MTU is lower than the physical link. Large packets vanish and small ones work — the classic symptom is a TLS handshake that hangs. Set MTU = 1420 on wg0 at both ends and test with ping -M do -s 1400 <gateway-tunnel-ip>.

Keepalive missing behind NAT. The tunnel goes silent and inbound connections stop arriving. Add PersistentKeepalive = 25 on the peer that sits behind NAT, not on the gateway.

MASQUERADE on the wrong interface. The rule names eth0, but the VPS renamed its interface to ens3. Check ip -br link and fix the rule; a mismatched -o means the rule never matches and return traffic is dropped.

Rules lost on reboot. iptables -L after a reboot shows an empty nat table. Save with iptables-save > /etc/iptables/rules.v4 and confirm the restore unit is enabled.

Single point of failure. The gateway dies and every workload behind it loses egress. Either accept the outage window, or run a second gateway with a second peer and a higher-metric route on the workload so failover is automatic. A health check that curls through the tunnel and flips the route is a few lines of shell.

FAQ

Why not just give the workload a public IP and skip the gateway?

A public IP still bills egress at the metered rate. The gateway exists to move traffic onto a flat-rate pipe, not to provide addressing.

Does the extra hop hurt latency?

It adds one network round trip between the workload and the VPS. If the VPS is in the same region, that is typically single-digit milliseconds. Measure it with mtr through the tunnel before committing.

Can I route only some traffic through the gateway?

Yes. Set AllowedIPs on the workload’s peer to the specific prefixes you want tunneled instead of 0.0.0.0/0, and leave the rest on the default route. This is useful when only one upstream API or registry is expensive.

What happens when the VPS bandwidth allowance runs out?

Depends on the provider: throttling, overage billing, or a hard cap. Check the terms before you move 3 TB through it, and monitor monthly usage from the provider’s console.

Is WireGuard the right choice versus IPsec or OpenVPN?

For a single static tunnel between two Linux hosts, WireGuard is less configuration and lives in the kernel. IPsec makes sense when you need to interoperate with existing gateways; OpenVPN when you need TCP transport through hostile networks.

How do I revoke a workload’s access?

Remove its [Peer] block from the gateway config, run wg syncconf wg0 <(wg-quick strip wg0), and delete the key on the workload. The tunnel drops immediately and no other peer is affected.