0
0
Terraformcloud~30 mins

Dependency inversion with modules in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Dependency inversion with modules
📖 Scenario: You are building a Terraform configuration to deploy cloud infrastructure. You want to organize your code using modules so that your main configuration depends on module outputs, not on internal details. This approach helps keep your code clean and flexible.
🎯 Goal: Create a Terraform project with a root module and a child module. The root module will use the child module's output to configure a resource, demonstrating dependency inversion by relying on module outputs instead of internal variables.
📋 What You'll Learn
Create a child module with a variable and an output
Create a root module that calls the child module
Use the child module's output in the root module to configure a resource
Follow Terraform best practices for module usage and dependency inversion
💡 Why This Matters
🌍 Real World
Organizing Terraform code into modules with clear inputs and outputs is a common practice in real-world cloud infrastructure projects. It helps teams manage complexity and reuse code.
💼 Career
Understanding dependency inversion with Terraform modules is important for cloud engineers and DevOps professionals to build scalable, maintainable infrastructure as code.
Progress0 / 4 steps
1
Create the child module with a variable and output
Create a Terraform module in a folder called child_module. Inside it, create a file variables.tf with a variable called instance_name of type string. Then create a file outputs.tf with an output called instance_name_output that outputs the value of the variable instance_name.
Terraform
Need a hint?

Define a variable block with the exact name instance_name and type string. Then define an output block named instance_name_output that returns var.instance_name.

2
Create the root module and call the child module
In the root module, create a main.tf file. Add a module block named child that uses the source ./child_module. Pass the string "my-instance" to the module variable instance_name.
Terraform
Need a hint?

Use a module block with the exact name child. Set source to ./child_module and pass instance_name = "my-instance".

3
Use the child module output in the root module resource
In the root module main.tf, add a resource block of type null_resource named example. Set the resource's triggers argument to a map with a key instance_name and value from the child module output module.child.instance_name_output.
Terraform
Need a hint?

Create a null_resource named example. Inside, set triggers to a map with key instance_name and value module.child.instance_name_output.

4
Add output in root module to expose child module output
In the root module main.tf, add an output block named root_instance_name that outputs the value of module.child.instance_name_output.
Terraform
Need a hint?

Define an output block named root_instance_name that returns module.child.instance_name_output.