📖 Scenario: You are setting up a basic Terraform project to manage cloud infrastructure. The root module is the main folder where Terraform configuration files live. It defines resources and variables for your infrastructure.
🎯 Goal: Build a simple Terraform root module that defines a provider and a resource with variables.
📋 What You'll Learn
Create a Terraform configuration file named main.tf.
Define the aws provider with a region variable.
Create a resource of type aws_s3_bucket with a bucket name variable.
Use variables for region and bucket name with default values.
💡 Why This Matters
🌍 Real World
Terraform root modules are the starting point for managing cloud infrastructure as code. They organize resources and variables in one place.
💼 Career
Understanding root modules is essential for cloud engineers and DevOps professionals to build and maintain infrastructure reliably and repeatably.
Progress0 / 4 steps
1
Create the Terraform provider block
In the main.tf file, write a Terraform provider block for aws using the variable region for the region setting.
Terraform
Hint
The provider block tells Terraform which cloud provider to use. Use var.region to get the region from a variable.
2
Add variables for region and bucket name
Below the provider block in main.tf, add two variable blocks named region and bucket_name. Set their default values to "us-east-1" and "my-terraform-bucket" respectively.
Terraform
Hint
Variables let you customize your Terraform configuration. Use variable "name" { default = "value" } syntax.
3
Create an S3 bucket resource using the bucket_name variable
Add a resource block of type aws_s3_bucket named my_bucket in main.tf. Set the bucket attribute to use the variable bucket_name.
Terraform
Hint
Resources define infrastructure components. Use resource "type" "name" { attribute = value } syntax.
4
Add output to show the bucket name
Add an output block named bucket_name_output in main.tf that outputs the value of the aws_s3_bucket.my_bucket.bucket attribute.
Terraform
Hint
Outputs let you see important information after Terraform applies changes. Use output "name" { value = ... } syntax.
Practice
(1/5)
1. What is the root module in Terraform?
easy
A. A Terraform provider plugin
B. A special Terraform resource type
C. A remote backend configuration
D. The main folder where Terraform commands are run and resources are defined
Solution
Step 1: Understand the root module role
The root module is the main folder where you write your Terraform code and run commands.
Step 2: Differentiate from other concepts
It is not a resource type, backend, or provider plugin but the main working directory.
Final Answer:
The main folder where Terraform commands are run and resources are defined -> Option D
Quick Check:
Root module = main Terraform folder [OK]
Hint: Root module is your main Terraform folder [OK]
Common Mistakes:
Confusing root module with provider or backend
Thinking root module is a resource type
Assuming root module is a plugin
2. Which command must be run inside the root module folder to initialize Terraform?
easy
A. terraform apply
B. terraform init
C. terraform plan
D. terraform validate
Solution
Step 1: Identify the initialization command
terraform init sets up the working directory by downloading providers and preparing the backend.
Step 2: Confirm command location
This command must be run inside the root module folder where the Terraform files exist.
Final Answer:
terraform init -> Option B
Quick Check:
Initialize Terraform = terraform init [OK]
Hint: Use 'terraform init' to start in root module folder [OK]
A. It can call the network module inside the modules folder
B. It must be empty to work correctly
C. It cannot use modules from subfolders
D. It only contains variables and no resources
Solution
Step 1: Understand root module and submodules
The root module can call other modules located in subfolders like modules/network.
Step 2: Evaluate options
It can call the network module inside the modules folder correctly states the root module can call the network module. Other options are false.
Final Answer:
It can call the network module inside the modules folder -> Option A
Quick Check:
Root module calls submodules = true [OK]
Hint: Root module can call modules in subfolders [OK]
Common Mistakes:
Thinking root module cannot use submodules
Assuming root module must be empty
Believing root module only has variables
4. You run terraform apply in your root module folder but get an error: "No configuration files found". What is the likely cause?
medium
A. You ran the command outside the root module folder
B. You forgot to run terraform init first
C. Your Terraform version is outdated
D. You have no internet connection
Solution
Step 1: Analyze the error message
"No configuration files found" means Terraform cannot find .tf files in the current folder.
Step 2: Identify common cause
This usually happens if you run terraform apply outside the root module folder where the files exist.
Final Answer:
You ran the command outside the root module folder -> Option A
Quick Check:
Run commands inside root module folder [OK]
Hint: Run commands inside root module folder with .tf files [OK]
Common Mistakes:
Assuming init missing causes this error
Blaming Terraform version or internet
Not checking current folder location
5. You want to organize your Terraform project with a root module that calls two child modules: network and compute. Which folder structure and root module code is correct?
hard
A.
root/
main.tf
network.tf
compute.tf
# main.tf does not call modules