What if you could fix a problem once and have it fixed everywhere automatically?
Creating a child module in Terraform - Why You Should Know This
Imagine you have to build many similar parts of your cloud setup by copying and pasting the same code again and again.
Each time you want to change something, you must find and update every copy manually.
This manual way is slow and easy to mess up.
If you forget one place or make a typo, your cloud setup can break or behave unexpectedly.
It's like trying to fix many copies of a recipe instead of just one master recipe.
Creating a child module lets you write the code once and reuse it everywhere.
You can change the module in one place, and all uses update automatically.
This saves time, reduces mistakes, and keeps your cloud setup neat and organized.
resource "aws_instance" "web1" { ... } resource "aws_instance" "web2" { ... }
module "web_server" { source = "./child_module" count = 2 ... }
You can build complex cloud setups quickly by reusing tested building blocks.
A company needs 10 similar servers for different teams. Instead of writing code 10 times, they create one child module and deploy it 10 times with different settings.
Manual repetition is slow and error-prone.
Child modules let you reuse code easily.
One change updates all uses, saving time and mistakes.
Practice
Solution
Step 1: Understand module purpose
Child modules help organize infrastructure by splitting it into smaller parts.Step 2: Identify benefits
This makes code reusable and easier to manage across projects.Final Answer:
To break down infrastructure into smaller, reusable pieces -> Option BQuick Check:
Child modules = reusable infrastructure parts [OK]
- Thinking child modules increase resource count in root
- Believing child modules remove need for variables
- Assuming child modules limit to one cloud provider
./modules/network?Solution
Step 1: Identify correct block type
Child modules are included using themoduleblock in Terraform.Step 2: Check syntax for source
Thesourceattribute points to the child module folder path.Final Answer:
module "network" { source = "./modules/network" } -> Option DQuick Check:
Use module block with source path [OK]
- Using resource or provider blocks instead of module
- Placing source inside resource or variable blocks
- Incorrect block type for including modules
module "web" {
source = "./modules/webserver"
instance_count = 3
}And the child module
variables.tf contains:variable "instance_count" {
type = number
}What will be the value of
var.instance_count inside the child module?Solution
Step 1: Check variable passing
The root module passesinstance_count = 3to the child module.Step 2: Confirm variable usage
The child module declaresinstance_countvariable to receive this value.Final Answer:
3 -> Option AQuick Check:
Passed variable value = 3 [OK]
- Assuming default or null if variable is declared
- Confusing variable names or missing passing
- Thinking child module variables reset automatically
module "db" {
source = "./modules/database"
db_name = "prod"
}But Terraform shows an error:
An argument named "db_name" is not expected here. Did you mean to define a variable named "db_name"?What is the likely cause?
Solution
Step 1: Analyze error message
The error indicates that the argument "db_name" is not expected, which happens when the child module does not declare a variable named "db_name".Step 2: Check child module variables
If child module does not declarevariable "db_name", Terraform cannot accept the passed value.Final Answer:
The child module does not declare a variable named db_name -> Option CQuick Check:
Passing undeclared argument causes "not expected" error [OK]
- Ignoring variable declaration in child module
- Assuming source path error without checking variables
- Thinking modules can't receive variables
variables.tf and main.tf snippets inside the child module is the best practice?Solution
Step 1: Check variable types and defaults
Bucket name should be string without default to force user input. Versioning is boolean with default true.Step 2: Validate resource usage
Resource usesvar.bucket_nameandvar.versioningcorrectly with boolean type for versioning enabled.Final Answer:
variables.tf with string and bool (default true), main.tf using these variables correctly -> Option AQuick Check:
Correct types and defaults = variables.tf:variable "bucket_name" { type = string } variable "versioning" { type = bool, default = true }
main.tf:resource "aws_s3_bucket" "bucket" { bucket = var.bucket_name versioning { enabled = var.versioning } }[OK]
- Using string type for boolean variables
- Setting wrong default types
- Using number type for bucket name
Ready to prove it?
Pass the test and this pattern moves unknown
