5

I want to use output variables of one resource/module as an input to another resource/modules. Is that possible? Here i want the output value from 'outputs.tf' in root to be used as input in 'main.tf' of module.

    root
    |--main.tf
    |--vars.tf
    |--outputs.tf
    |---module
         |--main.tf
         |--vars.tf
9
  • Any more questions? Does it solve your problem? Commented Apr 1, 2020 at 7:11
  • This doesnt answer my question Commented Apr 2, 2020 at 14:51
  • So what is the issue you meet now? And can you share all the terraform files you use? Commented Apr 3, 2020 at 1:20
  • 1
    I wanted to use the output variables that are defined in output.tf file as an input to another resource..Is that possible? @CharlesXu Commented Apr 3, 2020 at 7:56
  • Of course yes. Do you read my answer carefully? I showed in it. Commented Apr 3, 2020 at 8:06

2 Answers 2

4

Of course you can. And there is nothing more you need to do. Just do it as usual. Here is an example:

main.tf
├── rg
│   ├── output.tf
│   └── rg.tf
└── vnet
    ├── output.tf
    └── vnet.tf

You create the modules rg and vnet as it does. Set the output you need. Here I set the output rg_name and rg_location. And I also set the variables rg_name and rg_location in the module vnet as need. Then the main.rf shows here:

provider "azurerm" {
  features {}
}

module "rg" {
  source = "./rg"

  rg_name = "charlesTerraform"
}

module "vnet" {
  source = "./vnet"

  rg_name = module.rg.rg_name
  rg_location = module.rg.rg_location
}

output "vnet" {
  value = module.vnet.vnet
}

You see, I use the output of the module rg as input for the module vnet. Hope it help you understand the Terraform modules.

Update:

It's also the same way when the structure is the thing you said. You just need to input the output you need into the module. For example:

resource "azurerm_resource_group" "example" {
  name = "xxxxxx"
  location = "xxxx"
}

module "vnet" {
  source = "./modules"

  resource_group = azurerm_resource_group.example.name
}

This is just an example, but it shows you how to achieve it. Hope you understand.

Sign up to request clarification or add additional context in comments.

3 Comments

@SiddhiMorajkar Hope you will try it soon and give the result.
@SiddhiMorajkar Well, you can accept it as the answer when it works for you.
Did it.! Thanks for the help
0

I think you can use the terraform output command to interpolate output value to other means as variables.

See this terraform docs https://www.terraform.io/cli/commands/output

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.