skip to content

Plan/Apply Workflow

The operational loop around the code: init, plan, apply, CLI workspaces, targeting, tests and running it all in CI. Interviewers ask how a plan gets reviewed: that separates IaC from a shared laptop.

part ofTerraformread it →
on this pageshow

explore

questions

page 2 of 2

What does the plan file produced by `terraform plan -out=tfplan` actually contain, and why does that make it sensitive?

level: seniorimportance: should knowfreq 40%

basics

~20 s

A saved plan is an opaque binary archive holding the configuration, a snapshot of prior state and every planned attribute value — including values marked sensitive, which are only masked in the printed output, not removed from the file. Protect it exactly like state.

open as a page

Why did a team's Infracost job post a cost comment and stay green while a pull request added $4,000 a month, and what would make that cost block the merge?

level: seniorimportance: should knowfreq 20%

basics

~20 s

Infracost treats a cost increase as information: scan and diff exit non-zero only on errors such as critical diagnostics. Blocking needs a guardrail set to block the pull request, or your own script testing the JSON, plus a required check.

open as a page

With Infracost, why can a pull request adding a function behind a busy queue show almost no monthly increase while the bill later jumps, and how do you handle it?

level: seniorimportance: should knowfreq 24%

basics

~20 s

Infracost prices what code declares, and request counts or stored gigabytes are not in the code. Such a component shows a unit price and adds nothing to the total, so supply usage values and read the total as partial.

open as a page

After `terraform apply -refresh-only` records a colleague's console change into Terraform state, what does the very next plain `terraform plan` propose, and why?

level: seniorimportance: should knowfreq 38%

basics

~20 s

It proposes changing the resource back to whatever the configuration says. Refresh-only updates state, never the .tf files, so the plan now compares an accurate state against an unchanged config and sees a difference it intends to correct.

open as a page

How would you run a scheduled Terraform drift-detection job in CI, and how should the job decide whether to raise an alert?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Run terraform init and a non-interactive plan on a schedule against the deployed branch, and branch on -detailed-exitcode: 0 means no changes, 2 means the world differs from code, 1 means the run itself failed. Alert on 2, page on repeated 1.

open as a page

After an incident you fixed production with `terraform apply -target=module.db`. The next untargeted `terraform plan` proposes changes to resources nobody edited. What explains that, and how do you handle the plan?

level: seniorimportance: should knowfreq 40%

basics

~20 s

A targeted apply evaluates and refreshes only the targeted subgraph, so the first untargeted plan afterwards shows the catch-up work: dependents that never picked up new attribute values, stale outputs, and real-world drift no run had refreshed since.

open as a page

A Terragrunt plan for `live/dev/us-east-1/app` shows `vpc-mock` as its VPC ID because `../vpc` was never applied. What is happening, and how do you stop that value reaching a real apply?

level: seniorimportance: should knowfreq 37%

basics

~20 s

The dependency block found no outputs in the unapplied vpc unit, so Terragrunt substituted mock_outputs. With no allow-list those mocks work for every command, so set mock_outputs_allowed_terraform_commands and never apply a saved plan built on mocks.

open as a page

A Terratest test of a VPC module fails its endpoint check right after apply but passes on rerun: where do retries belong, and what should not be retried?

level: seniorimportance: should knowfreq 33%

basics

~10 s

Retry at two layers: terraform.WithDefaultRetryableErrors re-runs a Terraform command on listed transient errors, and http_helper or retry.DoWithRetryContext polls the live check. Deterministic failures should stop at once, and retries must fit the test timeout.

open as a page

Under Terraform's `mock_provider`, why does a format-checking validation reject a computed ARN in a credential-free CI job running a module's `.tftest.hcl` tests, and how do you fix it?

level: seniorimportance: should knowfreq 30%

basics

~20 s

Terraform's mock_provider knows an attribute's type, not its format, so a computed arn arrives as a random 8-character string and fails the regex at apply. Set an ARN-shaped value with mock_resource defaults or override_resource on the producing resource.

open as a page

Why do teams run Checkov or Trivy against `terraform show -json` plan output instead of the raw HCL, and what does that cost them?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Plan JSON contains resolved values, so a scanner sees the attribute a resource will really have instead of an unresolved var or module reference. The cost is that it needs init, credentials and a successful plan, so it runs late and can never be a pre-commit check.

open as a page

In a Terraform `.tftest.hcl` file using a setup helper module, how do state keys decide which `run` blocks can run in parallel and what cleanup destroys first?

level: seniorimportance: should knowfreq 36%

basics

~20 s

Terraform test files keep one in-memory state per state key: by default the main configuration and each helper module source. Run blocks sharing a key never run concurrently, and cleanup destroys the most recently updated state first.

open as a page

A Terraform CI job applied against the default workspace instead of `staging`. How does Terraform decide which CLI workspace is active, and how do you make that deterministic in a pipeline?

level: seniorimportance: should knowfreq 35%

basics

~20 s

Terraform reads the active workspace from .terraform/environment in the working directory. A fresh CI checkout has no such file, so it silently uses default. Make it explicit with TF_WORKSPACE or a terraform workspace select step after init.

open as a page

When pull requests show an Infracost monthly cost diff and leadership wants large increases blocked, who should own the threshold, and should cost ever block a merge?

level: principalimportance: should knowfreq 17%

basics

~20 s

Default to a visible comment routed to an accountable owner, not a veto: the estimate misses usage, and engineers cannot usually approve spending alone. The budget holder owns the threshold; reserve blocking for high-confidence limits with a fast, recorded override.

open as a page

