Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Terraform create Azure Virtual Network subnets from map of object and show name in the header

This is a second part and little update to Using element function with count meta argument example Terraform Azure subnets. After submitting PR (Pull Request), reviewer asked they want small change in the output how it shows on the console. As I am using count to loop through subnet address prefixes on the each subnet name it shows number in the header iteration. They wanted me to make change and show the name of the subnet instead of count numbers.

Microsoft Azure Terraform hashcorp hcl azurerm resource manager virtual network for loop count will be created address prefixes private endpointprivate link.png

Below is the output after changing the configuration file code in Terraform. Here how it looks like.

Microsoft Azure resource manager terraform provider subnet address prefixes enforce private link endpoint private network policies virtual network subnets configuration tf file resource group.png

In the below code earlier I was using list for address prefixes inside variable subnet. After feedback I am simply used map object in for_each loop to iterate through it.

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      #version = "=2.91.0"
    }
  }
}

# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}
}

variable "resource_group" {
  type        = string
  default     = "vcloud-lab.com"
  description = "Azure Virtual Network"
}

variable "virtual_network" {
  type        = string
  default     = "vcloud_lab_global_vnet01"
  description = "Azure Virtual Network"
}

variable "subnets" {
  type = map(object({
    address_prefix = string
  }))
  default = {
    subnet-1 = { address_prefix = "10.0.1.0/24" }
    subnet-2 = { address_prefix = "10.0.2.0/24" }
    subnet-3 = { address_prefix = "10.0.3.0/24" }
  }
  description = "vNet Subnet list"
}

data "azurerm_resource_group" "rginfo" {
  name = var.resource_group
}

data "azurerm_virtual_network" "vnetinfo" {
  name               = var.virtual_network
  resource_group_name  = data.azurerm_resource_group.rginfo.name
}

resource "azurerm_subnet" "name" {
  for_each            = var.subnets
  name                 = each.key
  resource_group_name  = data.azurerm_resource_group.rginfo.name
  virtual_network_name = data.azurerm_virtual_network.vnetinfo.name
  address_prefixes     = [each.value.address_prefix]
}

output "subnet_ids" {
  value = { for k, v in azurerm_subnet.name : k => v.id }
}

Download this script Terraform_functions_Azure_Subnet_Resource_Header_Name.tf here or it is also available on github.com.

Below is the new output makes more sense now when I look into output.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
terraform apply --auto-approve
data.azurerm_resource_group.rginfo: Reading...
data.azurerm_resource_group.rginfo: Read complete after 0s [id=/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/vcloud-lab.com]
data.azurerm_virtual_network.vnetinfo: Reading...
data.azurerm_virtual_network.vnetinfo: Read complete after 1s [id=/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/vcloud_lab_global_vnet01]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # azurerm_subnet.name["subnet-1"] will be created
  + resource "azurerm_subnet" "name" {
      + address_prefixes                               = [
          + "10.0.1.0/24",
        ]
      + default_outbound_access_enabled                = true
      + enforce_private_link_endpoint_network_policies = (known after apply)
      + enforce_private_link_service_network_policies  = (known after apply)
      + id                                             = (known after apply)
      + name                                           = "subnet-1"
      + private_endpoint_network_policies              = (known after apply)
      + private_endpoint_network_policies_enabled      = (known after apply)
      + private_link_service_network_policies_enabled  = (known after apply)
      + resource_group_name                            = "vcloud-lab.com"
      + virtual_network_name                           = "vcloud_lab_global_vnet01"
    }

  # azurerm_subnet.name["subnet-2"] will be created
  + resource "azurerm_subnet" "name" {
      + address_prefixes                               = [
          + "10.0.2.0/24",
        ]
      + default_outbound_access_enabled                = true
      + enforce_private_link_endpoint_network_policies = (known after apply)
      + enforce_private_link_service_network_policies  = (known after apply)
      + id                                             = (known after apply)
      + name                                           = "subnet-2"
      + private_endpoint_network_policies              = (known after apply)
      + private_endpoint_network_policies_enabled      = (known after apply)
      + private_link_service_network_policies_enabled  = (known after apply)
      + resource_group_name                            = "vcloud-lab.com"
      + virtual_network_name                           = "vcloud_lab_global_vnet01"
    }

  # azurerm_subnet.name["subnet-3"] will be created
  + resource "azurerm_subnet" "name" {
      + address_prefixes                               = [
          + "10.0.3.0/24",
        ]
      + default_outbound_access_enabled                = true
      + enforce_private_link_endpoint_network_policies = (known after apply)
      + enforce_private_link_service_network_policies  = (known after apply)
      + id                                             = (known after apply)
      + name                                           = "subnet-3"
      + private_endpoint_network_policies              = (known after apply)
      + private_endpoint_network_policies_enabled      = (known after apply)
      + private_link_service_network_policies_enabled  = (known after apply)
      + resource_group_name                            = "vcloud-lab.com"
      + virtual_network_name                           = "vcloud_lab_global_vnet01"
    }

Plan: 3 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + subnet_ids = {
      + subnet-1 = (known after apply)
      + subnet-2 = (known after apply)
      + subnet-3 = (known after apply)
    }
azurerm_subnet.name["subnet-2"]: Creating...
azurerm_subnet.name["subnet-3"]: Creating...
azurerm_subnet.name["subnet-1"]: Creating...
azurerm_subnet.name["subnet-1"]: Creation complete after 7s [id=/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/vcloud_lab_global_vnet01/subnets/subnet-1]
azurerm_subnet.name["subnet-2"]: Still creating... [10s elapsed]
azurerm_subnet.name["subnet-3"]: Still creating... [10s elapsed]
azurerm_subnet.name["subnet-2"]: Creation complete after 14s [id=/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/vcloud_lab_global_vnet01/subnets/subnet-2]
azurerm_subnet.name["subnet-3"]: Still creating... [20s elapsed]
azurerm_subnet.name["subnet-3"]: Creation complete after 20s [id=/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/vcloud_lab_global_vnet01/subnets/subnet-3]

Apply complete! Resources: 3 added, 0 changed, 0 destroyed.

Outputs:

subnet_ids = {
  "subnet-1" = "/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/vcloud_lab_global_vnet01/subnets/subnet-1"
  "subnet-2" = "/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/vcloud_lab_global_vnet01/subnets/subnet-2"
  "subnet-3" = "/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/vcloud-lab.com/providers/Microsoft.Network/virtualNetworks/vcloud_lab_global_vnet01/subnets/subnet-3"
}

Useful Articles
Terraform clone virtual machine template in VMware vSphere vCenter from CSV file
Terraform error retrieving storage account failure responding to request StatusCode 404 StorageAccountNotFound The storage account was not found
Terraform testing local variables and output csv file without resource Part 1
Terraform testing variable map object values without resource configuration part 2
Terraform foreach module output to show only required results
Terraform deploy create A Private DNS Record in Microsoft Azure from list of objects
Terraform clone virtual machine template in VMware vSphere vCenter Dynamic Content Part 2
Creating a Private Endpoint for Azure Storage Account with required sub services using Terraform
Terraform Azure Create Private Endpoint to existing Storage Account with Custom Private DNS zone record link

Go Back

Comment

Blog Search

Page Views

12176521

Follow me on Blogarama