Teams should fetch sensitive values only through ephemeral resources, then pass them into provider blocks, locals, or other ephemeral contexts. The key control is to keep secrets out of plan and state artifacts entirely, because those files are commonly copied, reviewed, and stored widely. That reduces blast radius, limits accidental exposure, and makes secret handling fit GitOps workflows more cleanly.
Using Ephemeral Resources to Keep Secrets Out of Terraform State
terraform state is not just an internal bookkeeping file. It becomes a durable copy of anything the provider, data source, or module logic persists, which is why secret placement matters more than secret formatting. Ephemeral resources shift sensitive retrieval into a short-lived context so the value can be used during execution without being written into plan or state artifacts. That is the practical difference between “managed in code” and “leaked through infrastructure metadata.”
For teams standardising on GitOps, the main benefit is reducing the number of places a secret can escape review boundaries. If a value lands in state, it can be replicated to remote backends, shared in drift tools, exported into CI logs, or read by operators who never needed the secret itself. The Ultimate Guide to NHIs — Static vs Dynamic Secrets is useful background because the same lifecycle logic that favours dynamic credentials also applies here: the shorter the lifetime and the narrower the exposure surface, the less likely the secret is to outlive the workflow that needed it.
In practice, many teams discover this only after a state backend, review export, or access grant has already made the value broadly retrievable.
How Ephemeral Secret Handling Works in Practice
The operational pattern is straightforward, but the boundaries matter. An ephemeral resource should retrieve or derive the sensitive value at execution time, hand it to the consuming block long enough for the provider to authenticate or configure the resource, and then disappear without persisting the value in state. That usually means the secret should never become a normal input variable, local value, or output. The consuming provider may still need the value in memory for a moment, but the workflow should prevent Terraform from treating it as durable configuration.
Teams should verify three things: first, that the secret source itself is not being mirrored into the state backend; second, that no downstream module or output reintroduces persistence; and third, that the provider or resource actually supports ephemeral handling rather than silently copying the value into state. If any of those steps fail, the design is only secret-adjacent, not secret-safe. For broader context on the exposure pattern, NHIMG’s Guide to the Secret Sprawl Challenge frames why centralised control matters when secrets move through many automation layers.
- Keep the secret source outside of long-lived Terraform variables and outputs.
- Use the ephemeral mechanism only at the exact point the provider needs the value.
- Check plan, state, and backend exports for accidental serialization after each change.
- Prefer short-lived credentials when the target system supports them, because persistence risk drops with lifetime.
The model works best when the secret is consumed once, during a narrowly bounded operation, and then discarded; it breaks down when modules reuse the same value across multiple resources because repetition increases the chance of serialization or logging.
Common Variations and Edge Cases
Tighter secret isolation often increases design complexity, so teams need to balance lower blast radius against provider compatibility and operational friction. Some resources accept ephemeral values cleanly, while others still expect configuration to be present in state for refresh, diffing, or drift correction. Current guidance suggests treating those cases as exceptions to investigate, not as proof that the control is unnecessary.
A common edge case is when the secret is not the final credential but an intermediate bootstrap token used to mint a longer-lived value. In that pattern, the bootstrap secret still deserves ephemeral handling even if the resulting credential is stored elsewhere, because the highest-risk step is the one that opens the door. Another frequent mistake is assuming that marking a value sensitive is enough; sensitive output redaction does not stop state persistence, it only hides the value from casual display. For teams facing broader secret handling complexity, the 2024 Non-Human Identity Security Report highlights why dynamic ephemeral credentials are increasingly valued in environments with inconsistent workload identity practices.
When the workflow requires human review, do not move the secret into a manual approval step just to avoid state exposure, because that usually expands access without reducing persistence. The better trade-off is to keep the secret ephemeral, make the retrieval auditable, and limit where the value can exist at all.
Risk and Threat Considerations
Terraform state is a high-value exposure point because it concentrates infrastructure metadata, access paths, and sometimes credentials in a durable artifact that is often replicated across teams and tools. The risk is not limited to accidental disclosure. If an attacker or unauthorised insider reaches state storage, they may obtain the exact sensitive values needed to move from infrastructure visibility into privileged access.
Failure mechanism: Secrets become exposed when they are written into state by a provider, echoed in a plan artifact, retained in a remote backend, or copied into CI/CD logs and review systems. Once stored, they can be reused long after the original operation is complete, which defeats the security value of short-lived retrieval.
Impact: Exposure can enable credential theft, unauthorized infrastructure changes, lateral movement into cloud services, and broader compromise of systems that trust the leaked secret. The longer the credential remains valid, the larger the recovery burden and the harder it becomes to prove the environment is clean.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
OWASP Non-Human Identity Top 10 and MITRE ATT&CK address the attack and risk surface, while CIS Controls v8 and NIST CSF 2.0 set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| OWASP Non-Human Identity Top 10 | NHI-01 — Secrets and Credential Management | Ephemeral resources are used to keep machine secrets out of durable state artifacts. |
| Recommendation — Use ephemeral credential handling to prevent secrets from being persisted in Terraform state. | ||
| CIS Controls v8 | 6.3 — Access Control Management | Limits who can access state, backends, and automation paths containing sensitive data. |
| 8.2 — Audit Log Management | Supports detection of secret exposure through logs, plans, and workflow artifacts. | |
| Recommendation — Restrict access to state backends and CI systems that can expose sensitive Terraform data. Monitor Terraform pipelines and logs for accidental secret serialization or disclosure. | ||
| MITRE ATT&CK | T1552 — Unsecured Credentials | State files can become a repository of credentials that attackers or insiders can harvest. |
| Recommendation — Hunt for credentials stored in infrastructure artifacts and remove any exposed secrets quickly. | ||
| NIST CSF 2.0 | PR.AC — Access Control | State backends and automation paths need least-privilege access to reduce exposure. |
| Recommendation — Apply least-privilege access to Terraform state storage and automation systems. | ||
Practitioner Guidance
What to prioritise: Treat state exposure as the primary control objective, not secret masking. If a secret can authenticate to a production system, prioritise ephemeral retrieval and blast-radius reduction before debating whether the value is “sensitive enough” to keep out of outputs.
What to verify: Confirm that the chosen resource path does not serialize the secret into state, and validate this with an actual plan-and-apply test against the real backend. Teams often underestimate how many adjacent layers can reintroduce persistence, especially wrappers, modules, and policy checks.
What good looks like: The secret exists only long enough to complete the provider action, no human-readable artifact can recover it from plan or state, and the operational team can rotate or revoke it without editing code in multiple places.
Practitioner takeaway: The right design is not “Terraform can use secrets safely,” but “Terraform should only touch secrets in a way that leaves no durable copy behind.”
Related resources from NHI Mgmt Group
- How should teams keep OpenTofu secrets out of state files?
- What breaks when teams keep hardcoding secrets inside Terraform modules or state files?
- What happens when secrets management is too complex for developers and platform teams to use consistently?
- Why do Terraform state files matter for secrets management?
Deepen Your Knowledge
Reviewed and updated by the NHIMG editorial team on September 9, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org