📖 Scenario: You are working on a Google Cloud Platform project where you need to create reusable infrastructure components. Using Terraform modules helps you avoid repeating code and makes your infrastructure easier to manage.
🎯 Goal: Build a simple Terraform module for creating a Google Cloud Storage bucket, then use that module in your main Terraform configuration to create a bucket with specific settings.
📋 What You'll Learn
Create a Terraform module with a variable for the bucket name
Use the module in the main Terraform configuration
Pass the bucket name to the module
Configure the bucket with versioning enabled
💡 Why This Matters
🌍 Real World
Terraform modules help cloud engineers reuse infrastructure code across projects, saving time and reducing errors.
💼 Career
Knowing how to create and use Terraform modules is a key skill for cloud infrastructure roles, enabling scalable and maintainable deployments.
Progress0 / 4 steps
1
Create the Terraform module folder and main.tf
Create a folder called gcs_bucket_module and inside it create a file main.tf with a resource google_storage_bucket named bucket. Set the bucket name to the variable bucket_name. Do not enable versioning yet.
GCP
Hint
Use resource "google_storage_bucket" "bucket" {} and inside set name = var.bucket_name.
2
Add a variable for the bucket name in the module
Inside the gcs_bucket_module folder, create a file variables.tf and define a variable called bucket_name of type string with no default value.
GCP
Hint
Use variable "bucket_name" { type = string } to declare the variable.
3
Use the module in the main Terraform configuration
In your main Terraform configuration file main.tf (outside the module folder), add a module block named my_bucket that uses the source ./gcs_bucket_module. Pass the bucket name my-unique-bucket-123 to the module variable bucket_name.
Enable versioning in the module's storage bucket resource
Modify the google_storage_bucket resource in gcs_bucket_module/main.tf to enable versioning by adding the block versioning { enabled = true } inside the resource.
module "storage" { source = "./modules/storage" } -> Option A
Quick Check:
Terraform module call = module "name" with source [OK]
Hint: Use 'module "name" { source = "path" }' to call modules [OK]
Common Mistakes:
Using keywords like call or import which are invalid
Missing quotes around module name
Not using a block structure for module
3. Given this Terraform module call:
module "network" {
source = "./modules/network"
region = "us-central1"
}
What will happen if the module network does not accept a region variable?
medium
A. Terraform will deploy but with a default region.
B. Terraform will ignore the extra variable and deploy successfully.
C. Terraform will throw an error about an unknown variable.
D. Terraform will deploy but skip the network module.
Solution
Step 1: Understand variable passing in modules
Terraform requires that variables passed to modules must be declared in the module's variables file.
Step 2: Identify behavior when unknown variable is passed
If a variable is passed but not declared, Terraform throws an error about unknown input variable.
Final Answer:
Terraform will throw an error about an unknown variable. -> Option C
Quick Check:
Unknown module variable = error [OK]
Hint: Modules must declare variables before use [OK]
Common Mistakes:
Assuming Terraform ignores unknown variables
Thinking Terraform uses defaults without declaration
Believing module is skipped silently
4. You have a module defined in ./modules/storage but when you run Terraform apply, it fails with: Error: Module not found What is the most likely cause?
medium
A. Terraform does not support modules in local folders.
B. The module code has syntax errors inside it.
C. You forgot to initialize the Terraform backend.
D. The source path in the module call is incorrect or misspelled.
Solution
Step 1: Understand the error meaning
The error 'Module not found' means Terraform cannot locate the module source path.
Step 2: Check common causes
The most common cause is a wrong or misspelled source path in the module call.
Final Answer:
The source path in the module call is incorrect or misspelled. -> Option D
Quick Check:
Module not found = wrong source path [OK]
Hint: Check module source path spelling if module not found error [OK]
Common Mistakes:
Assuming syntax errors cause 'Module not found'
Thinking Terraform disallows local modules
Confusing backend init with module loading
5. You want to create multiple GCP VM instances with similar settings using a module. Which approach best uses modules for reusability and scalability?
hard
A. Use a module only once and duplicate its code for other VMs.
B. Create a module for one VM and call it multiple times with different instance names.
C. Create separate modules for each VM with hardcoded settings.
D. Write all VM configurations directly in the main file without modules.
Solution
Step 1: Understand module reusability
Modules let you reuse the same configuration by calling them multiple times with different inputs.
Step 2: Identify scalable approach
Calling one VM module multiple times with different names is scalable and clean.
Final Answer:
Create a module for one VM and call it multiple times with different instance names. -> Option B
Quick Check:
Reuse module calls for multiple resources [OK]
Hint: Call one module multiple times with different inputs [OK]
Common Mistakes:
Duplicating module code instead of reusing
Hardcoding settings in separate modules
Avoiding modules and repeating code
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.