0
0
Terraformcloud~5 mins

Why modules enable reusability in Terraform - Why It Works

Choose your learning style9 modes available
Introduction
When building infrastructure, repeating the same code wastes time and can cause mistakes. Modules let you write code once and use it many times, making your work faster and safer.
When you need to create the same type of server or resource multiple times with small differences.
When you want to share infrastructure code with your team to keep things consistent.
When you want to organize your code into smaller, easy-to-understand parts.
When you want to update many resources at once by changing just one module.
When you want to avoid copying and pasting code, which can cause errors.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
}

module "web_server" {
  source = "./modules/web_server"
  server_name = "example-web"
  server_port = 8080
}

output "web_server_ip" {
  value = module.web_server.server_ip
}

This file uses a module called web_server from the modules/web_server folder.

The server_name and server_port are inputs to customize the module.

The output web_server_ip shows the IP address created by the module.

Commands
This command sets up Terraform and downloads the module code so it can be used.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... 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.
This command shows what Terraform will do before making any changes. It helps you check that the module is used correctly.
Terminal
terraform plan
Expected OutputExpected
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.web_server.aws_instance.example will be created + resource "aws_instance" "example" { + ami = "ami-12345678" + instance_type = "t2.micro" + tags = { + "Name" = "example-web" } } Plan: 1 to add, 0 to change, 0 to destroy.
This command creates the resources defined in the module without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
module.web_server.aws_instance.example: Creating... module.web_server.aws_instance.example: Creation complete after 10s [id=i-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skip manual approval to apply changes immediately
This command shows the IP address of the server created by the module.
Terminal
terraform output web_server_ip
Expected OutputExpected
54.123.45.67
Key Concept

If you remember nothing else from this pattern, remember: modules let you write infrastructure code once and reuse it many times to save effort and avoid mistakes.

Common Mistakes
Copying and pasting the same resource code instead of using a module
This causes duplicated code that is hard to update and can lead to errors.
Create a module for the repeated resource and call it with different inputs.
Not running 'terraform init' after adding a module
Terraform won't download the module code, causing errors when planning or applying.
Always run 'terraform init' after adding or changing modules.
Forgetting to pass required input variables to the module
Terraform will fail because the module needs those inputs to create resources.
Check the module's input variables and provide values when calling the module.
Summary
Use modules to write reusable infrastructure code once and call it multiple times.
Run 'terraform init' to download module code before planning or applying.
Use 'terraform plan' to check what changes will happen, then 'terraform apply' to create resources.
Use outputs from modules to get information about created resources.