Debug School

Terraform Azure — Hands-On Assignments - Day 1

Rajesh Kumar on October 12, 2026

Terraform Azure — Hands-On Assignments Assignment 1: Create Multiple Azure Subnets Using count Objective: Learn Terraform's c...
Collapse
 
sakshi_gadekar profile image
Sakshi Gadekar •

`resource "azurerm_resource_group" "lab" {
name = "rg-count-subnets-lab"
location = "eastus"
}

resource "azurerm_virtual_network" "lab" {
name = "vnet-count-subnets-lab"
location = azurerm_resource_group.lab.location
resource_group_name = azurerm_resource_group.lab.name

address_space = ["10.20.0.0/16"]
}

resource "azurerm_subnet" "lab" {
count = length(var.subnet_address_prefixes)

name = "subnet-${count.index + 1}"
resource_group_name = azurerm_resource_group.lab.name
virtual_network_name = azurerm_virtual_network.lab.name

address_prefixes = [var.subnet_address_prefixes[count.index]]
}`

Collapse
 
lingamgunta_sreelakshmi_f profile image
Lingamgunta Sreelakshmi • • Edited
1.`main.tf`

# STRING - Azure region and VM settings
variable "azure_region" {
  description = "Azure region for every resource in this lab."
  type        = string
}

# LIST(STRING) - ordered Azure network address prefixes
variable "vnet_address_spaces" {
  description = "Address spaces configured on the Azure virtual network."
  type        = list(string)
}

variable "subnet_address_prefixes" {
  description = "Address prefixes configured on the VM subnet."
  type        = list(string)
}

# MAP(STRING) - stable names of the existing Azure resources
variable "resource_names" {
  description = "Resource names from Labs 1-4. Keep unchanged to retain state."
  type        = map(string)
}

# --------------------------------------------
# 1. Resource Group
# --------------------------------------------
resource "azurerm_resource_group" "lab" {
  name     = var.resource_names["resource_group"]
  location = var.azure_region
}
# --------------------------------------------
# 2. Virtual Network
# --------------------------------------------
resource "azurerm_virtual_network" "lab" {
  name                = var.resource_names["virtual_network"]
  location            = azurerm_resource_group.lab.location
  resource_group_name = azurerm_resource_group.lab.name

  address_space = var.vnet_address_spaces
}
# --------------------------------------------
# 3. Subnet
# --------------------------------------------
resource "azurerm_subnet" "vms" {
  name                 = var.resource_names["subnet"]
  resource_group_name  = azurerm_resource_group.lab.name
  virtual_network_name = azurerm_virtual_network.lab.name

  address_prefixes = var.subnet_address_prefixes
}


Enter fullscreen mode Exit fullscreen mode
Collapse
 
rishikesh_thite_8859d2092 profile image
Rishikesh Thite • • Edited
# ============================================
# Assignment One
# ============================================

# Terraform configuration
terraform {
  required_version = ">= 1.15.0, < 2.0.0"

  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 5.9.0"
    }
  }
}

# Azure provider config
provider "azurerm" {
  features {}
}

# Resource Group
resource "azurerm_resource_group" "rg" {
  name     = "rg-terraform-lab-centralindia-rt-test"
  location = "centralindia"
}

# Virtual Network
resource "azurerm_virtual_network" "vnet" {
  name                = "vnet-terraform-lab-rt-test"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  address_space = ["10.20.0.0/16"]
}

# subnet CIDRs
variable "subnet_cidrs" {
  type = list(string)

  default = [
    "10.20.1.0/24",
    "10.20.2.0/24"
  ]
}

