Skip to content
5 min read Updated

Automating MikroTik WireGuard VPN with Role-Based Access via Terraform

Deploy a WireGuard VPN on MikroTik with Terraform, with role-based network access that keeps a phone away from what the admin laptop can reach.


Disclosure: This post contains Amazon affiliate links (marked with *). As an Amazon Associate I earn from qualifying purchases. Buying through them costs you nothing extra.

Update (May 2026): I decommissioned this WireGuard setup on 2026-05-20 and moved remote access to Headscale with OIDC via Authelia. The Terraform patterns below still work on RouterOS 7; they just no longer run in my network.

WireGuard has become the default for remote-access VPNs: fast, simple, and built on modern cryptography. This setup runs on the MikroTik RB5009*, where RouterOS 7 handles WireGuard natively without a separate container. The easy setup also invites sloppy security, like giving every connected device full access to the entire internal network.

In a secure environment, enterprise or homelab, a phone checking a dashboard should not have the same network access as an admin laptop running infrastructure deployments.

This guide automates a WireGuard “Roadwarrior” setup on MikroTik RouterOS with Terraform and enforces role-based access with firewall filters.

View the complete MikroTik IaC source code on GitHub

1. Defining the VPN Topology

🇩🇪 Deutsch: This article is also available in German: WireGuard-VPN auf MikroTik automatisiert: Role-based Peers via Terraform →.

Start with the VPN subnet, the listening port, and the static IPs for each peer (device). Keeping them in Terraform locals keeps the configuration in one place and prevents IP conflicts later.

locals {
  vpn_config = {
    subnet = "10.6.0.0/24"
    port   = 51820
    name   = "wg-roadwarrior"
  }

  vpn_handy_ip  = "10.6.0.2/32"
  vpn_laptop_ip = "10.6.0.3/32"
}

2. Setting up the WireGuard Interface

With the terraform-routeros provider, the WireGuard interface and the router’s gateway IP in the VPN subnet take two resources.

# Create the WireGuard Interface
resource "routeros_wireguard" "wg_vpn" {
  name        = local.vpn_config.name
  listen_port = local.vpn_config.port
  comment     = "Remote Access VPN"
}

# Assign the Gateway IP to the Router
resource "routeros_ip_address" "wg_ip" {
  interface = routeros_wireguard.wg_vpn.name
  # Uses cidrhost to automatically calculate the first IP (.1)
  address   = "${cidrhost(local.vpn_config.subnet, 1)}/24"
}

cidrhost() calculates the .1 address from the subnet in the locals, so there is no address to mistype. If you pick the 10.6.0.0/24 range by hand, check it for collisions in the IPv4 subnet calculator. A VPN range that overlaps a LAN range tends to show up only weeks later, as a broken route.

3. Provisioning the Peers (Devices)

Next, add the devices’ public keys to the router. WireGuard uses cryptokey routing: the router uses the allowed_address field to decide which peer a given IP belongs to, and drops packets from a peer whose source IP isn’t in that peer’s list.

Binding each /32 to one key gives every device a fixed identity that the firewall rules can rely on.

resource "routeros_wireguard_peer" "handy_dw" {
  interface            = routeros_wireguard.wg_vpn.name
  comment              = "Smartphone DW - Limited Access"
  public_key           = "X9iI0RGNf7kTxdBOs4CsDcOQtKRFMYALY/ugHv67uAo="
  allowed_address      = [local.vpn_handy_ip]
  persistent_keepalive = "25s"
}

resource "routeros_wireguard_peer" "laptop_dw" {
  interface            = routeros_wireguard.wg_vpn.name
  comment              = "Laptop DW - Full Admin Access"
  public_key           = "zD+/yfLfjQ7N1H5NBIwa2zKNd/bLZ6VRdEbKBxCmOVA="
  allowed_address      = [local.vpn_laptop_ip]
  persistent_keepalive = "25s"
}

4. Enforcing Role-Based Access (The Firewall)

The VPN is up, but without firewall rules traffic goes nowhere, assuming a strict default-deny firewall like the one in my Zero-Trust MikroTik firewall post.

The forward chain defines what each device may reach.

The Admin Laptop (Full Access)

The laptop is a trusted admin device. It gets the entire 10.0.0.0/16 block: servers, management interfaces, and the DMZ.

resource "routeros_ip_firewall_filter" "fwd_06_vpn_laptop" {
  action       = "accept"
  chain        = "forward"
  src_address  = local.vpn_laptop_ip
  dst_address  = "10.0.0.0/16"
  place_before = routeros_ip_firewall_filter.fwd_99_drop_all.id
  comment      = "06: VPN - Laptop Full Access"
  
  lifecycle {
    ignore_changes = [src_address]
  }
}

The Mobile Phone (Split-Tunnel / Limited Access)

Phones are riskier. They join public Wi-Fi and get lost. The phone is limited to specific service subnets: the vlan20 server network and the DMZ proxy. It has no access to the Proxmox management VLAN or internal infrastructure backends.

resource "routeros_ip_firewall_filter" "fwd_07_vpn_handy_srv" {
  action       = "accept"
  chain        = "forward"
  src_address  = local.vpn_handy_ip
  dst_address  = "10.0.20.0/24"
  place_before = routeros_ip_firewall_filter.fwd_99_drop_all.id
  comment      = "07: VPN - Mobile limited to internal services"
}

resource "routeros_ip_firewall_filter" "fwd_08_vpn_handy_dmz" {
  action       = "accept"
  chain        = "forward"
  src_address  = local.vpn_handy_ip
  dst_address  = "10.0.30.0/24"
  place_before = routeros_ip_firewall_filter.fwd_99_drop_all.id
  comment      = "08: VPN - Mobile access to DMZ (External Proxy)"
}

place_before inserts these rules above the final “Drop All” rule that anchors the chain.

5. Don’t Forget the Input Chain!

Finally, for the VPN to connect at all, the listening port has to be open on the router’s WAN interface. This rule belongs in the input chain, because the handshake terminates at the router itself.

resource "routeros_ip_firewall_filter" "in_02_wg" {
  action       = "accept"
  chain        = "input"
  protocol     = "udp"
  dst_port     = local.vpn_config.port
  in_interface = "ether1"
  place_before = routeros_ip_firewall_filter.drop_all_input.id
  comment      = "IN-02: WireGuard handshake"
}

Identity as Address

WireGuard’s static cryptokey routing maps each device key to a fixed IP address. With that mapping in Terraform, identity and firewall policy live in the same reviewed code, and the Zero-Trust rules stay strict, readable, and documented in Git.

Tying cryptographic identity to network access is the same principle behind Azure Private Link and Managed Identity, one layer up.

Was this helpful?
Share
DW

David Woitzik

Hybrid Cloud Engineer

I work mostly with Azure, Terraform, and Zero-Trust network design, and publish the Terraform templates and write-ups I wanted when I was solving the same problems.

Keep reading

More like this in your inbox

New Terraform modules and articles by email. No spam.