Terraform Essentials — Plan and Apply, Variables, Outputs, Data Sources, and Sharing State at Scale

Plan and apply, saved and stale plans, variables, outputs and data sources as the Terraform docs define them. Why terraform_remote_state fails at scale and the documented alternatives.

Overview

Most Terraform incidents are not provider bugs. They are a plan that was reviewed against one state and applied against another, a variable that came from a file nobody meant to load, a secret that was marked sensitive and still ended up readable in state, or a downstream configuration that broke because an upstream team renamed an output. This page covers the core building blocks (plan, apply, variables, locals, outputs, data sources) exactly as the Terraform documentation defines them, then the scaling problem every growing platform team hits: sharing values between many configurations with terraform_remote_state.

The one idea to keep: state is a single document. The terraform_remote_state documentation warns that anyone who can read a configuration's root module outputs can also read its full state snapshot. Everything about how mature setups share data follows from that, plus the coupling it creates.

How a change flows through Terraform

Every change follows the same loop. The configuration, the variables and the prior state feed terraform plan; terraform apply carries out the plan and writes a new state snapshot; and that snapshot's root outputs are what other configurations may read.

Key concepts

  • Saved plan
  • Lineage and serial
  • Refresh-only mode
  • Variable precedence
  • Sensitive
  • Ephemeral value
  • Data source
  • terraform_remote_state
  • tfe_outputs
  • Run trigger

Interview questions

  • Explain the actual failure mode of using terraform_remote_state data source to share outputs between many terraform configurations at scale and what you'd use instead in a mature setup
  • What's a data source in Terraform, how is it different from a resource block
  • Difference between terraform plan and terraform apply
  • Purpose of variables and output blocks in terraform
  • What does terraform plan -refresh-only do, and why was terraform refresh deprecated?
  • A variable marked sensitive still leaked. Where can a sensitive value still appear, and how do you keep it out?
  • A variable has a value you did not expect in production. How do you find where it came from?
  • How do you run plan and apply safely in a CI/CD pipeline?
  • When does Terraform read a data source during apply instead of plan, and what does depends_on do to it?
  • Two HCP Terraform workspaces must share a value, and the consumer must update when the producer changes. How do you set it up?

Sign up free to read the full topic

More DevOps topics

Start free on Cariara · Coding problems · System design studio