> ## Documentation Index
> Fetch the complete documentation index at: https://docs.levelrail.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How IaC apply works

> How files become a plan, an approved revision and a ledger of owned resources, and how prune and drift use that ledger. One page, one diagram.

There is no state file. The control plane keeps a ledger of what each stack owns, and every step below reads live state through the ordinary API with your token.

```mermaid theme={"dark"}
flowchart LR
  F["YAML files in git"] --> P["plan"]
  P -->|"exit 2: changes"| A{"approval<br/>required?"}
  A -->|no| AP["apply"]
  A -->|yes| PE["pending revision"] -->|"another admin approves"| AP
  AP --> L[("ledger of owned resources")]
  L --> PR["prune: deletes only what the stack owns"]
  L --> D["drift check: live state vs latest revision"]
  D -->|"differs"| W["warn or lock"]
```

## Files to plan

Every document is checked against the [schema](https://docs.levelrail.com/iac/reference.md) first. If any document has an issue, nothing is planned. Then live state is read with your permissions and a plan is computed: create, update with a field level diff, delete (only with prune) or no change. Env values show as `(hidden)`. `levelrail-cli diff -f infra/` is a plan that exits 2 when something would change.

The plan carries a hash. The CLI, dashboard and MCP send it back so an apply proceeds only if live state still matches what was reviewed.

## Approve and apply

Apply runs each item through the existing endpoint and is safe to repeat: a re-run plans again and does only what is missing. The first failed item stops the run unless `--continue-on-error` is set. `--wait` (on `apply` and `stack apply`) then polls the apps the run wrote until each is running or failed, prints the failure reason per app and exits 1 when any failed; without it a pull error shows up later as `Attention needed`. A stack can require approval, in which case an apply is stored as a pending revision until another administrator approves it. In apply mode, exit 2 then means "awaiting approval". Details: [Stacks](https://docs.levelrail.com/stacks.md#approvals).

## Ledger and prune

A stack owns the resources it created or adopted. Prune deletes only owned resources the files no longer declare, and never projects or databases. A stack must allow prune, and a large delete needs an explicit confirmation. Things made by hand, or by another stack, are never touched. See [Ownership and prune](https://docs.levelrail.com/stacks.md#ownership-and-prune).

## Drift

A checker plans each stack's latest revision against live state on a timer. A difference is recorded and raises an attention item (`warn`), or also refuses manual edits to owned resources (`lock`). See [Drift](https://docs.levelrail.com/stacks.md#drift).

## Values per environment

`${{ env.NAME }}` placeholders are filled at apply time, in this order: `--var`, `--values FILE`, `--var-file`, then names allowed with `--allow-env`. The control plane never reads its own environment for them. Secrets are never in the files: refer to them with `secretRef` and pass values with `--secret`. See [Platform as code](https://docs.levelrail.com/platform-as-code.md#secrets).

## Exit codes

| Code | Meaning |
| - | - |
| 0 | No changes, or the changes were applied |
| 1 | An error: invalid files, a failed or denied item, an unreachable server |
| 2 | Changes are pending (`diff`, `--dry-run --exit-code`), or in apply mode a revision awaits approval |

Error codes on plan items and API errors are listed in [IaC errors](https://docs.levelrail.com/iac/errors.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.