# Subnets using count
resource "azurerm_subnet" "subnet" {
  count = length(var.subnet_cidrs)

  name                 = "subnet-${count.index + 1}"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.vnet.name

  address_prefixes = [
    var.subnet_cidrs[count.index]
  ]
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
yash_sarnobat_31 profile image
Yash Sarnobat • • Edited
# Assignment 1 - main.tf

variable "vnet_address_spaces" {
  description = "Azure virtual network"
  type        = string
  default     = "10.20.0.0/16"
}

variable "subnet_address_prefixes" {
  description = "Azure subnets"
  type        = list(string)
  default     = ["10.20.1.0/24", "10.20.2.0/24"]
}

resource "azurerm_resource_group" "assign1" {
  name     = "rg-assign1-ys"
  location = "eastus"
}

resource "azurerm_virtual_network" "vnet" {
  name                = "assign1-vnet"
  location            = azurerm_resource_group.assign1.location
  resource_group_name = azurerm_resource_group.assign1.name

  address_space = [var.vnet_address_spaces]
}

resource "azurerm_subnet" "sub" {
  count = length(var.subnet_address_prefixes)

  name                 = "subnet-${count.index + 1}"
  resource_group_name  = azurerm_resource_group.assign1.name
  virtual_network_name = azurerm_virtual_network.vnet.name

  address_prefixes = [var.subnet_address_prefixes[count.index]]
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
sakshi_gadekar profile image
Sakshi Gadekar •

`# Resource Group
resource "azurerm_resource_group" "sakshi_lab" {
name = "rg-sakshi-foreach-nsg-lab"
location = "eastus"
}

Network Security Group

resource "azurerm_network_security_group" "sakshi_lab" {
name = "nsg-sakshi-foreach-lab"
location = azurerm_resource_group.sakshi_lab.location
resource_group_name = azurerm_resource_group.sakshi_lab.name
}

Create SSH and RDP rules dynamically using for_each

resource "azurerm_network_security_rule" "sakshi_rules" {
for_each = var.nsg_rules

name = "sakshi-${each.value.name}"
priority = each.value.priority
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = ""
destination_port_range = tostring(each.value.port)
source_address_prefix = var.allowed_source_cidr
destination_address_prefix = "
"
resource_group_name = azurerm_resource_group.sakshi_lab.name
network_security_group_name = azurerm_network_security_group.sakshi_lab.name
}`

Collapse
 
khuma_pokhare_d9fdc896ef9 profile image
Khuma Pokhare • • Edited
**variables.tf **

variable "cidr_range" {
  description = "The CIDR range for the network"
  type        = list(string)
}




variable "location" {
  description = "The location for the resource group"
  type        = string
  default     = "West US 3"
}

variable "subnet_names" {
  description = "The names of the subnets"
  type        = list(string)
}


terraform.tfvars
subnet_names = ["subnet-1", "subnet-2"]

cidr_range = ["10.20.1.0/24", "10.20.2.0/24"]


**main.tf**


resource "azurerm_resource_group" "khuma_assignment_rg" {
  name     = "khuma-assignment-rg"
  location = var.location
}

resource "azurerm_virtual_network" "khuma_assignment_vnet" {
  name                = "khuma-assignment-vnet"
  address_space       = ["10.20.0.0/16"]
  location            = var.location
  resource_group_name = azurerm_resource_group.khuma_assignment_rg.name


}

resource "azurerm_subnet" "khuma_subnet" {
  count                = 2
  name                 = var.subnet_names[count.index]
  resource_group_name  = azurerm_resource_group.khuma_assignment_rg.name
  virtual_network_name = azurerm_virtual_network.khuma_assignment_vnet.name
  address_prefixes     = [var.cidr_range[count.index]]
}



Enter fullscreen mode Exit fullscreen mode
Collapse
 
tariq_alghawanmeh_68388ac profile image
tariq alghawanmeh • • Edited

assignment #1:

terraform {
  required_version = ">= 1.15.0, < 2.0.0"

  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 5.9.0"
    }
  }
}

provider "azurerm" {
  features {}
}

# Requirement 1: Resource Group
resource "azurerm_resource_group" "lab" {
  name     = "tg-assignment1"
  location = "westus3"
}

# Requirement 2: Virtual Network
resource "azurerm_virtual_network" "lab" {
  name                = "tg-assignment1-vnet"
  location            = azurerm_resource_group.lab.location
  resource_group_name = azurerm_resource_group.lab.name
  address_space       = ["10.20.0.0/16"]
}

# Requirement 3:
variable "subnet_address_prefixes" {
  description = "Address prefixes for the subnets."
  type        = list(string)
}

resource "azurerm_subnet" "vms" {
  name                 = var.resource_names["subnet"]
  resource_group_name  = azurerm_resource_group.lab.name
  virtual_network_name = azurerm_virtual_network.lab.name

  address_prefixes = var.subnet_address_prefixes
}

variable "create_instance" {
  description = "This is to enable windows vm creation."
  type   = bool
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
gauri_thorat_c64a5d5bdfa0 profile image
Gauri Thorat •

terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "5.9.0"
}

}
}

provider "azurerm" {
# Configuration options
features {}
}

