VOOZH about

URL: https://thenewstack.io/how-to-manage-a-home-network-with-infrastructure-as-code/

⇱ How to Manage a Home Network with Infrastructure as Code - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2020-02-06 13:38:41
How to Manage a Home Network with Infrastructure as Code
contributed,sponsor-hashicorp,sponsored,sponsored-post-contributed,
Edge Computing / Networking / Security

How to Manage a Home Network with Infrastructure as Code

How one engineer used Terraform to manage a complex home network.
Feb 6th, 2020 1:38pm by Paul Tyng
👁 Featued image for: How to Manage a Home Network with Infrastructure as Code
Feature image from Pixabay.
HashiCorp sponsored this post.
Paul Tyng
Paul is an engineering manager at HashiCorp and based in Baltimore, MD. He has been working in software engineering for over 20 years and still cannot figure out printers.

I decided to take advantage of some downtime I had during the holiday break to overhaul my home network. As I began my project, I bought a Unifi Dream Machine home management device because, in addition to my personal and guest SSIDs, there is an apartment in my house for which I wanted to segment traffic. I also wanted to add an extra layer of security around some of the home automation and IoT devices that were being added to our home network with a fourth SSID.

I started to configure the new network, I had started a spreadsheet of VLANs, subnet CIDRs and mappings of those to SSIDs. Additionally, I needed to track firewall rules, port forwards and other settings and configurations. Needless to say, this was a lot of information to maintain and manage.

My day job is working on the Infrastructure-as Code (IaC) product HashiCorp Terraform, an open source tool for automating and codifying infrastructure provisioning. Syncing a spreadsheet of data to a web UI by clicking around in my browser just felt tedious and error-prone — I was bound to make some mistakes. There had to be a better way, so I decided that this was an area where the automation and versioning features of Terraform could help.

My Search for Terraform Integrations

My first step was just to see what solutions were out there. Others have to have this problem, right? How do they solve it? Maybe there already was a Terraform provider out there!

My first search for “terraform unifi” brought a few potentially promising results. A module for spinning up a controller on Google cloud  and a repository called “terraform-provider-unifi.” Here we go! Unfortunately, once I clicked through, it looked like the repository only had README and LICENSE files, no code yet, so someone else had the same idea but hadn’t made much progress yet.

👁 Image

If no Terraform provider existed, maybe at least there was a Go SDK, which would provide a way for me to talk to the API in the Go programming language. Go is the language Terraform providers are typically implemented in and the one I have been primarily using since joining HashiCorp, I thought. I did find a few on GitHub, but they were entirely read-only, while others seemed to just not have many create, update or delete methods or were otherwise lacking in functionality.

There were SDKs in other languages, namely a PHP one that seemed to be the most popular. The Ubiquiti Community Wiki also has some documentation on the Unifi controller API, but without links to existing SDK packages, so it looked like I was going to have to write an SDK myself.

Infrastructure enables innovation. HashiCorp provides consistent workflows to provision, secure, connect, and run any infrastructure for any application.
Learn More
The latest from HashiCorp

Writing an SDK

While investigating the existing SDKs, I did find some information that would be useful for writing my own. Specifically, I found a precedent for the authentication flow, but in order to find how the create, update and delete methods worked, I needed to start collecting examples via the development tools in my browser.

I started with three of the Unifi data types I wanted to manage in the provider:

  1. Wireless networks (referred to as WLANs in the API).
  2. Networks (used to implement VLANs).
  3. Clients (referred to as users in the API.)

Once I grabbed the example list, get, create, update and delete methods, I was able to find the structure of the JSON messages, the HTTP methods and the URL patterns for the requests. With those pieces of data, I could write the SDK in Go using the standard library’s net/http package.

However, I soon realized doing this manually for every API endpoint would be extremely tedious. In a previous job, I had experimented with extracting message-type information from a .jar file and a Java .class file and then generating Go structs for the JSON serialization. Since the Unifi controller is written in Java, I was curious if this strategy would work.

Unfortunately, the .jar file is obfuscated and I have not found a way to get the information I need from its .class files. However, inside the .jar file there were .json files (here is an example for WLANs) that had all the fields listed for every endpoint. I could thus use that to at least help ensure my SDK implementation had all the data elements the server could accept.

