Skip to main content

On This Page

Terraform Modules: Refactoring Azure VM Deployments for Reusability

2 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

Terraform Basics – Week 6: Building and Using Your First Terraform Module

Ozan Guner introduces Terraform modules to refactor Azure VM deployments, reducing code duplication by centralizing infrastructure logic. The article details a module that encapsulates VM, NIC, and NSG resources for reuse across environments.

Why This Matters

Terraform’s root module manages all resources directly, leading to duplication and inconsistency when scaling. Without modules, teams must replicate resource blocks for each environment (dev, staging, production), increasing maintenance costs and error risk. For example, updating an NSG rule requires changes across ten VM configurations, risking drift. Modules solve this by centralizing logic, ensuring consistency and reducing redundant code.

Key Insights

  • “Terraform modules eliminate code duplication across environments, as shown in the Azure VM refactoring example.” (https://dev.to/ozanguner/terraform-basics-week-6-building-and-using-your-first-terraform-module-3k9m)
  • “Sagas over ACID for e-commerce” – not applicable here; instead, modules use input/output variables to decouple root and child configurations.
  • “Temporal used by Stripe, Coinbase” – not applicable; the article highlights Terraform modules as a reusable pattern for infrastructure-as-code.

Working Example

# modules/vm/main.tf
resource "azurerm_network_security_group" "nsg" {
  name                = var.nsg_name
  location            = var.rg_location
  resource_group_name = var.rg_name
}

resource "azurerm_network_interface" "nic" {
  name                = var.nic_name
  location            = var.rg_location
  resource_group_name = var.rg_name
  subnet_id           = var.subnet_id
  security_group_id   = azurerm_network_security_group.nsg.id
}

resource "azurerm_virtual_machine" "vm" {
  name                  = var.vm_name
  location              = var.rg_location
  resource_group_name   = var.rg_name
  network_interface_ids = [azurerm_network_interface.nic.id]
  vm_size               = var.vm_size
  admin_username        = var.admin_username
  admin_password        = var.admin_password
}
# modules/vm/variables.tf
variable "rg_name" {}
variable "rg_location" {}
variable "subnet_id" {}
variable "nsg_name" {}
variable "nic_name" {}
variable "vm_name" {}
variable "vm_size" {}
variable "admin_username" {}
variable "admin_password" {}
# modules/vm/outputs.tf
output "vm_id" {
  value = azurerm_virtual_machine.vm.id
}
output "nic_name" {
  value = azurerm_network_interface.nic.name
}
# root/main.tf
module "vm" {
  source = "./modules/vm"
  rg_name            = azurerm_resource_group.prod.name
  rg_location        = azurerm_resource_group.prod.location
  subnet_id          = azurerm_subnet.prod.id
  nsg_name           = "vm-nsg"
  nic_name           = "vm-nic"
  vm_name            = "example-vm"
  vm_size            = "Standard_DS1_v2"
  admin_username     = "adminuser"
  admin_password     = "P@ssw0rd"
}

Practical Applications

  • Use Case: Azure VM deployments using Terraform modules to standardize configurations across dev, staging, and production.
  • Pitfall: Hardcoding resource group names in modules leads to configuration drift when environments change.

References:

Continue reading

Next article

Terraform Data Source: Bridging Existing and Managed Infrastructure

Related Content