Assignment 1
main.tf
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 5.9.0"
}
}
}
provider "azurerm" {
features {}
}
Create Resource Group
resource "azurerm_resource_group" "rg-lb" {
name = "rg-terraform-lab-westus-lb"
location = "West US 3"
}
Create Virtual Network
resource "azurerm_virtual_network" "vnet" {
name = "vnet-terraform-lab-westus-lb"
location = azurerm_resource_group.rg-lb.location
resource_group_name = azurerm_resource_group.rg-lb.name
address_space = ["10.20.0.0/16"]
}
Create two subnets using count
resource "azurerm_subnet" "subnets" {
count = length(var.subnet_cidrs)
name = "subnet-${count.index + 1}"
resource_group_name = azurerm_resource_group.rg-lb.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = [var.subnet_cidrs[count.index]]
}
variables.tf
variable "subnet_cidrs" {
description = "CIDR ranges for Azure subnets"
type = list(string)
}
terraform.tfvars
subnet_cidrs = [
"10.20.1.0/24",
"10.20.2.0/24"
]
Assignment 2:
main.tf
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 5.9.0"
}
}
}
provider "azurerm" {
features {}
}
1. Create Azure Resource Group
resource "azurerm_resource_group" "rg-lb" {
name = "rg-terraform-nsg-westusi-lb"
location = "West US 3"
}
2. Create Network Security Group
resource "azurerm_network_security_group" "nsg" {
name = "nsg-terraform-eastus"
location = azurerm_resource_group.rg-lb.location
resource_group_name = azurerm_resource_group.rg-lb.name
}
3. Create inbound rules dynamically using for_each
resource "azurerm_network_security_rule" "inbound" {
for_each = var.inbound_rules
name = each.key
priority = each.value.priority
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = ""
destination_port_range = tostring(each.value.port)
source_address_prefix = var.my_public_ip_cidr
destination_address_prefix = ""
resource_group_name = azurerm_resource_group.rg-lb.name
network_security_group_name = azurerm_network_security_group.nsg.name
}
Top comments (0)