Back to portfolio

Reusable Azure Terraform Network Module

Defining the same network topology for development and staging from one module.

Situation

Development and staging needed the same network layout with different address ranges. Copying resource definitions across the two would make future changes easy to miss.

Task

I needed one reusable module and a test that could check its planned topology without Azure credentials.

Action

I made the network name, resource group, address space, and subnet map configurable. I used the subnet names as resource keys and created one network security group and one association for each subnet. I kept the Azure default rules as the baseline for this example.

I wrote a two-environment example that calls the same module for development and staging. Each environment supplies a distinct address range and has separate application and data subnets. This puts the settings that vary between environments in the caller and keeps the resource definitions in one place.

I wrote a Terraform plan test with a mocked AzureRM provider. I asserted that two subnets get two security group associations and that the application subnet retains its requested CIDR. I added validation and the test to GitHub Actions.

What I would improve

I would add CIDR overlap and containment checks, support explicit security rules, and test a plan against a disposable Azure resource group.

Result

One module serves two environment configurations. The mocked plan confirmed two network security group associations and the expected application subnet CIDR. Terraform validation and the test passed in CI without deploying resources.