TerraformPilot

DevOps

Terraform for SONiC: Open Network Operating System

Automate SONiC switches with Terraform: REST/gNMI, Azure-style fabric automation, and disaggregated network OS fleets.

LLuca Berton1 min read

SONiC (Software for Open Networking in the Cloud) is the open-source NOS born at Microsoft Azure, now in production at many hyperscalers and growing in enterprise. SONiC has no single "official" Terraform provider — most teams drive it via gNMI/RESTCONF using community providers or wrap config_db.json rendering with Terraform templates.

Templated config_db.json

#
locals {
  config_db = {
    DEVICE_METADATA = {
      localhost = {
        hwsku    = "Accton-AS7726-32X"
        type     = "LeafRouter"
        bgp_asn  = "65001"
      }
    }
    INTERFACE = {
      "Ethernet0|10.0.0.1/31" = {}
    }
    BGP_NEIGHBOR = {
      "10.0.0.0" = {
        asn       = "65000"
        local_asn = "65001"
        rrclient  = "false"
      }
    }
  }
}
 
resource "local_file" "config_db" {
  content  = jsonencode(local.config_db)
  filename = "${path.module}/out/${var.hostname}-config_db.json"
}

Push via SSH (null_resource)

#
resource "null_resource" "push" {
  triggers = { hash = sha256(local_file.config_db.content) }
 
  connection {
    type        = "ssh"
    user        = "admin"
    host        = var.switch_ip
    private_key = file(var.ssh_key)
  }
 
  provisioner "file" {
    source      = local_file.config_db.filename
    destination = "/tmp/config_db.json"
  }
 
  provisioner "remote-exec" {
    inline = [
      "sudo cp /tmp/config_db.json /etc/sonic/config_db.json",
      "sudo config reload -y",
    ]
  }
}

gNMI Provider Alternative

#

For larger fleets, the community karimra/gnmic driver or vendor-specific gNMI providers expose SONiC paths declaratively. Check provider compatibility against your SONiC build.

Best Practices

#
  • Pin SONiC build (e.g., 202311) — yang models drift between major releases.
  • Render config_db.json from a single source of truth (Terraform locals or Jinja).
  • config save after each successful apply — startup config persists separately.
  • Validate with sonic-mgmt tests in CI before merging Terraform PRs.
#
#Terraform#SONiC#Open NOS#gNMI#Disaggregation

Share this article