Bird
Raised Fist0
Terraformcloud~10 mins

Terraform CLI overview - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Process Flow - Terraform CLI overview
Start: User writes Terraform config
↓
Run 'terraform init'
↓
Terraform downloads providers
↓
Run 'terraform plan'
↓
Terraform shows planned changes
↓
Run 'terraform apply'
↓
Terraform creates/updates infrastructure
↓
Run 'terraform destroy'
↓
Terraform deletes infrastructure
↓
End
This flow shows how a user writes config, initializes Terraform, plans changes, applies them, and optionally destroys infrastructure.
Execution Sample
Terraform
terraform init
terraform plan
terraform apply
terraform destroy
These commands initialize Terraform, show planned changes, apply them, and destroy resources.
Process Table
StepCommandActionResult
1terraform initInitialize working directory and download providersProviders downloaded, backend configured
2terraform planCreate execution planShows what will be created/changed/destroyed
3terraform applyApply changes to infrastructureResources created/updated as planned
4terraform destroyDestroy all managed infrastructureResources deleted
5-End of workflowNo further actions
💡 Workflow ends after destroy or when no further changes are needed
Status Tracker
VariableStartAfter initAfter planAfter applyAfter destroy
Working DirectoryEmptyInitialized with .terraform folderPlan generatedInfrastructure state updatedInfrastructure state cleared
ProvidersNot downloadedDownloadedUsed for planUsed for applyUsed for destroy
State FileNoneNoneNoneCreated/UpdatedCleared
Key Moments - 3 Insights
Why do we run 'terraform init' before other commands?
'terraform init' sets up the working directory and downloads necessary providers. Without it, 'plan' or 'apply' will fail. See execution_table step 1.
What does 'terraform plan' do if no changes are detected?
'terraform plan' will show that no changes are needed and no resources will be created or modified. This is shown in execution_table step 2.
Does 'terraform apply' always create new resources?
'terraform apply' only creates or updates resources if the plan shows changes. Otherwise, it does nothing. See execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of running 'terraform init'?
AResources created
BExecution plan created
CProviders downloaded, backend configured
DResources destroyed
💡 Hint
Check execution_table row 1 under Result column
At which step does Terraform show what changes it will make?
Aterraform plan
Bterraform init
Cterraform apply
Dterraform destroy
💡 Hint
See execution_table step 2 Action and Result
If you want to remove all infrastructure, which command do you run?
Aterraform plan
Bterraform destroy
Cterraform init
Dterraform apply
💡 Hint
Look at execution_table step 4 Command and Result
Concept Snapshot
Terraform CLI commands:
- terraform init: sets up directory and downloads providers
- terraform plan: shows what changes will happen
- terraform apply: makes changes to infrastructure
- terraform destroy: deletes all managed resources
Always run init first before plan or apply.
Full Transcript
Terraform CLI is a set of commands to manage infrastructure. First, you write your configuration files. Then you run 'terraform init' to prepare your working directory and download necessary providers. Next, 'terraform plan' shows you what changes Terraform will make. If you agree, 'terraform apply' makes those changes to your cloud resources. When you want to remove everything, 'terraform destroy' deletes all managed infrastructure. This workflow helps you safely create, update, and delete cloud resources using simple commands.

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

  1. Step 1: Understand the role of terraform init

    This command sets up the working directory by downloading necessary provider plugins and preparing the environment.
  2. Step 2: Differentiate from other commands

    Commands like apply and destroy perform changes, while init only prepares the setup.
  3. Final Answer:

    To initialize a working directory and download required provider plugins -> Option D
  4. 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

  1. Step 1: Identify the command to preview changes

    terraform plan shows what changes will happen without applying them.
  2. Step 2: Check other options for correctness

    apply --preview and preview are not valid commands; show displays current state, not planned changes.
  3. Final Answer:

    terraform plan -> Option A
  4. 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

  1. 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.
  2. Step 2: Analyze the sequence

    The commands run in order: initialize, plan, then apply, which is the recommended workflow.
  3. Final Answer:

    Initialize the directory, show planned changes, then apply those changes -> Option B
  4. 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

  1. Step 1: Understand backend configuration role

    The backend stores state remotely and must be set up before applying changes.
  2. Step 2: Identify how backend is configured

    terraform init configures the backend; skipping it causes errors.
  3. Final Answer:

    You forgot to run terraform init first -> Option C
  4. 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

  1. Step 1: Understand the purpose of terraform destroy

    This command deletes all resources managed by Terraform.
  2. Step 2: Use terraform plan before destroy

    Planning shows what will be destroyed, helping avoid mistakes.
  3. Step 3: Confirm the sequence

    Running terraform plan first, then terraform destroy is the safest approach.
  4. Final Answer:

    terraform destroy after terraform plan to review changes -> Option A
  5. Quick Check:

    Plan before destroy to avoid surprises [OK]
Hint: Plan first, then destroy to confirm changes [OK]
Common Mistakes:
  • Destroying without planning first
  • Applying changes before destroying
  • Running init after plan