CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / IaC & Config Management / terraform apply

terraform apply

Create, change or destroy real infrastructure

Intermediateterraformiacdeploy

terraform apply carries out a plan: it calls the cloud APIs to create, update, replace and delete resources, in dependency order and in parallel, then saves the result to state. Without a plan file it plans first and asks you to type yes.

Syntaxterraform apply [PLANFILE] | terraform apply [-auto-approve] [-var-file=F] [-replace=ADDR] [-target=ADDR] [-parallelism=N] · terraform destroy

Watch it run

Create the dev environment for the first time: a random suffix, the logs bucket and the web instance.

devops@devops-lab: ~/infra/envs/dev — zsh — 96×28
➜ ~/infra/envs/dev
Step 1 of 1
terraform apply

Only the exact word yes approves. The bucket and instance start in parallel once random_id (which the bucket name needs) is done. Long operations print 'Still creating...' every 10 seconds. Each finished resource is saved to state immediately.

Anatomy of a command

$
commandterraform

The Terraform CLI.

How it works

apply takes the state lock and either loads the saved plan (checking that the state hasn't changed since; a stale plan is rejected) or computes a fresh plan and asks for 'yes'. Then Terraform walks the resource graph: anything without unfinished dependencies runs concurrently, up to -parallelism (10). For each action it calls the provider's ApplyResourceChange over gRPC; the AWS provider makes the real API calls (RunInstances, then polls DescribeInstances until 'running'; CreateBucket; and so on) and returns the new attributes, which fill in the (known after apply) values that dependants need. After each resource finishes, Terraform persists the updated state (to S3 for a remote backend), so a crash mid-apply loses little. If an operation fails, the other independent operations finish, the error is printed, and state records what really happened; a half-created resource can end up 'tainted' and replaced next time. There is no rollback: you fix and apply again. Finally the lock is released and outputs are printed.

Options you'll actually use

PLANFILEApply a saved plan exactly; no prompt
-auto-approveSkip the yes prompt (only for automation you trust)
-var / -var-fileVariables, when not applying a saved plan
-replace=ADDRForce-recreate one resource (replaces old taint)
-target=ADDROnly this resource and its dependencies (exceptional)
-parallelism=NConcurrent operations (default 10)
-lock-timeout=5mWait for a busy lock instead of failing
-refresh-onlyAccept drift into state; change no resources
destroyterraform destroy = apply -destroy: remove everything in state

Common mistakes

terraform apply -auto-approve from a laptop against prod

No review, and whatever is on your branch and in your shell variables gets applied.

Plan in CI with -out, review, then apply the saved plan from the pipeline
Using -lock=false because 'the lock is stuck'

If another apply is running, both write state and one set of changes is lost or corrupted.

Find who holds it (Lock Info); terraform force-unlock ID only when that run is really dead
Expecting a failed apply to roll back

Terraform has no transactions; what succeeded stays and is in state.

Read the error, fix the code, apply again; it converges from where it stopped
Fixing a broken resource by deleting it in the console

State still has it; the next plan shows it must be created, but other resources may reference a now-missing ID.

terraform apply -replace=ADDR to recreate it through Terraform

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    In ~/infra (after apply), recreate only aws_instance.web without touching anything else, with an approval prompt. Compare the instance ID before and after.

  2. 2

    Read this lock message and decide what to do: 'Who: priya@ci-runner-7 Created: 2026-10-29 10:42:33' and it's now 10:44.

  3. 3

    Write the commands a pipeline's apply stage should run, given that the plan stage uploaded tfplan.

Related commands

terraform planPreview what Terraform will changeterraform stateInspect and repair Terraform's stateterraform initPrepare a Terraform working directoryaws ec2Find, start and stop EC2 instances from the CLI
Previousterraform planNextterraform state
Linux / IaC & Config Management / terraform apply

terraform apply

Create, change or destroy real infrastructure

Intermediateterraformiacdeploy

terraform apply carries out a plan: it calls the cloud APIs to create, update, replace and delete resources, in dependency order and in parallel, then saves the result to state. Without a plan file it plans first and asks you to type yes.

Syntaxterraform apply [PLANFILE] | terraform apply [-auto-approve] [-var-file=F] [-replace=ADDR] [-target=ADDR] [-parallelism=N] · terraform destroy

Watch it run

Create the dev environment for the first time: a random suffix, the logs bucket and the web instance.

devops@devops-lab: ~/infra/envs/dev — zsh — 96×28
➜ ~/infra/envs/dev
Step 1 of 1
terraform apply

Only the exact word yes approves. The bucket and instance start in parallel once random_id (which the bucket name needs) is done. Long operations print 'Still creating...' every 10 seconds. Each finished resource is saved to state immediately.

Anatomy of a command

$
commandterraform

The Terraform CLI.

How it works

apply takes the state lock and either loads the saved plan (checking that the state hasn't changed since; a stale plan is rejected) or computes a fresh plan and asks for 'yes'. Then Terraform walks the resource graph: anything without unfinished dependencies runs concurrently, up to -parallelism (10). For each action it calls the provider's ApplyResourceChange over gRPC; the AWS provider makes the real API calls (RunInstances, then polls DescribeInstances until 'running'; CreateBucket; and so on) and returns the new attributes, which fill in the (known after apply) values that dependants need. After each resource finishes, Terraform persists the updated state (to S3 for a remote backend), so a crash mid-apply loses little. If an operation fails, the other independent operations finish, the error is printed, and state records what really happened; a half-created resource can end up 'tainted' and replaced next time. There is no rollback: you fix and apply again. Finally the lock is released and outputs are printed.

Options you'll actually use

PLANFILEApply a saved plan exactly; no prompt
-auto-approveSkip the yes prompt (only for automation you trust)
-var / -var-fileVariables, when not applying a saved plan
-replace=ADDRForce-recreate one resource (replaces old taint)
-target=ADDROnly this resource and its dependencies (exceptional)
-parallelism=NConcurrent operations (default 10)
-lock-timeout=5mWait for a busy lock instead of failing
-refresh-onlyAccept drift into state; change no resources
destroyterraform destroy = apply -destroy: remove everything in state

Common mistakes

terraform apply -auto-approve from a laptop against prod

No review, and whatever is on your branch and in your shell variables gets applied.

Plan in CI with -out, review, then apply the saved plan from the pipeline
Using -lock=false because 'the lock is stuck'

If another apply is running, both write state and one set of changes is lost or corrupted.

Find who holds it (Lock Info); terraform force-unlock ID only when that run is really dead
Expecting a failed apply to roll back

Terraform has no transactions; what succeeded stays and is in state.

Read the error, fix the code, apply again; it converges from where it stopped
Fixing a broken resource by deleting it in the console

State still has it; the next plan shows it must be created, but other resources may reference a now-missing ID.

terraform apply -replace=ADDR to recreate it through Terraform

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    In ~/infra (after apply), recreate only aws_instance.web without touching anything else, with an approval prompt. Compare the instance ID before and after.

  2. 2

    Read this lock message and decide what to do: 'Who: priya@ci-runner-7 Created: 2026-10-29 10:42:33' and it's now 10:44.

  3. 3

    Write the commands a pipeline's apply stage should run, given that the plan stage uploaded tfplan.

Related commands

terraform planPreview what Terraform will changeterraform stateInspect and repair Terraform's stateterraform initPrepare a Terraform working directoryaws ec2Find, start and stop EC2 instances from the CLI
Previousterraform planNextterraform state