004 · Domain 3: Core Terraform workflow · 13% of the exam
Task 3.5: Apply changes to infrastructure with Terraform
terraform apply: approving a fresh plan or applying a saved one, automation with -auto-approve, forcing replacement with -replace, parallelism, and what happens to state when an apply fails half way.
Study it
terraform apply: approval, saved plans, -replace and failures part way
Lesson coming
Sample questions
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 2
A pipeline runs terraform plan -out=tfplan, waits for a person to approve the printed plan, and then runs terraform apply tfplan. Which two statements about the apply step are true? (Choose TWO.)
AIt runs a new plan and applies that instead if the configuration has changed
BIt does not ask for confirmation, because passing the plan file counts as approval
CIt needs -auto-approve, or it waits for input that the pipeline cannot give
DIt cannot take extra planning options such as -var or -replace
EIt reads variable values again from terraform.tfvars at apply time
Show the answer and why
AIt runs a new plan and applies that instead if the configuration has changed
Incorrect
A saved plan is applied as saved; Terraform does not silently re-plan.
BIt does not ask for confirmation, because passing the plan file counts as approval
Correct
When you pass a saved plan file, apply takes its actions without prompting; -auto-approve is ignored with a plan file.
CIt needs -auto-approve, or it waits for input that the pipeline cannot give
Incorrect
The saved plan is the approval, so no prompt appears.
DIt cannot take extra planning options such as -var or -replace
Correct
With a saved plan you cannot specify additional planning modes or options; the plan file already contains the final decisions.
EIt reads variable values again from terraform.tfvars at apply time
Incorrect
Input variables are stored in the plan file; apply does not re-read them.
The two-step workflow separates deciding (plan) from doing (apply), and the saved plan carries every decision with it.
During large applies a provider's API returns rate-limit errors. The team wants Terraform to run fewer operations at once. Which option controls this, and what is its default?
A-lock-timeout=n; by default it waits 10 seconds between operations
B-parallelism=n; up to 10 operations at once by default
C-compact-warnings; by default 10 warnings at once
D-target=n; by default 10 resources per run
Show the answer and why
A-lock-timeout=n; by default it waits 10 seconds between operations
Incorrect
-lock-timeout controls waiting for the state lock.
B-parallelism=n; up to 10 operations at once by default
Correct
-parallelism limits concurrent operations as Terraform walks the graph; the default is 10.
C-compact-warnings; by default 10 warnings at once
Incorrect
That option only shortens warning output.
D-target=n; by default 10 resources per run
Incorrect
-target selects resources by address, not by count.
Parallelism is a graph-walk setting; lowering it is an advanced fix for rate limits.
A repository keeps each environment's root module in its own directory, such as envs/prod. A wrapper script runs from the repository root and must apply the prod configuration. Which command does this?
Aterraform -chdir=envs/prod apply
Bterraform apply envs/prod
Cterraform apply -target=envs/prod
Dterraform apply -var-file=envs/prod
Show the answer and why
Aterraform -chdir=envs/prod apply
Correct
The global -chdir option makes Terraform switch to the given directory before running the subcommand, for all commands.
Bterraform apply envs/prod
Incorrect
Passing a directory to apply was removed in Terraform v0.15; the argument now has to be a saved plan file.
Cterraform apply -target=envs/prod
Incorrect
-target takes resource addresses, not directories.
Dterraform apply -var-file=envs/prod
Incorrect
-var-file reads variable values from a file; it does not change the root module.
-chdir goes before the subcommand and works the same for init, plan and apply.