Writing the Provider

Now that I had an SDK, it was time to write (and test) the provider. A Terraform provider is the integration between Terraform and another library or application, most commonly this is a cloud API like AWS, Azure, or Google Cloud, but it can be written for any API (even for ordering pizza). The code was relatively straightforward — I work on Terraform in my role at HashiCorp and have created multiple integrations in the past (like the New Relic one).

I started with the template repository that HashiCorp maintains. In addition to the template, there is a large section of the Terraform docs site dedicated to writing custom Terraform providers. We also have some videos and other resources on our site that talk through the process.

I typically start by creating Terraform config files that are examples of how I’d like to interact with the API  and then from there create the resources to support those files, but there are many ways to approach this. In some cases, where we are granularly exposing an upstream API, we start from the API definitions and try to translate those as faithfully as possible.

Testing the Provider

Once I had a provider, I needed to be able to test it end to end. In the beginning, I just tested it live on my own network controller. However, this had a number of downsides:

  • Provisioning to some equipment on my own network caused the Access Points to reset their radios, which caused my laptop to disconnect from WiFi, interrupting the tests and forcing me to switch to hard-wired.
  • If you counted above, you’ll notice I have four SSIDs and at the time I thought the max per controller was four (I later found out this is the max per WLAN group), but for a while, I just deleted one and made sure my test concurrency didn’t go above one.
  • It was tested in production, what could go wrong?

Testing against my own network also had some other downsides that we experience in other Terraform providers, for example, it would make it very difficult to run tests against pull requests or difficult for contributors to run their own tests. I dug around a little and found some docker containers with the controller inside that I could swap to and after setting up a GitHub action, I now get end to end testing on all my code on GitHub.

Writing the Configuration for My Network

After I had a functional, tested provider, it was relatively straightforward to describe my VLANs, SSIDs, fixed IPs and other networking configuration options in Terraform. This is what it looks like to create a VLAN and map it to an SSID for your Unifi controller:

👁 Image

This was a good opportunity to take advantage of several new features released in last year’s Terraform 0.12 version. There were two specifically that I wanted to try out: csvdecode and resource for _each. When mapping MAC address to notes and fixed IPs, I thought this would be much easier to describe in a CSV:

👁 Image

To map this into the unifi_client resource, the code would look like:

👁 Image

You can see I use locals to simplify the CSV decoding and convert the data to map with the MAC address as the key. This makes iterating with a for_each more straightforward as each item has a unique key to identify its resource.

Importing the Existing Infrastructure

Now that I had written all the config that described my network, I didn’t want to just create it all again from scratch. I instead wanted to test it against my existing infrastructure to make sure I captured everything necessary. When I implemented the provider, I also implemented the terraform import functionality for each resource if you specify the ID from Unifi. This gets a little tricky though as the IDs are sometimes not apparent in the web UI. Occasionally you can grab them from a URL. For example in this URL:

https://unifi.ubnt.com/5.12.22.0/unifi/site/default/v2/settings/wifi/manage/edit/700cc880b9d3a0b50aab0636

The ID is the last path segment that looks like a 24-digit hexadecimal number. If they are not available in the URL, you have to grab them directly from the API requests in dev tools.

What’s Next?

So what’s next for the provider and my home network? One thing I’d like to do is to add support for WebRTC to the provider. This is how the Unifi web portal is able to communicate with your local controller without opening a port in your firewall. It may not be possible, but it’s something I’d like to investigate. Additionally, I’d like to add full coverage to the provider to manage all the pieces of the Unifi controller via Terraform. Of course, I’m also going to need to buy a lot more Unifi networking gear to test it all.

Infrastructure enables innovation. HashiCorp provides consistent workflows to provision, secure, connect, and run any infrastructure for any application.
Learn More
The latest from HashiCorp
TRENDING STORIES
Paul is an engineering manager at HashiCorp and based in Baltimore, MD. He has been working in software engineering for over 20 years and still cannot figure out printers.
Read more from Paul Tyng
HashiCorp sponsored this post.
SHARE THIS STORY
TRENDING STORIES
AWS and New Relic are sponsors of The New Stack.
TNS owner Insight Partners is an investor in: Pragma.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.