Terraform Azure — Hands-On Assignments
Assignment 1: Create Multiple Azure Subnets Using count
Objective: Learn Terraform's c...
For further actions, you may consider blocking this person and/or reporting abuse
`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"
}`
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
}