Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to output name of a resource from module in terraform

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading