-
Notifications
You must be signed in to change notification settings - Fork 116
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
Comments
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. |
Kind of defeats the purpose of unique_name, I think. |
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
}
|
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. |
@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 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
} |
I did some more investigation with this and found that, it is not possible to return unique name every time the 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
} |
I had the same issues with aiming to create dedicated nsgs per subnet. I used
|
Hi Team,
I like to create multiple resources with
name_unique
method, When I try apply I can see one resouce_group created .The text was updated successfully, but these errors were encountered: