I have been trying to figure out how to output the "name" parameter of a resource in Terraform so I can then use this in other modules. This is my best attempt so far:
resource "azurerm_resource_group" "rg_infrastructure" {
name = "rg_${var.environment}_Infrastructure"
location = var.location
}
output "rg_infrastructureName" {
value = module.rg_infrastructure.name # <- How to get value of "name" from above resource group into here.
}
I have tried multiple variations of this. I can offcourse make it work if I write "rg_${var.environment}_Infrastructure" twice, but the whole point is I dont want to as then there is a chance of a missmatch.
What am i missing?
>Solution :
If you are inside a module’s code, you don’t reference values via module.whatever you just reference the values directly. Your output should be:
output "rg_infrastructureName" {
value = azurerm_resource_group.rg_infrastructure.name
}
Now, outside your module, you would declare your module and then access the module’s output values like this:
module "module_name" {
source = "./module_source"
# Other module attributes
}
resource "some_other_resource" "resource_name" {
infraName = module.module_name.rg_infrastructureName
}
Note that the output is referenced as module.module_name.rg_infrastructureName where module_name is whatever you named the module when you declared it.