Real-Time Terraform Interview Questions: Workflow (2027)
44 real-time Terraform interview questions on daily team workflow: plan review, CI/CD, state locks, provider upgrades, drift and promotion.
Real-time Terraform interview questions ask how you use Terraform on an ordinary working day in a team: reviewing a plan in a pull request, running Terraform from CI/CD, handling state locks, upgrading providers, catching drift and promoting changes from dev to production. Interviewers want the commands you would type and the checks you would make, not definitions. This page has 44 questions on that daily workflow, grouped by level.
This page sits beside the other Terraform guides rather than replacing them. The 120+ Terraform interview questions guide is the main question bank. Use Terraform questions for freshers for basics, advanced Terraform questions for internals, and Terraform scenario drills for incident practice. For the wider tool picture, see 12 infrastructure as code tools ranked.
Key takeaways
- "Real-time" questions are about workflow: plan files, pull request review, pipeline gates, locks and promotion between environments.
- Save the plan with
-outand apply that exact file. Interviewers check whether you know why. - Use
-detailed-exitcodein CI: exit code 2 means changes are present. - Commit
.terraform.lock.hclso every machine uses the same provider versions. - The S3 backend supports native lock files with
use_lockfilesince Terraform 1.10; DynamoDB-based locking is deprecated.
How the questions are organised
| Section | Questions | Daily task |
|---|---|---|
| Freshers | 1 to 12 | Running the core commands correctly |
| 2 to 5 years | 13 to 30 | Pipelines, state operations, provider and module upgrades |
| Senior | 31 to 38 | Workflow design, policy, promotion, team scale |
| Scenarios | 39 to 44 | Things that go wrong during a normal week |
The daily Terraform loop
| Step | Command | What you check |
|---|---|---|
| Format | terraform fmt -check -recursive | Style consistent, no diff |
| Initialise | terraform init | Backend and providers match the lock file |
| Validate | terraform validate | Syntax and references are valid |
| Plan | terraform plan -out=tfplan | Only expected adds, changes, destroys |
| Review | terraform show tfplan | Replacements, sensitive changes, counts |
| Apply | terraform apply tfplan | Applies exactly what was reviewed |
Freshers: core commands done right
1. What does terraform init do each time you run it?
It configures the backend, downloads providers that match version constraints and the lock file, and fetches modules. Run it after cloning, after changing the backend, or after changing provider or module sources. -upgrade lets it pick newer allowed versions.
2. Why save a plan with -out?
A saved plan is a fixed set of changes. Applying the file applies exactly what was reviewed. If you run apply without it, Terraform makes a new plan, which may differ if the code or the real infrastructure changed in between.
3. How do you read a plan quickly?
Look at the summary line ("Plan: X to add, Y to change, Z to destroy") first. Then search for must be replaced and -/+, which mean destroy and recreate. Any unexpected destroy on a database, bucket or network is a stop sign.
4. What is the difference between ~, + , - and -/+ in a plan?
+ create, - destroy, ~ update in place, -/+ destroy then create, +/- create then destroy (when create_before_destroy is set).
5. What does terraform validate check?
Whether the configuration is syntactically valid and internally consistent: references exist, argument types are correct. It does not contact cloud APIs or check whether resources will create successfully.
6. Why run terraform fmt in CI?
So every file follows the same canonical style and diffs show real changes only. terraform fmt -check exits non-zero if any file needs formatting.
7. What is the .terraform.lock.hcl file?
The dependency lock file records the exact provider versions and checksums selected. Commit it so teammates and CI use identical providers. Update it on purpose with terraform init -upgrade.
8. What is remote state and why use it in a team?
State stored in a shared backend (S3, Azure Blob Storage, Google Cloud Storage, HCP Terraform) instead of a laptop. Everyone works from the same state, it supports locking and you can version and encrypt it. See the role of remote state in Terraform.
9. What is state locking?
The backend takes a lock during operations that write state so two runs cannot change it at the same time. If a run crashes, the lock can stay behind and must be cleared with care.
10. How do you pass different values per environment?
Use variables with .tfvars files per environment (-var-file=prod.tfvars), environment variables named TF_VAR_name, or separate root modules per environment that call shared modules.
11. How do you see outputs after an apply?
terraform output lists them; terraform output -json gives machine-readable output for scripts. Sensitive outputs are hidden in normal display.
12. How do you destroy just one resource safely?
Prefer removing it from the code and running a plan, so the change is reviewed. terraform destroy -target=ADDRESS exists, but HashiCorp describes -target as for exceptional cases, not routine use.
2 to 5 years: pipelines, state and upgrades
13. What does a Terraform pull request pipeline look like?
On each pull request: fmt -check, init, validate, a linter such as TFLint, a security scan such as Checkov or Trivy, then plan -out with the plan posted as a PR comment. After merge, apply the saved plan or re-plan and apply with an approval gate for production.
terraform fmt -check -recursive
terraform init -input=false
terraform validate
terraform plan -input=false -out=tfplan -detailed-exitcode
# exit 0: no changes, 1: error, 2: changes present
terraform show -no-color tfplan > plan.txt
14. How do you use -detailed-exitcode?
With it, plan exits 0 when there are no changes, 1 on error and 2 when changes are present. Pipelines use it to skip apply when nothing changed, or to flag drift in scheduled jobs.
15. How do you authenticate Terraform in CI without static keys?
Use OIDC federation from the CI system to the cloud: an AWS IAM role with a web identity trust, Azure workload identity federation, or Google Cloud Workload Identity Federation. HCP Terraform supports dynamic provider credentials in the same way.
16. How do you set up an S3 backend with locking today?
Enable bucket versioning and encryption, then set use_lockfile = true so Terraform writes a lock file in the bucket. The older dynamodb_table argument still works but is deprecated.
terraform {
backend "s3" {
bucket = "acme-tfstate-prod"
key = "network/terraform.tfstate"
region = "ap-south-1"
encrypt = true
use_lockfile = true
}
}
17. How do you clear a stuck lock?
First confirm no run is still active (check the pipeline and who holds the lock in the error message). Then run terraform force-unlock LOCK_ID. Never force-unlock while another apply is running.
18. How do you rename a resource without destroying it?
Add a moved block from the old address to the new one, then plan. The plan should show the move and no destroy. terraform state mv does the same imperatively but leaves no trace in code.
19. How do you bring an existing resource under Terraform?
Write an import block with the address and ID (Terraform 1.5 and later), optionally run terraform plan -generate-config-out=generated.tf to draft the resource code, tidy it, and apply. The import happens as part of the reviewed plan.
20. How do you check for drift?
Run terraform plan -refresh-only to see what changed outside Terraform without proposing config changes, or a normal plan in a scheduled job with -detailed-exitcode. HCP Terraform has built-in drift detection for some tiers. See configuration drift detection.
21. How do you upgrade a provider major version?
Read the provider upgrade guide, bump the constraint in a branch, run terraform init -upgrade, fix deprecations and breaking changes, then plan against every environment and confirm no unexpected replacements before merging.
22. How do you pin and upgrade module versions?
Pin registry modules with version constraints and Git modules with ?ref=v1.4.0 tags. Upgrade one environment at a time and read the module changelog. Avoid pointing at a branch, since its content changes under you.
23. How do you lock providers for several platforms?
Developers on macOS and CI on Linux need checksums for both. Run terraform providers lock -platform=linux_amd64 -platform=darwin_arm64 and commit the updated lock file.
24. How do you debug a failing apply?
Read the provider error first, since most failures are API errors (quota, permissions, name conflicts). Set TF_LOG=DEBUG and TF_LOG_PATH to capture detail. Re-run plan after fixing, because a partial apply already updated state for resources that succeeded.
25. What happens to state when an apply fails half way?
Resources that were created are recorded in state. The failed ones are not, or are marked tainted if creation started. The next plan picks up from there. Do not delete state to "start fresh".
26. How do you replace a broken resource on purpose?
Use terraform apply -replace=ADDRESS. It shows the replacement in the plan before you approve. terraform taint is deprecated.
27. How do you share values between state files?
Expose outputs in the producer and read them with the terraform_remote_state data source, or better, look up the resources directly with provider data sources or a parameter store so consumers do not need read access to the whole state.
28. How do you keep secrets out of plans and state?
Mark variables sensitive to hide them in output (state still holds them), restrict state access, and where supported use ephemeral values and write-only arguments, which keep the value out of state. Pull secrets from a secrets manager at runtime instead of tfvars files.
29. When would you use workspaces?
CLI workspaces give several state files for one configuration, which suits short-lived copies such as feature environments. For long-lived dev, staging and prod with different access controls, separate root directories or separate backends are clearer.
30. How do you speed up slow plans?
Split large root modules by lifecycle and ownership, avoid huge count loops over data sources, and use -refresh=false only when you know state is current. -parallelism (default 10) can be raised if the API allows it.
Senior: workflow design
31. How do you design promotion from dev to prod?
Use the same versioned modules everywhere and change only inputs. Merge to main triggers dev apply, then the same commit promotes to staging and prod with approval gates. The prod plan must be reviewed by someone other than the author.
32. How do you enforce policy before apply?
Run policy as code against the plan JSON (terraform show -json tfplan) with OPA/Conftest or Sentinel in HCP Terraform. Typical rules: required tags, no public buckets, approved instance types, cost limits.
terraform show -json tfplan > tfplan.json
conftest test tfplan.json --policy policy/
33. How do you split state for a large organisation?
By blast radius and ownership: networking, shared platform, and each application stack in its own state. Fewer resources per state means faster plans, smaller locks and less risk from one bad change.
34. Who should be allowed to apply to production?
Only the pipeline identity, after review and approval. Humans get read access and break-glass access that is logged and time-limited.
35. HCP Terraform or self-run pipelines?
HCP Terraform (renamed from Terraform Cloud in 2024) gives remote runs, state, policy, private registry and run history out of the box. Self-run pipelines in GitHub Actions, GitLab CI or Jenkins give more control and avoid another vendor. Choose based on team size, compliance and cost.
36. How do you handle the Terraform licence change in planning?
Terraform moved to the Business Source License from version 1.6 in 2023, and the community forked OpenTofu under the Linux Foundation. For most internal use the BSL does not matter, but legal teams may ask. Know both exist and that OpenTofu is largely compatible.
37. How do you test modules in daily work?
Write terraform test files with plan runs for fast checks and apply runs in a sandbox account, use mocks for providers where possible, and run them in the module's CI before tagging a release.
38. How do you onboard a new team to the Terraform workflow?
Give them a module catalogue, a template repository with the pipeline already wired, documented conventions for naming and tags, and a sandbox account. Review their first few PRs closely.
Scenarios from a normal week
39. The PR plan shows a database will be replaced after a small change.
Stop. Find the attribute marked forces replacement. Often it is a name, engine version path or subnet group change. Revert that attribute, use a moved block if an address changed, or plan a controlled migration. Add prevent_destroy on critical data stores.
40. CI fails with "Error acquiring the state lock".
Check whether another pipeline run or a colleague is running Terraform. If a crashed job left the lock, confirm it is dead, then force-unlock with the lock ID from the error. Add pipeline concurrency limits per state to stop it recurring.
41. Someone changed a security group in the console. The next plan wants to revert it.
Ask why first. If the change is valid, add it to the code so plan shows no changes. If not, apply to revert. Either way, tighten console write access and schedule drift checks.
42. A provider upgrade breaks plans in only one environment.
Compare lock files and provider versions across environments, check the provider changelog for changed defaults, and pin the old version for that environment while you fix code. Commit a single lock file across environments in future.
43. Plan works locally but fails in CI.
Usually credentials, different provider versions or missing variables. Check the CI identity's permissions, that the lock file is committed, that -input=false is not hiding a missing variable, and that the backend config matches.
44. A teammate ran apply from a laptop with an old branch.
Run a plan from main to see what changed, then re-apply from main to restore the intended state. Then remove direct apply permissions for humans and require the pipeline.
How to prepare
- Build a small repo with a GitHub Actions or GitLab CI pipeline that plans on PR and applies on merge using OIDC.
- Practise
moved,import,-replaceandforce-unlockin a sandbox. - Explain your workflow aloud as if onboarding a new teammate.
- For guided labs, see our Terraform training in the Kharadi, Pune classroom or live online.
FAQ
What does "real-time Terraform questions" mean?
Questions about how you use Terraform day to day in a team: plan review, pipelines, state locking, upgrades, drift and promotion.
Should I apply a saved plan file?
Yes, in pipelines. Apply the plan file that was reviewed so nothing changes between review and apply.
Is DynamoDB still needed for S3 state locking?
No. Since Terraform 1.10 the S3 backend can lock with a lock file in the bucket using use_lockfile, and DynamoDB locking is deprecated.
Should the lock file be committed?
Yes. Commit .terraform.lock.hcl so every machine uses the same provider versions and checksums.
Is terraform taint still used?
It is deprecated. Use terraform apply -replace with the resource address.
How do I detect drift automatically?
Run a scheduled plan with -detailed-exitcode or a refresh-only plan and alert when the exit code shows changes.
Official references
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Angry
0
Sad
0
Wow
0