Secrets in IaC are credentials such as API keys, passwords and tokens hardcoded in infrastructure-as-code files like Terraform or CloudFormation templates, variable files and pipelines. Once committed, they persist in version history, and Terraform state files can also hold them in plaintext. Defenders scan repositories, reference secrets managers instead of literals, restrict state access, and rotate any exposed value.
What secrets in infrastructure as code really create
Secrets in IaC are dangerous because they turn deployment code into a long-lived distribution channel for credentials. A value copied into Terraform, CloudFormation, variables, or pipeline config can survive beyond the original file, reappear in version history, and end up in state or logs where many more people and systems can reach it.
That persistence is what makes this issue more than a simple coding mistake. The exposure surface expands from the source file to the whole delivery path, including cloned repositories, build systems, remote state backends, review tools, and any automation that prints or caches configuration artifacts.
Why hardcoded secrets in IaC are so hard to contain
The main problem is that infrastructure code is meant to be copied, reviewed, applied, and stored, which makes accidental secret placement especially sticky. Once a literal credential is committed, the organisation often has to assume the secret is discoverable anywhere the repo, branch, cache, artifact, or state file has traveled.
Terraform state is especially sensitive because it can preserve values in plaintext even when the original template looked harmless. That means teams can “fix” the source file and still leave an exposed credential behind in backend storage, state exports, or automation outputs.
The practical lesson is that IaC should reference secrets, not contain them. A secrets manager or short-lived injected value keeps the sensitive material outside the declarative configuration and reduces the chance that code review, drift tooling, or deployment logs become a leakage path. Secrets Management Guide
How defenders find and limit exposure
Exposure checks usually start with repository scanning, but they should not stop there. Teams need an inventory of repositories, branches, build jobs, templates, variable files, and remote state locations so they can identify where a secret might exist even if it is no longer visible in the current source tree.
State access deserves special attention because it is often broader than source code access. A backend that is readable by many operators, CI jobs, or tooling integrations can turn a single hardcoded secret into a shared organisational asset, which is the opposite of how credentials should behave.
For teams that want a broader IaC control loop, IaC Scanning vs Runtime Cloud Scanning is useful because it explains why pre-deploy checks and runtime visibility both matter when configuration drift or “click-ops” can reintroduce secret exposure after review.
Remediation and recovery after a leak
When a secret has been exposed in IaC, the right response is not just deletion from the file. The exposed value should be treated as compromised, which means rotation or revocation, followed by replacement with a managed secret reference and a search for other copies in branches, forks, logs, state snapshots, and build artifacts.
Teams also need to reduce repeat exposure by changing the workflow that allowed the leak in the first place. That usually means tightening who can read state, removing plaintext variables from pipelines, and preferring dynamic or ephemeral credentials where the platform supports them.
For Terraform-specific patterns, Terraform and IaC Pipeline Credential Security Guide is a strong companion because it covers state secrets, sensitive values, encrypted backends, and dynamic access in the same operational context.
What good practice looks like in delivery pipelines
Good IaC hygiene is mostly about refusing to let secrets become part of the code artifact. That means secret scanning before merge, encrypted or restricted backends, careful handling of plan output, and a clear rule that high-value credentials are injected or fetched at runtime rather than copied into template text.
It also helps to distinguish secrets management from general configuration management. A parameter may be safe to commit, but a token, password, signing key, or API key should have a lifecycle of its own, with ownership, expiry, and rotation handled outside the IaC file.
For practitioners comparing implementation options, the API Key Management Guide is relevant because exposed IaC values are often API credentials that need scoping, revocation, and replacement, not just storage.
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 addresses the attack and risk surface, while NIST SP 800-53 Rev 5 and OWASP ASVS set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| OWASP Non-Human Identity Top 10 | NHI-02 — Secret Leakage | IaC secrets are credentials written into code and state, creating secret leakage risk. |
| Recommendation — Scan IaC, state and pipelines for embedded secrets and move credentials to managed injection. | ||
| NIST SP 800-53 Rev 5 | IA-5 — Authenticator Management | Exposed IaC secrets must be rotated, revoked and lifecycle-managed as authenticators. |
| AC-6 — Least Privilege | Restricting state and pipeline access limits who can read plaintext credentials in IaC. | |
| CM-5 — Access Restrictions for Change | IaC changes and deployment paths should enforce guardrails that prevent secret-bearing misconfigurations. | |
| Recommendation — Rotate leaked credentials promptly and manage their lifecycle outside IaC files. Limit state, build and backend access to the minimum set of identities that need it. Apply change guardrails so secret values cannot be committed or applied in templates. | ||
| OWASP ASVS | V14 — Data Protection | Secrets in code and state are sensitive data that require protected handling and storage. |
| Recommendation — Treat embedded credentials as sensitive data and keep them out of source-controlled artifacts. | ||
Practitioner Guidance
What to watch for: The highest-risk pattern is not only a visible secret literal, but any IaC workflow that serialises credentials into state, logs, previews, or exported artifacts. If those artefacts are shared more widely than the deployment target, the secret has already escaped its intended trust boundary.
Governance implication: Ownership should sit with the team that controls the pipeline and the backend, not just the author of the template. Secret handling rules need to cover code review, state retention, rotation after exposure, and access review for every storage location that can preserve IaC output.
Related resources from NHI Mgmt Group
- What should teams do when IaC secrets and state files are exposed?
- What do security teams get wrong about scanning for secrets, SAST, SCA, and IaC issues in one workflow?
- What is the difference between SAST, DAST, SCA, IaC scanning, and secrets scanning?
- What is the difference between storing secrets in IaC files and using a dedicated secrets manager?
Deepen Your Knowledge
Free weekly newsletter
Subscribe to the NHI & AI Identity Journal
The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.
Bonus 33% off our NHI Course when you subscribe.
Reviewed and updated by the NHIMG editorial team on November 26, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org