Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124

If youโve been working with Azure, you know the drill: log into the portal, click around, set configurations, and repeat every time you need a new resource. It worksโbut itโs not scalable, and itโs prone to human error.
Enter Terraform, one of the most popular Infrastructure-as-Code (IaC) tools out there. With Terraform, you define what you want in simple configuration files, and Terraform takes care of how to build it in Azure. No more guesswork, no more repetitive clicking.
In this post, weโll walk through:
Letโs get started ๐
Terraform is an open-source IaC tool from HashiCorp that lets you:
.tf).Unlike ARM templates or Bicep, which are Azure-native, Terraform is cloud-agnostic. That means you can use the same tool to manage Azure, AWS, Google Cloud, Kubernetes, and more.
For teams that run multi-cloud or hybrid environments, Terraform is often the go-to choice.
Before we can deploy anything, letโs get Terraform and Azure ready.
Download Terraform from the official site, and verify the installation:
terraform -v
You should see the version number printed out.
Terraform talks to Azure through the Azure CLI, so make sure you have it installed:
az --version
Login to your Azure account:
az login
A browser window will pop up. After logging in, Terraform can use your credentials.
Letโs deploy something simple: an Azure Resource Group and a Virtual Machine.
Create a new folder and inside it a file called main.tf.
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "my_rg" {
name = "terraform-demo-rg"
location = "East US"
}
resource "azurerm_virtual_network" "my_vnet" {
name = "terraform-demo-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.my_rg.location
resource_group_name = azurerm_resource_group.my_rg.name
}
resource "azurerm_subnet" "my_subnet" {
name = "terraform-demo-subnet"
resource_group_name = azurerm_resource_group.my_rg.name
virtual_network_name = azurerm_virtual_network.my_vnet.name
address_prefixes = ["10.0.1.0/24"]
}
resource "azurerm_network_interface" "my_nic" {
name = "terraform-demo-nic"
location = azurerm_resource_group.my_rg.location
resource_group_name = azurerm_resource_group.my_rg.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.my_subnet.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_linux_virtual_machine" "my_vm" {
name = "terraform-demo-vm"
resource_group_name = azurerm_resource_group.my_rg.name
location = azurerm_resource_group.my_rg.location
size = "Standard_B1s"
admin_username = "azureuser"
network_interface_ids = [
azurerm_network_interface.my_nic.id,
]
admin_password = "P@ssw0rd1234!"
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
}
Now the fun partโletโs run Terraform commands:
terraform init
This downloads the Azure provider and sets up your working directory.
terraform plan
This shows what Terraform will createโlike a โdry run.โ
terraform apply
Confirm with yes, and Terraform provisions the resource group, VNet, subnet, NIC, and VM.
Head to the Azure Portal, refresh the Resource Groups, and youโll see terraform-demo-rg with all your deployed resources. ๐
With Terraform, you gain:
Whether youโre deploying a simple VM or a full-scale production environment, Terraform + Azure gives you the power of automation and flexibility of code.