Menu

Virtual Geek

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

Using element function with count meta argument example Terraform Azure subnets

I had a task to deploy multiple subnets to a Azure Virtual Network (vNet) using Hashicorp Terraform HCL. I wanted to test and use element() function combination with count meta argument to do the simple POC with less code and automation. Here is snapshot of my Virtual Network's Subnets view in the portal after deploying subnet resources.

Microsoft Azure Terraform AWS amazon web services google cloud platform infrastructure as a code iac hashicorp ibm terraform hcl virtual network subnet element count meta argument functions.png

To use element with count, I have defined list(any) type in Subnets variable. In the count I am using terraform function length() of subnets to get number. Subnet names will be made automatically and they all will be unique. Each subnet name is a combination of string and respective count.index with iteration (function sum() is be used to addition number 1 in count index). Next in the address_prefixes I am using element() with count index to iterate through list of subnets.

Download this script Terraform_functions_Azure_Subnet_Element_Count.tf here or it will be available in github.com.

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        = list(any)
  default     = ["10.0.1.0/24", "10.0.2.0/24", "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" {
  count                = length(var.subnets)
  name                 = "subnet-${sum([count.index, 1])}"
  resource_group_name  = data.azurerm_resource_group.rginfo.name
  virtual_network_name = data.azurerm_virtual_network.vnetinfo.name
  address_prefixes     = ["${element(var.subnets, count.index)}"]
}

Below is the output on the console after applying terraform configuration to Azure Virtual Network.

 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
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 0s [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[0] 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[1] 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[2] 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.
azurerm_subnet.name[0]: Creating...
azurerm_subnet.name[2]: Creating...
azurerm_subnet.name[1]: Creating...
azurerm_subnet.name[0]: 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[2]: Still creating... [10s elapsed]
azurerm_subnet.name[1]: Still creating... [10s elapsed]
azurerm_subnet.name[1]: Creation complete after 13s [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[2]: 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.

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

Go Back

Comment

Blog Search

Page Views

12278625

Follow me on Blogarama