Stand up an Azure VM with Terraform, batteries included
An Azure virtual machine is never just one resource — it needs a network to live in. This builder generates the complete dependency chain (resource group, virtual network, subnet, NIC) plus the azurerm_linux_virtual_machine itself, with the cross-references already wired so terraform apply works first time.
How it works
Terraform creates Azure resources in dependency order using interpolated references. The VM references its network interface ID, the NIC references the subnet, the subnet references the virtual network, and everything references the resource group’s name and location. By chaining azurerm_resource_group.<id>.name style references, Terraform builds the correct graph and provisions resources in the right sequence.
For authentication, the recommended SSH path injects an admin_ssh_key block that reads your public key with file("~/.ssh/id_rsa.pub"). The password path sets disable_password_authentication = false and pulls the secret from a sensitive Terraform variable, so the credential never appears in your committed HCL.
Tips and notes
- Prefer SSH keys — they avoid password brute-forcing and don’t leak into state as readable strings.
- The OS image is pinned to Ubuntu 22.04 LTS gen2; change the
source_image_referenceblock if you need a different distro. - Choose
Premium_LRSfor production disks andStandard_LRSto minimise cost on dev VMs. - Add a network security group and public IP if the VM needs inbound access — this template keeps it internal-only by default.