Team Workflow
20. Describe a safe CI/CD workflow for Terraform.
- On every pull request:
fmt -check, validate, a linter such as TFLint, a security scan such as Checkov or Trivy, terraform test, and a plan posted to the PR for review.
- After merge: apply the saved plan for that commit, with a manual approval for production.
- Credentials: short-lived tokens through OIDC federation from GitHub Actions or GitLab to AWS, Azure or GCP, never long-lived keys in CI variables.
- One apply at a time per state, enforced by the backend lock and by pipeline concurrency settings.
Our CI/CD interview guide covers the pipeline side, such as OIDC and pinned actions, in more depth.
21. How do you handle secrets in Terraform?
Keep them out of code, plan files and state. Read them at run time from a secret manager, mark variables sensitive to keep them out of output, and, on Terraform 1.10 and later, make them ephemeral so Terraform does not persist them at all.
The strongest answer is to have the cloud generate and own the secret: for example, let RDS manage the master password in Secrets Manager (manage_master_user_password = true) so Terraform never sees it.
Where Terraform must pass a secret to a resource, use a write-only attribute (question 26). And encrypt the state backend regardless, because older providers still store secrets there.
22. What is policy as code for Terraform?
Rules evaluated against the plan before apply: no public S3 buckets, only approved instance types, mandatory tags, a cost ceiling.
HCP Terraform and Terraform Enterprise run Sentinel or OPA policies as a step in each run, with advisory, soft-mandatory and hard-mandatory enforcement levels.
Outside HCP Terraform, teams run OPA with Conftest, or Checkov, against terraform show -json output in CI.
Policy on the plan catches what the code will do, including values that come from variables, which a static scan of .tf files can miss. A strong candidate mentions both layers: static scans for fast feedback, plan policies as the gate.
23. How do you test Terraform code?
With terraform test, generally available since 1.6. Tests live in .tftest.hcl files as run blocks that plan or apply the module and assert on the result:
run "creates_two_buckets" {
command = plan
assert {
condition = length(aws_s3_bucket.this) == 2
error_message = "expected two buckets"
}
}
command = plan tests are fast and need no real resources; apply tests create real infrastructure and destroy it afterwards. Since 1.7, mock_provider and overrides let apply-mode tests run without cloud credentials.
Terratest in Go remains the choice for end-to-end tests that call the deployed service.
24. How would you upgrade a major provider version across many stacks?
Read the provider's upgrade guide first. The AWS provider's 6.0 release in June 2025 and AzureRM's 5.0 in July 2026 both removed deprecated resources and arguments, and some changes force replacement.
Then upgrade one low-risk stack: bump the constraint, run init -upgrade, fix errors and deprecation warnings, and read the plan for anything that says "must be replaced".
Roll out stack by stack, oldest risk last, and commit the lock file with each. Fixing deprecation warnings on the old major version before the jump makes the upgrade itself small.
The same approach applies to Terraform versions: required_version keeps anyone on an older CLI from writing a newer state format.
25. Scenario: an apply failed halfway and the state lock is stuck. What do you do?
First, confirm no run is still active: check the CI job and the lock's owner and timestamp in the error message. Only then run terraform force-unlock <LOCK_ID>.
Next, run a plan. Terraform writes state as each resource completes, so resources created before the failure are usually already tracked, and the plan shows what remains.
If the state itself is damaged, restore an earlier version from the backend's versioning (S3 bucket versioning, or state versions in HCP Terraform) and reconcile with a -refresh-only plan and import blocks for anything created but not recorded.
Candidates who say "delete the state and start again" fail this question.