π§ Introduction
When starting with Azure infrastructure as code (IaC), Terraform is one of the most powerful tools available. In this post, weβll walk through how to use Terraform to create a basic network setup on Azure β including a Resource Group, Virtual Network (VNet), and Subnet. This is an ideal starting point for beginners who want to automate infrastructure deployment.
π Project Structure
The project consists of four core Terraform files:
azure-terraform-infra-demo/
βββ main.tf # Main resource definitions
βββ variables.tf # Input variable declarations
βββ terraform.tfvars # Variable values
βββ outputs.tf # Output values
Each file serves a specific purpose and keeps the configuration modular and easy to manage.
π§ Terraform Configuration Breakdown
1. Resource Group
resource "azurerm_resource_group" "rg" {
name = var.resource_group_name
location = var.location
}
This creates a logical container for all other Azure resources.
Virtual Network
resource "azurerm_virtual_network" "vnet" {
name = "demo-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
Defines a virtual network in the specified resource group.Subnet
resource "azurerm_subnet" "subnet" {
name = "demo-subnet"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.0.1.0/24"]
}
Creates a subnet inside the virtual network.
π How to Deploy
Once you have all files ready, you can deploy the infrastructure using the following steps. Make sure you have the Azure CLI and Terraform installed locally.
Step 1: Login to Azure
az login
Step 2: Initialize Terraform
terraform init
Step 3: Preview the Plan
terraform plan
Step 4: Apply the Configuration
terraform apply
Type yes when prompted to confirm, and Terraform will begin creating the resources on Azure.
β
Conclusion
This simple project is a great starting point for anyone new to Terraform on Azure. By automating the creation of a Resource Group, Virtual Network, and Subnet, you can begin building more advanced infrastructure in a modular and repeatable way.
In future posts, I plan to share:
How to deploy Azure Virtual Machines with NSGs
How to create snapshots and managed images
Comparing Terraform and Bicep for infrastructure as code
Building multi-subnet architectures with internal routing
Thanks for reading! Feel free to βοΈ the GitHub repo or leave a comment if you found this helpful.
Top comments (0)