0
0
Terraformcloud~5 mins

Dependency inversion with modules in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
When building infrastructure with Terraform, you often want to reuse code and keep your setup organized. Dependency inversion with modules helps by letting you create small reusable pieces that depend on abstract inputs rather than concrete details. This makes your infrastructure easier to manage and change.
When you want to reuse the same infrastructure setup in different environments like development and production.
When you need to change parts of your infrastructure without rewriting the whole setup.
When you want to share common infrastructure code across multiple projects.
When you want to clearly separate responsibilities in your infrastructure code.
When you want to avoid tight coupling between different parts of your infrastructure.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
}

module "network" {
  source = "./modules/network"
  cidr_block = "10.0.0.0/16"
}

module "web_server" {
  source = "./modules/web_server"
  subnet_id = module.network.subnet_id
}

This main Terraform file uses two modules: network and web_server. The network module creates a network with a CIDR block. The web_server module depends on the network module by receiving the subnet ID as input. This shows dependency inversion because the web server module does not create the network itself but depends on the network module's output.

Commands
This command initializes the Terraform working directory. It downloads the modules and sets up the backend to manage state.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.
This command shows what Terraform will do when applying the configuration. It helps verify that the modules and their dependencies are set up correctly.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # module.network.aws_vpc.main will be created + resource "aws_vpc" "main" { + cidr_block = "10.0.0.0/16" + id = (known after apply) } # module.web_server.aws_subnet.main will be created + resource "aws_subnet" "main" { + vpc_id = (known after apply) + id = (known after apply) } Plan: 2 to add, 0 to change, 0 to destroy.
This command applies the planned changes and creates the infrastructure. The -auto-approve flag skips manual confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_vpc.main: Creating... aws_vpc.main: Creation complete after 3s [id=vpc-12345678] aws_subnet.main: Creating... aws_subnet.main: Creation complete after 2s [id=subnet-87654321] Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply without asking for confirmation
This command shows the subnet ID output from the network module, proving the dependency inversion works by passing this value to the web server module.
Terminal
terraform output subnet_id
Expected OutputExpected
subnet-87654321
Key Concept

If you remember nothing else from this pattern, remember: modules should depend on inputs and outputs, not on each other's internal details.

Common Mistakes
Hardcoding resource IDs inside modules instead of passing them as inputs.
This creates tight coupling and makes modules less reusable and harder to maintain.
Pass resource IDs and other dependencies as input variables to modules.
Not defining outputs in modules to share important values.
Without outputs, other modules cannot receive necessary information, breaking dependencies.
Define outputs in modules for any values that other modules need.
Calling resources directly across modules instead of using outputs and inputs.
Terraform modules are isolated; direct resource references across modules cause errors.
Use module outputs and input variables to connect modules.
Summary
Initialize Terraform to download modules and set up the environment.
Use terraform plan to preview changes and verify module dependencies.
Apply the configuration to create resources with terraform apply.
Use terraform output to check values passed between modules.