OnCallReady

Terraform in Real Life & the Associate Exam: interview questions

The question you are most likely to get for each topic, a model answer, and what else comes up. From chapter 14 of the course.

How would you set up Terraform for a team that runs dev, test and prod? Mid

Also asked: How do you keep secrets out of Terraform? · What does "plan as an artifact" mean? · What is the difference between terraform fmt, validate and tflint?

How do you manage dev and prod with Terraform: separate directories or workspaces? Mid

Directory per environment for real environments: shared code in modules/, one thin root per environment (envs/dev, envs/prod) with its own backend key and its own terraform.tfvars.

Why not CLI workspaces (terraform workspace new prod, one folder, one state per workspace):

  1. Hidden state - which environment you are in lives in .terraform/environment, not in the path. An apply in the wrong workspace looks like the right one.
  2. One backend, one set of credentials for all workspaces - prod cannot get stronger protection than dev.
  3. Same code, forced - you cannot try a new module version in dev only; differences become terraform.workspace == "prod" ? ... : ... everywhere.
  4. A PR or a pipeline cannot show or gate "this touches prod".

Workspaces are fine for short-lived copies of one environment: a preview per branch, a load-test copy. And promotion becomes visible: bump the module ?ref= in dev, then test, then prod - three PRs.

Also asked: What are Terraform workspaces, and when would you use them? · How do you keep several environments from drifting apart? · What is the difference between a CLI workspace and an HCP Terraform workspace?

Learn it: 14.1 Environments: directory per environment vs workspaces

You are reviewing a platform module that creates a network, a managed identity, a container cluster and a Key Vault. What do you look for? Mid

Also asked: What is a managed identity, and why use a user-assigned one? · What is least privilege, and how does it apply to role assignments? · Why does a subnet NSG association exist as its own resource?

Learn it: 14.2 The platform module: network, identity, cluster (AKS), Key Vault

What is the difference between terraform fmt, terraform validate and tflint? Junior

Three layers, cheapest first, none needs a cloud login:

Chain them in a script with set -euo pipefail, run it in CI on every PR and in a pre-commit hook.

Also asked: What can these static checks not catch? · What is a pre-commit hook, and why do you still need CI? · How would you introduce tflint on a repo with hundreds of warnings?

Learn it: 14.6 Code quality: fmt, validate, tflint

What is policy as code, and how would you use it with Terraform? Junior

Policy as code means security and organisation rules ("storage must not be public", "no SSH open to the internet") written as machine-checked rules that run on every change, instead of hoping a reviewer remembers them.

With Terraform, a scanner like checkov:

Custom rules (an owner tag on everything) go in your own policies; Sentinel or OPA do the same in other tools.

Also asked: A checkov scan fails on a storage account. How do you handle it? · Why scan the plan as well as the code? · What can a security scanner not tell you about a change?

Learn it: 14.10 Security scanning: checkov on code and on plans

What does a good CI setup for Terraform look like? Mid

On every pull request: fmt -check, validate, tflint, checkov, and a plan per environment posted on the PR for reviewers.

On merge to main, per environment:

  1. terraform plan -input=false -lock-timeout=10m -out=tfplan - the plan is saved as an artifact, with a text version for the approver and JSON for tools.
  2. Gates on that exact plan: a checkov plan scan, and a jq policy gate that fails if resource_changes would delete anything stateful.
  3. A human approval of that specific plan.
  4. terraform apply tfplan - the file reviewed is the file applied. If state changed in between, it fails with Saved plan is stale: re-plan and re-approve, never switch to -auto-approve.

Around it: login with OIDC (no stored secret), separate identities for plan and apply and per environment, one run per state at a time, never cancel an apply halfway, short retention on plan artifacts (they contain secrets). The pipeline is the only path to prod.

Also asked: What does "plan as an artifact" mean? · How do you authenticate a Terraform pipeline without a stored secret? · The apply fails with "Saved plan is stale". What happened, and what do you do?

Learn it: 14.15 The pipeline: plan as an artifact, gates, approval, apply

How do you keep secrets out of Terraform? Mid

First know where they leak: committed tfvars (git history, forever), -var on a command line (CI logs), outputs without sensitive, state and saved plans (every value in plain text), TF_LOG=TRACE. sensitive = true only fixes the screen output.

Then design them away, strongest first:

  1. No secret at all - managed identities: the pipeline logs in with OIDC, the app reads storage or the database as its identity.
  2. Generated where they live - random_password written straight into Key Vault with azurerm_key_vault_secret (with an expiry); no human sees it.
  3. References, not values - the app setting holds a Key Vault reference to the secret's versionless_id, and the app's identity gets Key Vault Secrets User on that vault only. Rotation then needs no Terraform change.
  4. Protect what is unavoidable - state in a locked-down backend, short plan retention.

Rotate on exposure: terraform apply -replace=random_password.db. Newer Terraform adds ephemeral values and write-only arguments that never reach state.

Also asked: Where can secrets leak in a Terraform setup? · Does sensitive = true keep a value out of state? · What should you do when a password appears in a CI log?

Learn it: 14.20 Secrets: Key Vault references, not tfvars

How do you pin versions in Terraform, and how do you upgrade them safely? Junior

Three things get pinned, each in its own place:

Upgrading: one PR. terraform init -upgrade, read the lock-file diff, read the changelog or upgrade guide for a major version, fix what terraform validate reports, then plan every environment - the target is No changes. Roll out dev first, prod last. Renovate or Dependabot can open these PRs; a human reads the plans.

Also asked: What does ~> 4.14 allow, and what does ~> 4.14.0 allow? · Someone applied with a newer Terraform from a laptop and now CI cannot read state. What do you do? · Why do modules state a minimum provider version instead of a tight pin?

Learn it: 14.23 Pinning and upgrading Terraform, providers and modules

What is HCP Terraform, and what does it add over running Terraform in your own pipeline? Junior

HCP Terraform (formerly Terraform Cloud) is HashiCorp's hosted service for running Terraform; Terraform Enterprise is the self-hosted version. It bundles what you would otherwise build from parts:

The model is organisation, project, workspace. Mapping it onto your own pipeline (storage account, approvals, checkov, drift job) is the useful skill.

Also asked: What are the three HCP Terraform workflows? · What is the difference between an HCP workspace and a CLI workspace? · What are the Sentinel enforcement levels?

Learn it: 14.28 HCP Terraform: the exam's objective 8

What are the benefits of Infrastructure as Code? Junior

Infrastructure described in text files in git, applied by a tool, gives you:

Terraform adds being declarative and idempotent (describe the end state; applying twice changes nothing), one workflow across many providers, and state that maps code to real objects - which is also what you must protect.

Also asked: What happens when you run terraform apply while someone else is applying the same configuration? · Which command replaced terraform taint? · What does the dependency lock file record, and should it be committed?

Learn it: 14.29 The Terraform Associate (004) exam

Practise these answers with flashcards and labs Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.