Skip to content
6 min read Updated

Implementing a Zero-Trust MikroTik Firewall with Terraform

How I run a default-deny MikroTik RouterOS firewall with strict VLAN isolation and FastTrack for established traffic, all managed in Terraform.


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.

MikroTik routers are powerful, but configuring them by hand in WinBox or the CLI leads to configuration drift and security holes. This setup runs on the RB5009*: its switch chip handles hardware offloading for VLAN-tagged traffic, which matters once you’re carrying multiple VLANs across a Proxmox trunk. If you are building a segmented edge network or a home lab with multiple VLANs, you need a firewall that is version-controlled, reproducible, and strictly default-deny.

Here’s how I build that Zero-Trust firewall for RouterOS with the terraform-routeros provider. That provider is also the main reason I picked MikroTik over UniFi; the full MikroTik vs Ubiquiti comparison covers the rest of the trade-off.

View the complete MikroTik IaC source code on GitHub

The Core Philosophy: Default-Deny

A standard home router allows everything outbound and blocks everything inbound. A Zero-Trust edge device blocks everything by default, including traffic between internal VLANs, unless explicitly permitted.

We enforce this by anchoring the input (traffic to the router) and forward (traffic through the router) chains with final drop rules:

# ===============================================
# ANCHOR RULES
# ===============================================

resource "routeros_ip_firewall_filter" "drop_all_input" {
  action  = "drop"
  chain   = "input"
  comment = "INPUT: Default drop"
}

resource "routeros_ip_firewall_filter" "fwd_99_drop_all" {
  action     = "drop"
  chain      = "forward"
  log        = true
  log_prefix = "FW_DROP"
  comment    = "99: Global - Final Drop (Zero Trust Policy)"
}

With these rules at the very end of their chains, anything that no accept rule catches is dropped, and in the forward chain logged.

1. Securing the Router (The Input Chain)

The input chain protects the MikroTik device itself. We only want established connections, our WireGuard VPN handshakes, and tightly scoped management access to reach the router’s CPU:

resource "routeros_ip_firewall_filter" "in_01_established" {
  action           = "accept"
  chain            = "input"
  connection_state = "established,related,untracked"
  place_before     = routeros_ip_firewall_filter.drop_all_input.id
  comment          = "IN-01: Allow established/related"
}

# Allow Admin-VLAN to access the Router API/WinBox
resource "routeros_ip_firewall_filter" "in_03_mgmt" {
  action           = "accept"
  chain            = "input"
  src_address_list = "Mgmt_Devices"
  place_before     = routeros_ip_firewall_filter.drop_all_input.id
  comment          = "IN-03: Allow Admin-VLAN access to Router-API"
}

Note the place_before argument. It’s the main reason to manage firewall rules with Terraform. Instead of relying on fragile manual ordering, we tell Terraform explicitly to place these allow rules above the default drop anchor.

2. Segmenting the Network (The Forward Chain)

This is where the actual routing happens. In my setup, I have dedicated VLANs for Management (vlan10), Servers (vlan20), and a DMZ (vlan30). The underlying L2 VLAN matrix (bridge ports, tagged/untagged assignments, and the Proxmox trunk) is automated separately with Terraform.

Because of our fwd_99_drop_all rule, a compromised Docker container in vlan20 cannot blindly scan the management interface of the Proxmox node in vlan10. Every flow that is actually needed gets its own explicit accept rule:

# FastTrack for CPU Efficiency (Crucial for Gigabit speeds on MikroTik)
resource "routeros_ip_firewall_filter" "fwd_00_fasttrack" {
  action           = "fasttrack-connection"
  chain            = "forward"
  connection_state = "established,related"
  hw_offload       = true
  place_before     = routeros_ip_firewall_filter.fwd_99_drop_all.id
  comment          = "00: Global - Fasttrack for CPU efficiency"
}

# Allow internal Reverse Proxy to reach Management GUIs
resource "routeros_ip_firewall_filter" "fwd_04_proxy_to_mgmt" {
  chain        = "forward"
  action       = "accept"
  src_address  = "10.0.20.0/24"
  dst_address  = "10.0.10.0/24"
  dst_port     = "8006,8007"
  protocol     = "tcp"
  place_before = routeros_ip_firewall_filter.fwd_99_drop_all.id
  comment      = "04: SRV - Internal Proxy access to MGMT Web GUIs"
}

# Allow Prometheus to scrape DMZ node exporters
resource "routeros_ip_firewall_filter" "fwd_05_srv_to_dmz_metrics" {
  chain        = "forward"
  action       = "accept"
  src_address  = "10.0.20.252" # Specific Docker LXC IP
  dst_address  = "10.0.30.0/24"
  dst_port     = "9100,9080"
  protocol     = "tcp"
  place_before = routeros_ip_firewall_filter.fwd_99_drop_all.id
  comment      = "05: Monitoring - Prometheus scrape DMZ node exporters"
}

FastTrack needs a companion rule. fasttrack-connection only marks connections for the fast path; packets that haven’t been fasttracked yet, or never can be, keep walking the chain. Right after it you need an accept for connection_state = "established,related,untracked", exactly as in MikroTik’s default configuration. Without that rule, the final drop catches return traffic. My live config has it as fwd_01_established.

By pinning the source to a single IP (10.0.20.252), only the Docker LXC that runs Prometheus can query the DMZ metrics.

3. Handling Public Exposure (NAT & WAN)

If you host a service like a Minecraft server in your DMZ, you need destination NAT (port forwarding). The NAT rule alone isn’t enough: the firewall must also let the traffic cross from the WAN into the DMZ.

# Port Forwarding
resource "routeros_ip_firewall_nat" "dnat_minecraft" {
  action       = "dst-nat"
  chain        = "dstnat"
  in_interface = "ether1"
  protocol     = "tcp"
  dst_port     = "25565"
  to_addresses = "10.0.30.2"
  to_ports     = "25565"
  comment      = "NAT: Port Forwarding for DMZ Minecraft Server"
}

# Firewall Allow
resource "routeros_ip_firewall_filter" "fwd_13_wan_to_dmz_minecraft" {
  action       = "accept"
  chain        = "forward"
  dst_address  = "10.0.30.2"
  dst_port     = "25565"
  protocol     = "tcp"
  in_interface = "ether1"
  place_before = routeros_ip_firewall_filter.fwd_99_drop_all.id
  comment      = "13: WAN - Allow Internet traffic to DMZ Minecraft Server"
}

Moving Beyond the Edge

Automating the edge router is the first step toward code-driven infrastructure. The same Zero-Trust principles apply in enterprise cloud environments, with far more rules and owners.

Nothing about default-deny changes when you move from a RouterOS edge router to Azure. The drop-by-default forward chain becomes NSG deny rules, Azure Firewall policies, and Private Link access control. The primitives are different; the posture is identical.

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.