Skip to content
6 min read Updated

Automating MikroTik Bridge VLAN Filtering & Proxmox Trunks with Terraform

MikroTik Bridge VLAN Filtering without the lockouts: automate the VLAN matrix, Proxmox trunk ports, and edge access ports with 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.

If you have ever configured VLANs on a MikroTik router, you know the pain. I run the RB5009* because its switch chip offloads bridge VLAN filtering to hardware (how it stacks up against a UniFi Dream Router is in MikroTik vs Ubiquiti UniFi for a homelab). On RouterOS 6, cheaper models like the hEX did bridge VLAN filtering on the CPU; RouterOS 7 added hardware offload for the hEX’s MT7621 chip as well, so check the switch chip of your model before you assume either way. Moving from the legacy switch-chip VLAN configuration to Bridge VLAN Filtering is also the classic way to lock yourself out of the router.

Add a virtualization host like Proxmox VE, which needs a trunk port carrying several tagged VLANs, and the complexity multiplies. Label your cables early: a Brother Labelmaker* saved me hours of tracing connections during a VLAN migration. Doing all of this in WinBox is a recipe for configuration drift and late-night outages.

This post manages MikroTik VLANs with Terraform. The result is an L2 layout driven by one map: a Proxmox trunk port, a dedicated admin access port, and hardware-offloaded access ports for edge nodes like the Raspberry Pis* (hardening guide).

View the complete MikroTik IaC source code on GitHub

The Architecture & Locals

Before writing any resources, define the topology. Hardcoding VLAN IDs across dozens of resources invites mistakes, so Terraform locals act as the single source of truth. Pick the IDs in the VLAN ID planner first; it tells you which IDs in your intended range are still free before you commit them to a variable map.

locals {
  homelab_vlans = {
    "vlan20-srv"    = 20
    "vlan30-dmz"    = 30
    "vlan40-iot"    = 40
    "vlan100-admin" = 100
  }

  rpi_port_mapping = {
    "ether6" = 20 # RPi 4B #1 (Keepalived Node A)
    "ether7" = 20 # RPi 4B #2 (Keepalived Node B)
  }

  proxmox_port = "ether5"
}

This map drives the entire Layer 2 setup. To add a “Guest” VLAN, add it to homelab_vlans, and Terraform generates the VLAN interface, gateway IP, DHCP server, and bridge VLAN entry for it.

1. The Core Bridge & Ports

Since RouterOS v6.41, the recommended layout is a single bridge for all ports, with segmentation done purely through VLAN filtering. That keeps hardware offloading available wherever your switch chip supports it.

resource "routeros_interface_bridge" "core_bridge" {
  name           = "bridge1"
  vlan_filtering = true
  comment        = "Core bridge managed by Terraform"
}

Assigning the Physical Ports

Next, attach the physical Ethernet ports to the bridge. This is where each port becomes an access port (untagged) or a trunk port (tagged).

The Proxmox Trunk: The Proxmox server (ether5) receives tagged traffic so its VMs can sit in different VLANs. It stays on the default pvid = 1. This port carries VLANs by explicit tag, so the PVID only matters for the rare untagged frame, and nothing on this trunk is meant to talk over untagged VLAN 1 anyway.

resource "routeros_interface_bridge_port" "proxmox_port" {
  bridge    = routeros_interface_bridge.core_bridge.name
  interface = local.proxmox_port
  pvid      = 1
  hw        = true
  comment   = "Proxmox Trunk (Tagged VLANs)"
}

The Edge Access Ports: Devices that don’t handle VLAN tags (a standard PC, a Raspberry Pi) get a specific pvid. The bridge strips the VLAN tag on egress to these ports and adds it on ingress.

resource "routeros_interface_bridge_port" "rpi_ports" {
  for_each  = local.rpi_port_mapping
  bridge    = routeros_interface_bridge.core_bridge.name
  interface = each.key
  pvid      = each.value
  hw        = true
  comment   = "Keepalived Node"
}

resource "routeros_interface_bridge_port" "mgmt_port" {
  bridge    = routeros_interface_bridge.core_bridge.name
  interface = "ether2"
  pvid      = 100
  hw        = true
  comment   = "Admin Workstation Access Port"
}

2. Creating the VLAN Interfaces

A bridge connects physical ports, but the router itself needs an IP address in each VLAN to act as the default gateway. That takes one VLAN interface per VLAN on top of bridge1, generated with for_each from the locals block:

resource "routeros_interface_vlan" "vlans" {
  for_each  = local.homelab_vlans
  interface = routeros_interface_bridge.core_bridge.name
  name      = each.key
  vlan_id   = each.value
}

# Assign Gateway IPs to the Router
resource "routeros_ip_address" "vlan_ips" {
  for_each  = local.homelab_vlans
  address   = "10.0.${each.value}.1/24"
  interface = routeros_interface_vlan.vlans[each.key].name
}

3. The Magic: Dynamic Bridge VLAN Matrix

This is the hardest part of MikroTik configuration, and where Terraform helps most. The bridge has to be told explicitly which ports may carry which VLANs.

If bridge1 itself is missing from the tagged list, traffic never reaches the router’s CPU, and DHCP and routing for that VLAN fail entirely.

The Admin VLAN Override

First, define the Admin VLAN (vlan100) explicitly, tagged on the CPU (core_bridge) and the Proxmox trunk.

resource "routeros_interface_bridge_vlan" "vlan100_mgmt" {
  bridge   = routeros_interface_bridge.core_bridge.name
  vlan_ids = [100]
  tagged = [
    routeros_interface_bridge.core_bridge.name,
    local.proxmox_port
  ]
  # Note: ether2 is dynamically added as untagged by its PVID
}

The Dynamic Matrix Loop

The remaining VLANs come from one loop. It computes the untagged ports by reading local.rpi_port_mapping and selecting every port whose PVID matches the VLAN.

resource "routeros_interface_bridge_vlan" "vlan_matrix" {
  # Loop through all VLANs EXCEPT 100 (since we handled it above)
  for_each = { for k, v in local.homelab_vlans : k => v if v != 100 }

  bridge   = routeros_interface_bridge.core_bridge.name
  vlan_ids = [each.value]

  tagged = [
    routeros_interface_bridge.core_bridge.name,
    local.proxmox_port
  ]

  untagged = [
    for port, vlan in local.rpi_port_mapping : port if vlan == each.value
  ]
}

Why This Scales

Look at the untagged logic. To move a Raspberry Pi from the Server VLAN (20) to the DMZ VLAN (30), you don’t touch the bridge configuration, the matrix, or the interface settings.

You change 20 to 30 in local.rpi_port_mapping at the top of the file. Terraform computes the diff, changes the port’s PVID, and updates the bridge VLAN table in the same apply.

Next: L3 Firewall Rules

As code, a fragile, click-heavy configuration becomes a reviewable, auditable state. Proxmox gets its tagged trunk, edge devices get their access ports, and the router keeps L2 isolation between VLANs. With the VLAN layer in place, the next step is L3 firewall rules on top: default-deny forward chains, micro-segmented allow rules, and NAT for public-facing services.

Define the desired state, let the provider compute the diff. That declarative loop is what makes enterprise Azure deployments auditable, and the same loop runs here on your edge router.

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.