variable "subnet_prefixes" {
type = list(string)

default = [
"10.20.1.0/24",
"10.20.2.0/24"
]
}

Resource Group

resource "azurerm_resource_group" "rg" {
name = "rg-count-demo"
location = "East US"
}

Virtual Network

resource "azurerm_virtual_network" "vnet" {
name = "vnet-count-demo"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
address_space = ["10.20.0.0/16"]
}

Subnets created using count

resource "azurerm_subnet" "subnet" {
count = length(var.subnet_prefixes)

name = "subnet-${count.index + 1}"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = [var.subnet_prefixes[count.index]]
}

Collapse
 
diptarag profile image
Diptarag Saha •

`
main.tf

Resource Group

resource "azurerm_resource_group" "rg" {
name = "rg-batman"
location = "West US 3"
}

Virtual Network

resource "azurerm_virtual_network" "vnet" {
name = "vnet-batman"
address_space = ["10.20.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}

variable.tf
resource "azurerm_subnet" "subnet" {
count = length(var.subnet_prefixes)
name = count.index == 0 ? "subnet-1" : "subnet-2"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = [var.subnet_prefixes[count.index]]
}

terraform.tfvars
subnet_prefixes = ["10.20.1.0/24", "10.20.2.0/24"]`

Collapse
 
swathi_endla_9c9db9977db3 profile image
Swathi Endla •

Assignment-1
main.tf
`variable "azure_region" {
description = "Azure region for every resource in this lab."
type = string
}
variable "vnet_address_spaces" {
description = "Address spaces configured on the Azure virtual network."
type = list(string)
}
variable "subnet_address_prefixes" {
description = "Address prefixes configured on the VM subnet."
type = list(string)
}

resource "azurerm_resource_group" "lab" {
name = var.resource_names["resource_group"]
location = var.azure_region
}
resource "azurerm_virtual_network" "lab" {
name = var.resource_names["virtual_network"]
location = azurerm_resource_group.lab.location
resource_group_name = azurerm_resource_group.lab.name

address_space = var.vnet_address_spaces
}
resource "azurerm_subnet" "vms" {
count = var.create_instance?2:0
name = var.resource_names["subnet"]-${(length(var.subnet_address_prefixes).index)+1}
resource_group_name = azurerm_resource_group.lab.name
virtual_network_name = azurerm_virtual_network.lab.name

address_prefixes = var.subnet_address_prefixes
}

==========================

STRING

azure_region = "West US 3"

LIST(STRING): ordered collections

vnet_address_spaces = ["10.20.0.0/16"]
subnet_address_prefixes = ["10.20.1.0/24","10.20.2.0/24"]

BOOL

create_instance = true

MAP(STRING): resource names must match the previous labs exactly.

resource_names = {
resource_group = "rg-terraform-lab-eastus"
virtual_network = "vnet-terraform-lab"
subnet = "subnet"
}`

Collapse
 
anirudha_ingole_9c8e0ffd0 profile image
Anirudha ingole •

STRING - Azure region and VM settings
variable "azure_region" {
description = "Azure region for every resource in this lab."
type = string
}

LIST(STRING) - ordered Azure network address prefixes

variable "vnet_address_spaces" {
description = "Address spaces configured on the Azure virtual network."
type = list(string)
}

variable "subnet_address_prefixes" {
description = "Address prefixes configured on the VM subnet."
type = list(string)
}

MAP(STRING) - stable names of the existing Azure resources

variable "resource_names" {
description = "Resource names from Labs 1-4. Keep unchanged to retain state."
type = map(string)
}

--------------------------------------------

1. Resource Group

--------------------------------------------

resource "azurerm_resource_group" "lab" {
name = var.resource_names["resource_group"]
location = var.azure_region
}

--------------------------------------------

2. Virtual Network

--------------------------------------------

resource "azurerm_virtual_network" "lab" {
name = var.resource_names["virtual_network"]
location = azurerm_resource_group.lab.location
resource_group_name = azurerm_resource_group.lab.name

address_space = var.vnet_address_spaces
}

--------------------------------------------

3. Subnet

--------------------------------------------

resource "azurerm_subnet" "vms" {
name = var.resource_names["subnet"]
resource_group_name = azurerm_resource_group.lab.name
virtual_network_name = azurerm_virtual_network.lab.name

address_prefixes = var.subnet_address_prefixes
}