Sensitive values and persisted artifacts
A hidden value is not a protected file
Terraform can hide a confidential value in ordinary console summaries while retaining that value in its artifacts. This is like covering a line on a printed report without removing it from the underlying document. Redaction protects a display; encryption protects stored or transmitted bytes against readers without keys; access control determines who may read or change them. They solve different problems. A local state file is normally plaintext. Treat saved plans, state and their copies as confidential, regardless of what the terminal shows.
Mark an input and output as sensitive
The provider-free lab below uses an invented marker, not a credential. It requires the existing provisional Terraform 1.14.8 pin and a fresh local directory. The built-in record stores its input. The output also carries the sensitive designation.
terraform { required_version = "= 1.14.8"}variable "marker" { type = string sensitive = true}resource "terraform_data" "record" { input = var.marker}output "record_marker" { value = terraform_data.record.output sensitive = true}In an authorized disposable copy, initialize, review and apply only the local record with an explicitly fake input supplied at the prompt. The expected summary masks the marker; the expected state contains it. Neither result has been observed here. The sensitive annotation is propagated through expressions; do not strip it just to make a plan easier to read. A root output carrying sensitive data needs its own sensitive declaration. The ordinary all-output summary redacts it; targeted output queries and raw or JSON formats can disclose it. Do not use those forms on real secret-bearing outputs.
Trace the data across the workflow
Consider a fictional CI runner that reads a value from a secret manager and passes it into a conventional resource argument. Even if the source is secure, the downstream resource schema may persist the value. Review the full path, not just the input method.
Secret manager -> runner memory -> Terraform evaluation -> provider request | +-> saved plan / state / backupsRunner diagnostics and service logs are separate potential paths.Variable files and command-line arguments can also leave copies on disk, in process listings or in shell history. Environment injection reduces some file and history risks but environment variables can still be inherited or exposed through diagnostics. Avoid persistent credentials in HCL, backend configuration and command arguments. Backend configuration values can be cached in local initialization metadata and embedded in plans; use the supported external credential mechanism for the chosen backend. Do not export the entire environment or enable debug tracing with secrets present.
Protect every retained copy
Use a controlled backend with encryption at rest and in transit, least-privilege read/write access, audit trails, appropriate retention and tested recovery. Verify those controls for the specific backend rather than assuming remote storage implies encryption. State locking prevents conflicting writers; it is not confidentiality. Restrict CI artifact access and lifetime, backups, logs, temporary files and crash reports. Avoid sharing state across trust boundaries merely to read one public output; publish the minimum required non-secret data separately. Ignore rules are a useful extra control, not a security boundary.
# Candidate repository ignore rules; adapt to your own conventions..terraform/*.tfstate*.tfstate.**.tfplan*.tfvars*.tfvars.jsoncrash.logcrash.*.log# Keep the reviewed .terraform.lock.hcl tracked.Keep approved non-secret example variable files separately named, and keep reviewed provider dependency locks versioned. An ignore pattern neither removes previously tracked files nor prevents forced additions. Never put actual values in a shared lab.
Respond to suspected exposure
If an actual plan or state was exposed, restrict access promptly, notify the responsible security and infrastructure owners, inventory affected credentials and copies, revoke or rotate affected credentials in a coordinated manner, review access logs, and verify recovery. Preserve evidence under the incident policy; do not indiscriminately delete state or backups. Removing a file from Git does not erase every historical copy, and changing a Terraform sensitivity flag does not retroactively protect old artifacts. Record the scope and follow up on residual copies.
Practice
Use the synthetic artifact classification worksheet. Explain separately whether a value is hidden in a display, persisted in a file, encrypted and accessible to an unauthorized actor. The next lesson addresses how to avoid persistence in certain supported contexts.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Sensitive values and persisted artifacts — knowledge check
Review sensitive and ephemeral value handling.
What does sensitive = true primarily change?
Which artifact can contain a redacted input?
What should a reviewer assume about raw or JSON output?
What does state locking establish?
What is an appropriate response to exposed real state?