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

DevOps

MOTOSHARE 🚗🏍️
Turning Idle Vehicles into Shared Rides & Earnings

From Idle to Income. From Parked to Purpose.
Earn by Sharing, Ride by Renting.
Where Owners Earn, Riders Move.
Owners Earn. Riders Move. Motoshare Connects.

With Motoshare, every parked vehicle finds a purpose. Owners earn. Renters ride.
🚀 Everyone wins.

Start Your Journey with Motoshare

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
}Code language: JavaScript (javascript)

Step 2 –

Call the Module in the Main Configuration:

main.tf:

module "module1" {
  source = "./modules/module1"
  // ... other input variables
}Code language: JavaScript (javascript)

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
}Code language: PHP (php)

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.
}Code language: JavaScript (javascript)

Step 5

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

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x