Your team runs about forty Terraform root modules with hand-copied backend and provider blocks. How would you decide whether to adopt Terragrunt, and what lock-in would you accept?

level: principalimportance: should knowfreq 33%

basics

~20 s

Adopt Terragrunt when repetition and ordering across many similar roots cost more than a second tool; keep plain root modules and CI when roots are few or independent. The lock-in sits in orchestration and configuration, not in state.

open as a page

Your organisation's Terratest suites apply real infrastructure into sandbox accounts: who should own those accounts and their bill, and what standing rules would you set?

level: principalimportance: should knowfreq 27%

basics

~20 s

A common answer: a platform team owns the sandbox accounts, guardrails and sweep, while each team writing tests sees and answers for its share of the bill. Standing rules cover isolation, naming, budgets, mandatory cleanup and leak reporting.

open as a page

You own a shared Terraform network module used by many teams: what do you require of its `terraform test` suite before you tag a release, and why?

level: principalimportance: should knowfreq 30%

basics

~20 s

For a shared Terraform module's test suite, require a failure case for each promised custom condition, plan-only logic tests and at least one real apply in a dedicated account that cleans up after itself, scaled to blast radius and cost.

open as a page

What does `terraform plan -detailed-exitcode` return, and how would you use it in a script?

level: middleimportance: nice to knowfreq 32%

basics

~20 s

With -detailed-exitcode, terraform plan exits 0 when there are no changes, 1 on error, and 2 when the plan succeeded and changes are pending. Scripts use it to tell an empty diff apart from a real one without parsing output.

open as a page

On Infracost CLI 2.x, what happens to an old script running `infracost breakdown --path . --format json --out-file base.json` and `infracost comment github`?

level: middleimportance: nice to knowfreq 15%

basics

~20 s

On Infracost CLI 2.x, breakdown warns and runs scan, keeping only the path and silently dropping the format and output flags, so no JSON file appears. The comment command fails outright and points to infracost ci setup.

open as a page

With Infracost, when would you commit an `infracost.yml` instead of relying on autodetection, and what does the file pin down?

level: middleimportance: nice to knowfreq 14%

basics

~20 s

Commit an infracost.yml when detection's project list or names are not what you want: it pins each project's path, name and type, plus a usage file. Names matter because diffs, guardrail costs and usage filters match projects by name.

open as a page

In Terragrunt, how do implicit stacks differ from explicit stacks defined in `terragrunt.stack.hcl`, and when is the generated form worth it?

level: middleimportance: nice to knowfreq 27%

basics

~20 s

An implicit Terragrunt stack is a directory tree of hand-written units. An explicit stack is a terragrunt.stack.hcl blueprint that generates its units into .terragrunt-stack. Explicit pays off when the same set of units repeats across environments.

open as a page

With Terratest's `test_structure` package, how do you re-run only the validation step of a slow test without redeploying the module each time?

level: middleimportance: nice to knowfreq 25%

basics

~10 s

Wrap deploy, validate and teardown in test_structure.RunTestStage, then set SKIP_<stage> for each stage to skip. Save options to disk in the deploy stage and load them later, because a skipped stage's function never runs.

open as a page

How do you correctly retire an ephemeral Terraform CLI workspace, and what happens if you run `terraform workspace delete` while its state still tracks resources?

level: middleimportance: nice to knowfreq 25%

basics

~20 s

Destroy inside the workspace first, then select another workspace and delete it. Terraform refuses to delete a workspace whose state still tracks resources unless you pass -force, which discards the state and orphans the real infrastructure.

open as a page

A large `terraform apply` keeps failing partway through with rate-limit errors from the cloud provider's API. What does Terraform's `-parallelism` option control, and how would you use it here?

level: seniorimportance: nice to knowfreq 35%

basics

~20 s

Terraform's -parallelism sets how many resource operations one run performs concurrently while walking the dependency graph, defaulting to 10. Lowering it thins the burst of API calls the run makes, which is the fastest way to get a throttled apply through.

open as a page

What does declaring `mock_provider "aws"` in a Terraform test file change about how the tests execute, and what stops being tested?

level: seniorimportance: nice to knowfreq 26%

basics

~20 s

A mock_provider block replaces the real provider, so Terraform walks the full plan and apply graph while generated values stand in for computed attributes. Nothing is created, no credentials are needed, and tests run in seconds — but no API ever validates the request.

open as a page

When would you run Terraform through a purpose-built run service such as Atlantis or HCP Terraform instead of hand-written plan and apply jobs in your existing CI system?

level: principalimportance: nice to knowfreq 34%

basics

~20 s

Adopt a run service when the number of Terraform repositories makes hand-rolled jobs a maintenance burden, and when you want cloud credentials, state, run history and approvals owned by one system instead of scattered across CI configurations.

open as a page

You own hundreds of Terraform root modules, and refresh on every plan is now saturating provider API rate limits. How do you keep refresh and drift detection affordable?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

Treat refresh cost as proportional to resources per state and runs per hour, then attack both: keep root modules small, tier how often each is fully refreshed, stagger schedules so runs do not collide, and reserve unrefreshed plans for cheap feedback rather than for gated applies.

open as a page

How much of a Terraform module's `terraform test` suite would you let run on `mock_provider` before a green result stops proving anything, and what rule would you set?

level: principalimportance: nice to knowfreq 22%

basics

~20 s

No fixed percentage works: Terraform mocks can carry what a module's own logic decides, never what the provider decides at plan or apply. Assertions reading only authored values prove nothing, so keep one real-provider path per module.

open as a page

showing 31–57 of 57