Lab Exercise: Automating Network Deployment with Terraform
In this final lab, you will use Terraform to automate the creation of a VNet and subnet as IaC, building on previous concepts for repeatable deployments.
- Install and initialize Terraform
- Write and apply a basic configuration
- Verify and destroy resources
- Active Azure subscription
- Azure Cloud Shell
- Local Setup: Terminal/Command Prompt
- Region: East US
- Resource Group:
terraform-net-rg
(created by Terraform)
- Create folder
terraform-lab
, addmain.tf
file. - Paste code:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "rg" {
name = "yourname-terraform-net-rg"
location = "East US"
}
resource "azurerm_virtual_network" "vnet" {
name = "terraform-vnet"
address_space = ["10.2.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_subnet" "subnet" {
name = "default"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.2.1.0/24"]
}
NOTE: Replace
yourname
inyourname-terraform-net-rg
with your actual name
- Open Azure Cloud Shell and switch to bash terminal
- Upload
main.tf
from theterraform-lab
folder you created in Step 1 - Run the following commands:
mkdir terraform-lab
mv main.tf terraform-lab/main.tf
cd terraform-lab
- Run
terraform init
. - Run
terraform plan
.
- Run
terraform apply --auto-approve
.
Verify in Portal
- Check for
terraform-net-rg
with VNet and subnet. - Run
terraform destroy --auto-approve
to clean up. - Delete the
rg-yourname-dns-lab
resource group you created in Lab 7
- Why use IaC like Terraform?
- How does this differ from portal deployments?
- What providers could you add for multi-cloud?