DEV Community

Cover image for How I Used WireGuard to Secure API Communication Between Cloud Servers
Afe Damilare
Afe Damilare

Posted on

How I Used WireGuard to Secure API Communication Between Cloud Servers

INTRODUCTION

In modern cloud architectures, microservices and APIs often communicate over the public internet. While HTTPS provides encryption in transit, sometimes you want an additional layer of security, especially when sensitive data flows between servers.

In this article, I’ll show how I used WireGuard, a lightweight and high-performance VPN, to secure API communication between two cloud servers. This setup ensures that all API traffic flows over an encrypted tunnel, reducing the risk of interception or tampering.


WHY WIREGUARD?

WireGuard offers several advantages for cloud-to-cloud communication:

Simplicity: Easy to configure compared to traditional VPNs like OpenVPN or IPsec.

Performance: High throughput with minimal latency.

Modern cryptography: Uses state-of-the-art protocols (ChaCha20, Poly1305).

Cross-platform: Works on Linux, Windows, macOS, and even mobile devices.

With WireGuard, you can create a private network between servers and secure all API calls automatically.


PREREQUISITES

Before you start, you’ll need:

  • Two cloud servers (e.g., AWS EC2, DigitalOcean Droplets) with root access.

  • Linux OS on both servers (Ubuntu 22.04 recommended).

  • Basic familiarity with the terminal and SSH.

Let’s call the servers:

Server A — the API client

Server B — the API server


Step 1: INSTALL WIREGUARD

On both servers, install WireGuard:

Update package list

sudo apt update

Install WireGuard

sudo apt install wireguard -y

Verify installation:

wg --version


Step 2: GENERATE KEYS

WireGuard uses public/private key pairs for authentication. On each server:

Generate private key

wg genkey | tee privatekey | wg pubkey > publickey

This will create two files:

  • privatekey → Keep secret

  • publickey → Share with the other server


Step 3: CONFIGURE WIREGUARD INTERFACES

  • On Server A (wg0.conf):

[Interface]
PrivateKey =
Address = 10.0.0.1/24
ListenPort = 51820

[Peer]
PublicKey =
Endpoint = :51820
AllowedIPs = 10.0.0.2/32
PersistentKeepalive = 25

  • On Server B (wg0.conf):

[Interface]
PrivateKey =
Address = 10.0.0.2/24
ListenPort = 51820

[Peer]
PublicKey =
Endpoint = :51820
AllowedIPs = 10.0.0.1/32
PersistentKeepalive = 25

  • Explanation:

Address → Private IP for WireGuard interface

Endpoint → Public IP and port of the peer

AllowedIPs → IPs to route through the VPN

PersistentKeepalive → Keeps the connection alive behind NAT


Step 4: ENABLE AND START WIREGUARD

Start WireGuard

sudo wg-quick up wg0

Enable at boot

sudo systemctl enable wg-quick@wg0

Check connection status:

sudo wg

You should see handshake information, indicating that both servers are connected.


Step 5: TEST API COMMUNICATION

Assume Server B is running an API on port 8000. On Server A:

curl http://10.0.0.2:8000/health

The request should succeed over the encrypted WireGuard tunnel, even if the API port is not exposed publicly.


Step 6: OPTIONAL — RESTRICT PUBLIC ACCESS

For maximum security, update the firewall on Server B to only allow API requests via WireGuard:

sudo ufw allow from 10.0.0.0/24 to any port 8000
sudo ufw deny 8000

Now, the API is effectively private to the VPN.


BENEFITS OF THIS SETUP:

End-to-end encryption for API communication

Private network between cloud servers

Minimal latency and easy deployment

Works for multi-cloud or hybrid cloud architectures


Conclusion

By using WireGuard, I was able to secure API communication between servers with minimal configuration and excellent performance. This approach is particularly useful when sensitive data flows across cloud providers or when you want to avoid exposing APIs publicly.

WireGuard is lightweight, fast, and easy to manage — making it a go-to solution for secure cloud-to-cloud networking.


If you found this tutorial helpful, follow me on Dev.to for more cloud security and networking tips!

Top comments (0)