How to call value from one module in terraform to another module

Step 1-

Define Outputs in the Source Module: First, you need to define outputs in the module from which you want to export data.

modules/module1/outputs.tf:

output "output_name" {
  description = "Description of the output value"
  value       = local.some_value_or_variable
}

Step 2 –

Call the Module in the Main Configuration:

main.tf:

module "module1" {
  source = "./modules/module1"
  // ... other input variables
}

Step 3

Access the Output from Another Module: If you want to use the output from module1 in another module (module2 for example), you'd reference it in the main configuration where you call module2:

main.tf:

module "module2" {
  source  = "./modules/module2"
  input_variable = module.module1.output_name
}

Step 4

Define the Input Variable in the Destination Module: In the module where you want to use the passed value, define a corresponding input variable.

modules/module2/variables.tf:


variable "input_variable" {
  description = "Description of the input variable"
  type        = type_of_the_variable // e.g., string, number, map, etc.
}

Step 5

Use the Input Variable: Now, within module2, you can use var.input_variable to access the value passed from module1.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x