Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to crate multiple resources in with name_unique #71

Open
bugcy013 opened this issue Nov 30, 2021 · 7 comments
Open

How to crate multiple resources in with name_unique #71

bugcy013 opened this issue Nov 30, 2021 · 7 comments

Comments

@bugcy013
Copy link

Hi Team,

I like to create multiple resources with name_unique method, When I try apply I can see one resouce_group created .

module "naming" {
  source  = "Azure/naming/azurerm"
  suffix  = ["prod", "app1"]
  unique-include-numbers = true
}

resource "azurerm_resource_group" "example" {
  name     = module.naming.resource_group.name_unique
  location = "West Europe"
}


resource "azurerm_resource_group" "example1" {
  name     = module.naming.resource_group.name_unique
  location = "West Europe"
}
@derekschauland
Copy link

It would be great if there was some way to loop over the naming module to generate this. I am currently trying to pass names into a module to create subnets, but am unable to create multiple subnets because there is only one name generated.

@davidmonk-cbts
Copy link

Kind of defeats the purpose of unique_name, I think.

@sturlabragason-devoteam
Copy link

sturlabragason-devoteam commented Apr 9, 2024

You could try to abstract something from this with a locals block. Each gives a separate unique name.

module "naming" {
  source = "Azure/naming/azurerm"
}

module "naming2" {
  source = "Azure/naming/azurerm"
}

resource "azurerm_storage_account" "main" {
  name                          = module.naming.storage_account.name_unique
  resource_group_name           = azurerm_resource_group.main.name
  location                      = azurerm_resource_group.main.location
  account_tier                  = "Standard"
  account_replication_type      = "LRS"
  tags = local.tags
}

resource "azurerm_storage_account" "main2" {
  name                          = module.naming2.storage_account.name_unique
  resource_group_name           = azurerm_resource_group.main.name
  location                      = azurerm_resource_group.main.location
  account_tier                  = "Standard"
  account_replication_type      = "LRS"
  tags = local.tags
}

@davidmonk-cbts
Copy link

Not seeing a locals block in your example. What you show is multiple module calls to naming. It makes sense how this works actually, however just using this module in general, while helping to standardize consistently, gives an excessive number of resources in the end.

@Arnab-Developer
Copy link

Arnab-Developer commented May 25, 2024

You could try to abstract something from this with a locals block. Each gives a separate unique name.

@sturlabragason-devoteam did you mean something like this?

terraform {
  required_version = ">= 1.8.3"

  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "3.103.1"
    }
  }

  cloud {
    organization = "[org name]"

    workspaces {
      name = "[workspace name]"
    }
  }
}

provider "azurerm" {
  features {}
}

locals {
  application   = "terraproj1"
  environment   = "dev"
  storage_count = 3
}

module "naming" {
  count = local.storage_count

  source = "Azure/naming/azurerm"
  suffix = [local.application, local.environment]
}

resource "azurerm_resource_group" "main" {
  name     = module.naming[0].resource_group.name
  location = var.location
}

resource "azurerm_storage_account" "main" {
  count = local.storage_count

  name                     = module.naming[count.index].storage_account.name_unique
  account_tier             = var.storage_type
  account_replication_type = var.replication_type
  resource_group_name      = azurerm_resource_group.main.name
  location                 = azurerm_resource_group.main.location
}

Now let's say I need to create another 5 new web apps, then I need another 5 new naming modules! This will not be a good solution. I agree with @davidmonk-cbts that this solution will create excessive number of resources.

I think it will be better if the name_unique gives unique name every time it is being called.

module "naming" {
  source = "Azure/naming/azurerm"
  suffix = ["terraproj1", "dev"]
}

resource "azurerm_storage_account" "storage_1" {
  name                     = module.naming.storage_account.name_unique // stterraproj1devty54
  account_tier             = var.storage_type
  account_replication_type = var.replication_type
  resource_group_name      = azurerm_resource_group.main.name
  location                 = azurerm_resource_group.main.location
}

resource "azurerm_storage_account" "storage_2" {
  name                     = module.naming.storage_account.name_unique // stterraproj1devrj4v
  account_tier             = var.storage_type
  account_replication_type = var.replication_type
  resource_group_name      = azurerm_resource_group.main.name
  location                 = azurerm_resource_group.main.location
}

@Arnab-Developer
Copy link

Arnab-Developer commented Jun 9, 2024

I did some more investigation with this and found that, it is not possible to return unique name every time the name_unique being used because at the time of module import the unique name has been generated and that same unique name has been used every time it is being called. So, the only workaround which I can think right now is to append any string with the name for your resource.

module "naming" {
  source = "Azure/naming/azurerm"
  suffix = ["terraproj1", "dev"]
}

resource "azurerm_storage_account" "storage_1" {
  name                     = "${module.naming.storage_account.name}storage1"
  account_tier             = var.storage_type
  account_replication_type = var.replication_type
  resource_group_name      = azurerm_resource_group.main.name
  location                 = azurerm_resource_group.main.location
}

resource "azurerm_storage_account" "storage_2" {
  name                     = "${module.naming.storage_account.name}storage2"
  account_tier             = var.storage_type
  account_replication_type = var.replication_type
  resource_group_name      = azurerm_resource_group.main.name
  location                 = azurerm_resource_group.main.location
}

@Egoorbis
Copy link

I had the same issues with aiming to create dedicated nsgs per subnet. I used count as a workaround and the appened count-index to the name. Maybe this will work for you as well

module "naming" {
 source  = "Azure/naming/azurerm"
 version = "~> 0.3"
}

resource "azurerm_resource_group" "aks-rg" {
 name     = module.naming.resource_group.name_unique
 location = var.location
}

module "avm-res-network-networksecuritygroup" {
 count               = 2
 source              = "Azure/avm-res-network-networksecuritygroup/azurerm"
 version             = "0.2.0"
 resource_group_name = azurerm_resource_group.aks-rg.name
 name                = "${module.naming.network_security_group.name}-${count.index}"
 location            = azurerm_resource_group.aks-rg.location
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants