Terraform Azure — Hands-On Assignments
Assignment 1: Create Multiple Azure Subnets Using count
Objective: Learn Terraform's count meta-argument.
Requirements:
- Create one Azure Resource Group in
eastus. - Create one Virtual Network with CIDR
10.20.0.0/16. - Use a
list(string)input variable containing["10.20.1.0/24", "10.20.2.0/24"]. - Use the
countmeta-argument to create two subnets namedsubnet-1andsubnet-2. - Both subnets must belong to the same Virtual Network.
Submission: Write the complete, standalone Terraform code and paste it into the comment section.
Assignment 2: Create Multiple Azure NSG Rules Using for_each
Objective: Learn Terraform's for_each meta-argument with a map variable.
Requirements:
- Create one Azure Resource Group in
eastus. - Create one Azure Network Security Group.
- Define a
mapinput variable containing two inbound rules: SSH (22, priority 100) and RDP (3389, priority 110). - Use the
for_eachmeta-argument to create both NSG rules dynamically. - Allow inbound connections only from your own public IP address using
/32.
Submission: Write the complete, standalone Terraform code and paste it into the comment section.
Instructions for Both Assignments
- Each assignment must have independent, working Terraform code.
- Use AzureRM provider 5.9.x.
- Do not use data sources or modules.
- Execute
terraform init,terraform validate, andterraform plan. - Submit your complete code, not just the
countorfor_eachblock. - Do not include real passwords or credentials in your submission.
Top comments (11)
`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]]
}`
`# 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
}`
assignment #1:
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]]
}
`
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"]`
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"
}`