prevent_destroy only stops Terraform. An API token with enough rights, or one click in the Proxmox UI, can still delete a database VM along with its disks. So the VMs that hold my databases carry two guards. One is the lifecycle rule in HCL. The other is Proxmox's own deletion protection flag, which the bpg/proxmox provider sets with protection = true.
I want the homelab in code, because it crossed the line from "a few boxes I remember how to rebuild" into real infrastructure. There are Proxmox virtual machines, NAS shares, Cloudflare DNS records, AWS bootstrap resources, Kubernetes nodes, and enough service scaffolding that rebuilding it from memory would be risky.
It also holds data I care about, and that's where a lot of infrastructure-as-code advice gets too clean. Recreate the node. Replace the volume. Destroy and apply again. That works until the thing Terraform wants to replace is a share with family documents, a database volume, a backup target, a media library, a game world, or a persistent volume an application actually depends on.
So the rule I settled on is the one in the title: rebuild the shells, protect the data.
Tap to enlarge, then pinch to zoom
The Terraform boundary follows the data
Terraform is very good at declaring desired state. It's also literal. If an object is in state but no longer in configuration, Terraform normally plans to remove it. If a provider marks an argument as requiring replacement, Terraform plans a destroy and create.
Terraform plans a destroy for a VM shell and for a NAS share full of files in exactly the same way. The mistake is treating them as if losing them costs the same.
A Proxmox VM shell before it runs a workload is cheap. It has a CPU count, memory allocation, firmware settings, network adapters, and an operating-system disk I can recreate. A NAS share with years of files isn't cheap. Neither is a database volume or a backup repository.
In cloud consulting, I see the same distinction in client environments. Nobody serious reviews an RDS database, an S3 bucket full of production data, and a disposable task definition as if they were the same kind of resource. My homelab is smaller, but the operating principle is identical. The dollar values are smaller. The recovery cost isn't.
What Terraform rebuilds in my homelab
The first category is infrastructure I'm willing to recreate.
Proxmox VM shells. The community-maintained bpg/proxmox provider manages Proxmox VE resources through its API. It's a good fit for the shape of a virtual machine: CPU, memory, boot order, clone source, empty disks, network attachments, and tags.
For example, this is the kind of configuration I use for a Kubernetes worker shell. Every identifier here is illustrative:
That VM is the chassis. Terraform can rebuild it.
Rebuildable doesn't mean Terraform gets to decide when. Both VM modules in my repo set prevent_destroy, and both tell Terraform to ignore the clone block after creation. The clone source is a record of how the VM was born. If I later move its disks, that shouldn't make a running VM look like it needs replacing. A Kubernetes worker is still replaceable, because the cluster and storage layer replicate the workload, but replacing it should be my decision and not a side effect of an unrelated argument change. I described the storage side of that decision in A Boring Kubernetes Homelab.
DNS records. Terraform is excellent at DNS. A Cloudflare record for app.example.com is declarative metadata with a small, visible diff. It belongs in code.
Bootstrap infrastructure. The AWS resources that support Terraform itself belong in code after the initial bootstrap: the state bucket, versioning, encryption settings, public access block, and IAM policies. My own state bucket breaks that rule. I created it by hand during the bootstrap, and it's still documented rather than codified. The buckets Terraform creates for Kubernetes backups, database backups and the file gateway are in code, with versioning and encryption. Small Kubernetes bootstrap resources and service scaffolding fit here too when the artifact is configuration rather than irreplaceable data.
The Proxmox side of storage, with guardrails. Terraform owns the records that tell Proxmox where its storage lives. Those are the local ZFS pools and the NFS export on the NAS that holds ISOs, snippets and backups, and every one of them carries prevent_destroy. The shares on the NAS, and who may mount them, are managed in the NAS's own console. Terraform only holds the records that point Proxmox at that storage. It doesn't manage the NAS itself.
What Terraform does not own destructively
There are things Terraform can describe but shouldn't be trusted to destroy in my environment:
- NAS shares and the files in them
- Physical disks, partitions, and ZFS pools after creation
- VM disks with application data
- Databases and database volumes
- Media libraries
- Backups and backup targets
- Game worlds
- Kubernetes persistent volume contents
A provider having a resource type for something doesn't mean I let Terraform destroy it.
Separate Terraform state by blast radius
My first instinct was a single root module: one command, one plan, one state file.
That's neat until the blast radii stop matching. A provider that can't refresh fails the plan for its whole root, so in a single root a flaky storage API would block a DNS change that has nothing to do with storage.
So the repo uses separate roots with separate state keys. As of writing there are three, and the names reflect the order the homelab grew rather than a perfect taxonomy:
Tap to enlarge, then pinch to zoom
Each root has its own state key and its own lock, so a provider failure in one root can't block a plan in another. Separate roots make it possible to give each one its own credentials, but splitting the directories doesn't do that by itself.
The S3 backend is part of the safety model
Terraform state maps code to real infrastructure. If that map is fragile, the whole setup is fragile.
I use the Terraform S3 backend with bucket versioning, server-side encryption, and native S3 lockfiles:
HashiCorp now documents use_lockfile as the S3 locking path and marks DynamoDB-based locking as deprecated. The runner needs s3:ListBucket on the bucket for the relevant prefix, plus s3:GetObject and s3:PutObject on the state object. The .tflock object also needs s3:GetObject, s3:PutObject, and s3:DeleteObject. The state object itself doesn't need delete permission for normal backend operation.
Versioning matters. HashiCorp explicitly recommends it for recovery from accidental deletion and human error. It gives me a path back if a state write goes wrong.
State is still sensitive. HashiCorp's sensitive data guidance is clear that plan and state files can contain passwords, API tokens, and other resource attributes. Marking a value sensitive redacts normal CLI output. It doesn't remove the value from state. Remote state needs narrow IAM, encryption, and versioned recovery because it's valuable data in its own right.
Provider credentials come from a wrapper, not from Terraform
I don't fetch provider credentials through Terraform data sources. Doing that makes the secret part of Terraform's evaluation, and depending on how the result is used, it can also put the value into plan or state.
Each root has a wrapper script instead. It reads one JSON secret from AWS Secrets Manager, exports the provider tokens as TF_VAR_* environment variables, and replaces itself with Terraform. Condensed, with a placeholder secret name:
On the HCL side, each provider names the variable that authenticates it, and the variable is marked sensitive:
I like that the wiring is visible. Reading providers.tf tells you exactly which input authenticates which provider. The value comes from the wrapper's environment, never from a file in the repo, and Terraform redacts sensitive values in plan output.
Redaction isn't the same as not storing, though. Terraform can write input variable values into a saved plan file unless the variable is declared ephemeral, which needs Terraform 1.10 or later. A saved plan deserves the same care as state.
Terraform and the provider plugin processes it starts can read that environment, so I still treat the entire process tree as privileged. This also doesn't make every Terraform secret ephemeral. If I pass a password into a normal resource argument, the provider may still store it in state. The rule only covers provider credentials. They shouldn't pass through Terraform data sources, where they can end up in plan or state.
Import existing objects before managing them
Most of what Terraform manages in my homelab existed before the code did. Pretending Terraform created those objects would be dishonest and dangerous.
The biggest adoption was my old TrueNAS NAS. Its datasets and NFS shares came in through the community barodeur/truenas provider, which uses the TrueNAS WebSocket API. It was the least trustworthy provider in the repo, with weak upstream maintenance signals, so it got a root of its own where a provider failure couldn't block compute or DNS plans. Before I moved those resources into that root, a refresh-only plan had to read all 18 live objects (10 datasets and 8 shares) cleanly. Then I imported each one by its exact ID and removed the same addresses from the old root's state.
That root is gone now. In August I replaced TrueNAS with a UniFi NAS, and its shares are managed in the NAS's own console, outside Terraform. I deleted the root's code and kept its last state file in the versioned bucket as a record. Nothing applies it.
Terraform's current import workflow supports declarative import blocks, which put the adoption step through the same pull request review as the resource configuration. Newer adoptions in the repo use them. My CI runners needed Proxmox roles I had created by hand, so the code declares each role and an import block adopts it. The role name and privileges here are illustrative:
The safety check was never the import itself. It was the first plan afterward. I write each resource block to match the live object, import it, and read the plan before going any further. A plan that proposes anything beyond the intended import means stop. With TrueNAS that mattered because of ZFS inheritance. The provider stored only properties with a local ZFS source, while inherited or default properties appeared as null, so I didn't fill every attribute just because the schema exposed it.
Prevent destroy is a guardrail, not a backup
prevent_destroy is useful because Terraform rejects a plan that would destroy a protected resource while that lifecycle rule is present. It turns a dangerous plan into a hard error.
It also has a limit that HashiCorp's lifecycle documentation spells out. Removing the resource configuration also removes the place where prevent_destroy is declared. The resource can still be destroyed as Terraform reconciles the configuration.
The declarative way to stop managing a protected object without deleting it is a removed block:
Terraform recommends this path over an imperative terraform state rm for normal workflows. The intent is visible in code. Terraform forgets the object and leaves the real role in Proxmox alone.
And prevent_destroy doesn't protect against application corruption, operator error on the NAS, disk failure, or a compromised credential. It doesn't even stop someone deleting the VM in the Proxmox UI, which is why the database VMs also carry Proxmox's protection flag. Snapshots, off-system backups, and tested restores still do the real work. The lifecycle rule only stops Terraform.
Why Atlantis fits this operating model
Local Terraform works when one person is careful. I wanted the plan to be visible, repeatable, and attached to code review.
Atlantis runs Terraform plans from pull requests and posts the result back to the PR. Its locking model locks a directory and Terraform workspace to that PR until the PR is merged, closed, or the plan is discarded. Terraform's backend lock still protects state during operations. Atlantis adds a higher-level lock that prevents a second pull request from planning the same directory and workspace while the first lock is held.
For the prod and tailscale roots, that gives me the review shape I want:
- Every change starts as a pull request.
- The plan is visible before an apply.
- Destructive actions appear beside the code that caused them.
- Protected storage plans are reviewed separately from rebuildable compute.
- Concurrent changes to the same root don't race each other.
There is a security catch. Atlantis plans untrusted HCL on a server that has provider credentials. Its own security documentation warns that a malicious provider or external data source can execute during plan, before an apply approval matters.
I treat Atlantis as privileged infrastructure. My server is allowlisted to a single private repository, fork pull requests are disabled, webhooks are authenticated, and plan access is limited to trusted contributors. It authenticates to AWS with IAM Roles Anywhere, so no long-lived access key sits in Kubernetes or in state. The Atlantis process still holds the credentials for every project it plans, and a malicious plan can use them.
After a successful atlantis apply, Atlantis merges the pull request and deletes the branch. Typing the apply comment is the only manual step.
Atlantis only plans when a pull request exists, so it never sees a change someone made in a console. Burrito covers that gap. It runs in the same cluster, plans the prod and tailscale roots from main every hour without applying anything, and a daily job reads those plan summaries and alerts me if either root has drifted.
Tap to enlarge, then pinch to zoom
This is the boundary I use:
Tap to enlarge, then pinch to zoom
The boundary isn't perfect. Providers change behavior, imports need care, and some resources mix rebuildable configuration with stateful disks in one schema.
Writing the boundary down still changes how I read a plan. If Terraform wants to create or update a shell, I ask whether the proposed shape matches the architecture. If it wants to destroy or replace something stateful, I stop and ask why Terraform believes it owns that lifecycle at all.