Terraform: plan, review, apply¶
Liftway never applies Terraform without a review. A plan runs in one pod, Liftway stores the binary plan encrypted, a person reviews the diff and approves it with a comment, and a second pod applies exactly that saved plan.
launch ─► plan pod ─► plan saved (encrypted) ─► waiting for review ─► approve + comment ─► apply pod ─► outputs, new state version
└─► reject or 24 h pass ─► failed, nothing applied
Before you start (administrator)¶
- A Kubernetes execution environment whose image contains Terraform. The default Liftway image ships Terraform 1.13.5; plan and apply always use the same image.
- A stable encryption key shared by all controllers (Helm
terraform.encryptionSecret). It encrypts saved plans, lock files and managed state. Losing it makes them unreadable. See Backup. - Runner pods must reach the controller's internal URL (Helm
terraform.internalURL, by default the controller Service over HTTP inside the cluster).
Prepare the repository¶
Commit the Terraform code and .terraform.lock.hcl. Plan and apply initialise providers from the same lock file; apply runs terraform init -lockfile=readonly.
For Liftway managed state (recommended), declare an empty HTTP backend:
Liftway injects the backend address, lock endpoints and a short-lived per-job credential through TF_HTTP_* variables. Never write backend credentials into HCL.
For External backend, configure your backend as usual and provide its credentials as environment-variable credentials. Plan and apply refuse to run without a remote backend (Remote backend required for plan/apply: runner storage is ephemeral.); with managed state they also require the http backend block above.
Create the template¶
Templates → Add job template → Terraform:
| Field | Value |
|---|---|
| Project | The synced repository |
| Path | Directory with the root module, e.g. envs/stage/network, or . |
| Command | Plan → Review → Apply (or Validate to only validate) |
| State | Liftway managed state or External backend |
| Workspace | A stable name such as hetzner-stage-network; empty means the template ID |
| Execution environment | Kubernetes, with Terraform |
| Credentials | Environment-variable credentials, e.g. HCLOUD_TOKEN |
| Extra variables | Become variables.tfvars.json, passed with -var-file |
The workspace is the identity of the state. Two templates with the same workspace share one state — useful on purpose, dangerous by accident. Do not rename a workspace that already manages infrastructure: a new name means an empty state.
Older templates with the command Apply behave exactly like Plan → Review → Apply. There is no immediate apply and no Destroy action.
Review the plan¶
Launch the template. The plan pod runs terraform plan and the step changes to waiting with the badge Plan review. Open the step's Terraform tab:
- resources grouped by action — add, change, replace, destroy, read, forget — searchable and paged;
- for each resource, before/after attributes with Terraform's sensitive values masked, unknown values marked, and known credential values masked too;
- changes to outputs;
- Raw log with the plan's text output.
Previews are capped at 300 attribute rows per resource and 10,000 resource rows per plan. They are encrypted at rest. Raw plan JSON is not logged.
Read destructive actions twice. A replace of a load balancer or a destroy of a server is shown clearly — Liftway will apply exactly what you approve.
Approve or reject¶
Approve & Apply requires a comment (up to 2,000 characters). The approval records who, when, the comment and the SHA-256 of the saved plan, and is valid for 24 hours. The apply pod then:
- downloads the encrypted plan and lock file from the controller using its own scoped, expiring capability;
- runs
terraform applyon the saved plan file — it does not plan again; - writes state through the controller's locking HTTP backend;
- captures
terraform output -json, masks sensitive and credential values and stores an encrypted snapshot.
Reject, or no decision within 24 hours, fails the step. Nothing is applied.
When no review is needed¶
If Terraform reports that the plan is not applyable (applyable: false, exit code 0 — nothing to do), the step succeeds without an approval or an apply pod. An empty resource list alone is not enough: output-only changes, imports, moves and state changes still go through review.
Stale plans¶
If another apply changed the same state after your plan was saved, applying the old plan fails. Liftway never silently re-plans. Launch a new run and review the new plan.
Managed state¶
Managed state lives in PostgreSQL, encrypted with the Terraform key:
- native HTTP locking: one writer per workspace at a time;
- lineage and serial checks reject state that does not belong or is older;
- a version history: every write is kept as a new encrypted version.
Run retention deletes saved plans with old runs but never deletes state or state versions. There is no automatic pruning of state history yet.
State inspector¶
State on the Terraform tab shows, read-only: storage, serial, lineage, last update, lock owner, up to 200 tracked resource addresses and the latest 10 versions. It never shows raw state JSON or attribute values. There is no download, restore or force-unlock button.
Outputs¶
After a successful apply the Terraform tab shows Outputs · Apply result, from the outputs captured for that apply (not the latest state of some other run). Sensitive outputs and known credential values are masked. If output capture fails, the apply itself still counts as successful.
From Terraform outputs to Ansible inventory¶
In a workflow, a Build inventory step turns the runway_hosts output of one managed-state Terraform step into an inventory for the Ansible steps that follow. New servers are created and configured in the same run, without copying addresses between tools.
Declare the output as a map:
output "runway_hosts" {
value = {
for name, server in hcloud_server.node : name => {
ansible_host = server.ipv4_address
groups = [server.labels.role]
vars = { zone = server.location }
}
}
}
Which becomes:
all:
hosts:
cp-fsn1-0: {ansible_host: 10.20.0.11, zone: fsn1}
children:
control_plane:
hosts:
cp-fsn1-0: {}
Rules, checked by the Build inventory step:
| Item | Rule |
|---|---|
| Output | Named runway_hosts, not marked sensitive |
| Size | 1–1,000 hosts; the resulting inventory at most 200 KB |
| Host names | Start with a letter or digit; letters, digits, _ . -; up to 128 characters; not localhost, all or ungrouped |
| Fields | Only ansible_host, groups, vars |
ansible_host |
A valid IPv4 or IPv6 address (not a DNS name) |
groups |
Up to 64 per host; valid Ansible group names; not all or ungrouped; no group named like a host |
vars |
Names use letters, digits and _, start with a letter and do not start with ansible_; values are strings, numbers or booleans; no {{ }} or {% %} templates |
The step's output lists the hosts, addresses and groups it produced. If the rules are not met, the step fails with the reason and the Ansible steps after it are skipped.
When an apply goes wrong¶
- The pod died during apply. The workspace may stay locked. Do not force-unlock while an apply might still be running. Unlocking needs an operator with database access — there is no UI for it.
- Cloud changed but state was not saved. Terraform could not persist state (for example, PostgreSQL was unreachable). Recover manually with Terraform (
import,statecommands) in a controlled session before running Liftway again. - Cancel during apply interrupts Terraform. Inspect state and the real infrastructure before the next run.
Keep PostgreSQL available during applies, and back up PostgreSQL and the encryption key.
Separating planners from approvers¶
With roles, give the people who launch templates the launch permission, and give a different group POST /api/runs/{run_id}/nodes/{node_id}/approval. Without roles (only the built-in administrator or an API token), whoever can launch can also approve.
A safe first test¶
Start with the Hetzner pilot in the backend repository (examples/terraform/hetzner), which creates a single network and no servers, in a dedicated workspace and test project. Or use the terraform_data resource, which creates nothing outside Terraform.