advanced31 min studyLesson 36 in Engineering

Infrastructure as code

Use declarative infrastructure with explicit state ownership, reviewed plans, bounded blast radius, and recovery from partial execution.

On this page

What you’ll understand

  • Explain the relationship between configuration, state, provider observation, and a plan.
  • Recognize why an apply is not a global transaction over cloud resources.
  • Choose change boundaries from dependencies and failure consequences.

Infrastructure code makes intent reviewable, while remote systems retain their own state and failures. A reviewed change can partially succeed, encounter drift, or lose its response after creating a resource. Preserve a trustworthy mapping between intent and reality while limiting the unrelated services one mistake can affect.

Understand what state connects

Terraform state records the correspondence between declared resource instances and remote objects, along with information needed for operations. Configuration alone does not always identify which existing object belongs to which declaration. Treat state as an operational artifact with controlled access, backup, and ownership. If several operators can modify the same infrastructure, choose a backend and workflow with appropriate locking support rather than assuming every state location serializes writers.

Assign one authoritative owner for each remote object's intended fields. Two independent state files managing the same object's lifecycle can produce contradictory plans. Separate environments and teams where their failure boundaries justify separation, while making shared dependencies explicit. A reusable module does not automatically create a state boundary or an independent release process. Name those boundaries so an on-call engineer knows which operation can affect which production services.

Δ=plan⁡(C,S,O)\Delta=\operatorname{plan}(C,S,O)
C is configuration, S is recorded state, and O is the provider's observed remote information. This conceptual notation emphasizes inputs to a proposed change; it is not a specification of Terraform's internal planning algorithm.↔ Scroll wide equations horizontally.

Review effects and handle partial application

A plan describes proposed operations against the information available during planning. Review replacements, deletions, identity changes, and dependencies, not just the count of added resources. A saved plan supports an approval workflow, but it does not freeze remote services or guarantee that every later API call succeeds. Sensitive values may appear in plan or state artifacts, so their storage and sharing need the same deliberate access policy as other operational secrets.

An apply across several providers is not one atomic database transaction. If creating a network succeeds and creating a database fails, recovery must reconcile that partial outcome. Preserve diagnostic state and inspect the next plan before retrying. Hand-editing remote resources during an incident may be necessary, but record the intervention and reconcile it afterward. A break-glass workflow that permanently leaves code and reality disagreeing creates uncertainty for the next apparently harmless deployment.

Choose a change size with a defensible blast radius

Combine resources that must evolve together, and separate unrelated high-impact systems when the coordination cost is acceptable. A single global apply is convenient until a provider failure blocks changes to every team. Extremely fragmented states can create their own brittle dependency network. Evaluate the duration of locks, frequency of cross-boundary changes, and restoration complexity. The objective is a manageable unit of ownership and recovery, not the maximum possible number of repositories or state files.

A simple probability calculation can illustrate why a larger batch creates more opportunities for trouble, but real failures are often correlated. Provider outages and a shared template bug can affect all resources at once. Use review gates, policy checks, a test environment, and small production steps to test actual mechanisms. Keep a rehearsed recovery path for state loss and accidental deletion; storing the declaration is not a substitute for protecting the data held by the declared service.

P(at least one issue)=1−(1−p)nP(\text{at least one issue})=1-(1-p)^n
This illustrative model assumes n independent changes, each with the same issue probability p. It demonstrates batch exposure only; correlated changes and unequal consequence make it unsuitable as a production risk score.↔ Scroll wide equations horizontally.

Compare batch exposure and recovery

For a teaching model, each of eight independent resource changes has a 2% probability of an issue. Compare the probability of any issue in one eight-change batch with one two-change batch. Then reason about a network creation that succeeded before a database operation failed.

  1. For eight independent changes, the no-issue probability is 0.98⁸ ≈ 0.8508. The probability of at least one issue is therefore approximately 14.92%.

  2. For two changes, the same calculation gives 1 − 0.98² = 0.0396, or 3.96%. Four two-change batches still contain eight total changes; splitting does not magically erase their total issue probability.

  3. Smaller batches can reduce the number of simultaneous unresolved effects and give observation between steps. Those operational benefits are distinct from changing the per-change probability in the toy calculation.

  4. When the network exists but the database failed, preserve the state and inspect actual remote objects. Generate a new reviewed plan that reflects the partial outcome rather than assuming the entire first apply rolled back.

The result

Batch boundaries change operational exposure and feedback opportunities; partial remote effects still need explicit reconciliation.

Explore the model

Interactive lab

Interactive lab · probability

Explore independent issue counts

Use success probability p as the probability of an issue and n as independent changes. The exact binomial bars show the count distribution, mean np, and variance np(1 − p). There is no resampling, provider graph, rollback engine, or correlated-failure model.

Explore independent issue countsThese are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Current readings: Mean · np: 5; Variance · np(1−p): 2.5; Total probability: 1. All parameters are adjustable with the labeled controls below.03581000.070.150.220.3Successes kP(X = k)k = 0 · P = 9.77e-4
Exact probabilityHighlighted outcomeMean np
Mean · np
5
Variance · np(1−p)
2.5
Total probability
1
t = 0 s

These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA probability model explains its assumptions; it cannot replace reviewing dependency and consequence.

Check your understanding

A reviewed infrastructure apply fails after creating two of three resources. Which assumption is unsafe?

Try it yourself

Under the independent equal-probability model, calculate the probability of any issue across five changes with p = 0.01. Would moving a common flawed module into five separate state files make those failures independent?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Subtract the probability of five issue-free changes from one. Then distinguish a storage boundary from a shared cause.

Compare with the solution

The modeled probability is 1 − 0.99⁵ ≈ 0.04901, or 4.90%. Five state files do not remove a common template bug or provider outage; failures caused by that shared mechanism can be strongly correlated. Separate states may improve ownership or limit an individual apply's scope, but correlation and recovery must be evaluated from actual dependencies.

Take it with you

  • State is a controlled mapping between declared instances and remote objects.
  • A plan is reviewable intent; applying it can leave partial effects.
  • Choose state and change boundaries from ownership, dependencies, and recovery.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.