Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Terraform CLI Overview
📖 Scenario: You are working as a cloud engineer setting up infrastructure using Terraform. You want to learn how to use the Terraform CLI commands to initialize a project, plan changes, apply infrastructure, and check the state.
🎯 Goal: Build a simple Terraform configuration and use the Terraform CLI commands terraform init, terraform plan, terraform apply, and terraform state list step-by-step to manage infrastructure.
📋 What You'll Learn
Create a Terraform configuration file with a provider and a resource
Initialize the Terraform working directory with terraform init
Generate and review an execution plan with terraform plan
Apply the planned changes with terraform apply
List the resources in the Terraform state with terraform state list
💡 Why This Matters
🌍 Real World
Terraform CLI commands are used daily by cloud engineers to manage infrastructure as code safely and predictably.
💼 Career
Knowing how to use Terraform CLI is essential for roles in cloud infrastructure, DevOps, and site reliability engineering.
Progress0 / 4 steps
1
Create Terraform configuration file
Create a file named main.tf with the following content exactly: a provider block for hashicorp/aws with region us-east-1, and a resource block aws_s3_bucket named my_bucket with bucket name my-terraform-bucket-12345.
Terraform
Hint
Use the provider block to specify AWS and region. Then add a resource block for an S3 bucket with the exact bucket name.
2
Initialize Terraform working directory
Run the Terraform CLI command terraform init in the directory containing main.tf to initialize the working directory and download the AWS provider plugin.
Terraform
Hint
Use your terminal or command prompt to run terraform init in the folder where main.tf is saved.
3
Generate and review execution plan
Run the Terraform CLI command terraform plan to create an execution plan showing the changes Terraform will make to create the S3 bucket resource.
Terraform
Hint
Use your terminal to run terraform plan and review the output showing what Terraform will do.
4
Apply changes and list state resources
Run the Terraform CLI command terraform apply to apply the changes and create the S3 bucket. Then run terraform state list to list the resources managed in the Terraform state.
Terraform
Hint
Use your terminal to run terraform apply and confirm the creation. Then run terraform state list to see the resource names.
Practice
(1/5)
1. What is the primary purpose of the terraform init command?
easy
A. To show the planned changes without applying them
B. To apply changes to cloud infrastructure
C. To destroy all managed resources
D. To initialize a working directory and download required provider plugins
Solution
Step 1: Understand the role of terraform init
This command sets up the working directory by downloading necessary provider plugins and preparing the environment.
Step 2: Differentiate from other commands
Commands like apply and destroy perform changes, while init only prepares the setup.
Final Answer:
To initialize a working directory and download required provider plugins -> Option D
Quick Check:
terraform init prepares environment [OK]
Hint: Init sets up your folder and downloads plugins [OK]
Common Mistakes:
Confusing init with apply
Thinking init changes infrastructure
Mixing init with destroy
2. Which of the following is the correct syntax to preview changes without applying them?
easy
A. terraform plan
B. terraform apply --preview
C. terraform preview
D. terraform show
Solution
Step 1: Identify the command to preview changes
terraform plan shows what changes will happen without applying them.
Step 2: Check other options for correctness
apply --preview and preview are not valid commands; show displays current state, not planned changes.
Final Answer:
terraform plan -> Option A
Quick Check:
Preview changes = terraform plan [OK]
Hint: Plan shows changes without applying [OK]
Common Mistakes:
Using apply --preview which is invalid
Confusing show with plan
Assuming preview is a Terraform command
3. Given the following sequence of commands:
terraform init
tf plan
terraform apply
What will happen when you run these commands?
medium
A. Only initialize the directory and apply changes, skipping the plan
B. Initialize the directory, show planned changes, then apply those changes
C. Initialize the directory, apply changes immediately, then show the plan
D. Show planned changes without initializing or applying
Solution
Step 1: Understand each command's role
terraform init sets up the environment, terraform plan shows what will change, and terraform apply makes those changes.
Step 2: Analyze the sequence
The commands run in order: initialize, plan, then apply, which is the recommended workflow.
Final Answer:
Initialize the directory, show planned changes, then apply those changes -> Option B
Quick Check:
Init -> Plan -> Apply = safe workflow [OK]
Hint: Init, then plan, then apply changes [OK]
Common Mistakes:
Running apply before plan
Skipping init before plan
Confusing plan and apply order
4. You run terraform apply but get an error saying the backend is not configured. What is the most likely cause?
medium
A. You need to run terraform destroy first
B. You used the wrong syntax for apply
C. You forgot to run terraform init first
D. Your cloud provider credentials are missing
Solution
Step 1: Understand backend configuration role
The backend stores state remotely and must be set up before applying changes.
Step 2: Identify how backend is configured
terraform init configures the backend; skipping it causes errors.
Final Answer:
You forgot to run terraform init first -> Option C
Quick Check:
Init configures backend before apply [OK]
Hint: Always run init before apply to configure backend [OK]
Common Mistakes:
Skipping init and running apply directly
Assuming credentials cause backend error
Thinking destroy fixes backend issues
5. You want to safely remove all cloud resources managed by Terraform. Which command sequence ensures this with minimal risk?
hard
A. terraform destroy after terraform plan to review changes
B. terraform apply followed by terraform destroy
C. terraform init then terraform apply
D. terraform plan then terraform init
Solution
Step 1: Understand the purpose of terraform destroy
This command deletes all resources managed by Terraform.
Step 2: Use terraform plan before destroy
Planning shows what will be destroyed, helping avoid mistakes.
Step 3: Confirm the sequence
Running terraform plan first, then terraform destroy is the safest approach.
Final Answer:
terraform destroy after terraform plan to review changes -> Option A
Quick Check:
Plan before destroy to avoid surprises [OK]
Hint: Plan first, then destroy to confirm changes [OK]