Using terraform, how to create multiple resources of same type with unique and unidentical names using list/count for azure?

11,675

Solution 1

You just need to change the resource group block like this:

resource "azurerm_resource_group" "test" {
  count    = 2
  name     = element(var.resource_group_name, count.index)
  location = var.location
}

Solution 2

You can use the for_each syntax to create multiple resource of similar type. It requires a set (of unique values) to iterate over, hence convert your variable resource_group_name to set.

variable.tf

 variable "resource_group_name" {
  description = "Default resource group name that the network will be created in."
  type        = list(string)
  default     = ["asd-rg","asd2-rg"]

}



variable "location" {
  description = "The location/region where the core network will be created.
  default     = "westus"
}

main.tf

provider "azurerm" {
 features {}
}


resource "azurerm_resource_group" "test" {
  name     = each.value                       // value from iteration
  location = var.location
  for_each = toset(var.resource_group_name)   // convert list to set and iterate over it
}

Edit: variable can be of type string, list or map. This needs to be converted to set to be used with for_each

Share:
11,675

Related videos on Youtube

Atindra
Author by

Atindra

Updated on May 29, 2022

Comments

  • Atindra
    Atindra almost 2 years

    Here is a basic example for what I am trying to achieve. I have two files (main.tf) and (variable.tf), I want to create two resource groups and in the variables file is a list of names which I want the resource groups to occupy. First name of the first resource group and similarly going forward. So help me out on how to achieve it. I am using terraform v0.13.

    main.tf file:

    provider "azurerm" {
     features {}
    }
    
    
    resource "azurerm_resource_group" "test" {
      count    = 2
      name     = var.resource_group_name
      location = var.location
    }
    

    variable.tf file:

      variable "resource_group_name" {
      description = "Default resource group name that the network will be created in."
      type        = list
      default     = ["asd-rg","asd2-rg"]
    
    }
    
    
    
    variable "location" {
      description = "The location/region where the core network will be created.
      default     = "westus"
    }
    
  • Atindra
    Atindra over 3 years
    It worked fine, Thanks. Can I use the same approach for other resources such as subnets? For example if I have 3 subnets and I want them to have unique names, can I achieve that by this method? variable "subnet_name" {default = ["a","b","c"] } variable "subnet_address" { default=["10.0.1.0/26", "10.0.2.0/26", "10.0.3.0/26"]} . Will the first subnet automatically take the name and the address space ? Or does it require any further modifications.
  • Atindra
    Atindra over 3 years
    It is resulting in error. Invalid type specification for type =set and functions call not allowed while using toast(rg_names)
  • Charles Xu
    Charles Xu over 3 years
    @atindra Of course, yes. It will work as you expect.
  • Atindra
    Atindra over 3 years
    It is not working. unable to index subnets using subnet id with virtual machines and network interfaces.
  • Charles Xu
    Charles Xu over 3 years
    @atindra What do you mean by index the subnets using the subnet Id with virtual machines and network interfaces?
  • Atindra
    Atindra over 3 years
    I want to create different types of virtual machine, one may be a linux machine one maybe a windows vm and one maybe a sql vm and associate one subnet with each vm. So how to do this using the count feature, so that the code is also small and the infrastructure can be reused/modified as per need?
  • Atindra
    Atindra over 3 years
    Presently I am creating a list of subnet names and address and using them for same types of vm. Suppose I create 2 windows vm using count then I will use two subnet address and name in a single list for those 2 windows vm. But when I create a different type of vm supposedly sql vm, I create a different list for subnet addresses and name because unable to use the count function of the previous list in this vm.
  • shanmuga
    shanmuga over 3 years
    Fixed the syntax
  • Charles Xu
    Charles Xu over 3 years
    @atindra The count only helps you to create the VMs with the configuration most part the same. Unless you create the list such as ["Linux", "Linux","windows","windows"].