Never hardcode credentials in Terraform files. Use secret stores and secure pipeline variables. Sensitive input handling is foundational, not optional.
Security, Secrets & Governance
Secure Terraform execution, protect secrets and state, and apply governance so infrastructure automation stays safe in multi-team environments.
🧒 Simple Explanation (ELI5)
Terraform can build powerful things, which means mistakes or weak permissions can also cause powerful damage. Security and governance decide who can run it, what they can change, and how sensitive values stay protected.
🤔 Why Do We Need It?
- Terraform can create, modify, or destroy critical infrastructure.
- State may contain secrets or sensitive metadata.
- Teams need guardrails, not just trust.
🔧 Technical Explanation
Key security domains include least-privilege identities, secure state backends, sensitive variable handling, code review, policy enforcement, and auditability. Governance may involve naming standards, tagging, policy checks, environment approvals, and module standards.
| Control Area | Example Practice |
|---|---|
| Identity | Use service principals or workload identity with limited permissions |
| Secrets | Store in Key Vault or CI secret stores, not in code |
| State | Restrict access and encrypt remote backend storage |
| Governance | Require tags, naming patterns, and pull-request review |
variable "client_secret" {
type = string
sensitive = true
}🌍 Real-World Use Case
An enterprise team uses Azure DevOps to run Terraform with an Azure service connection that can access only the target subscription scope. State lives in a dedicated storage account. Pull requests must pass validation and policy checks before any production apply is approved.
🛠️ Hands-on
Security Checklist
- Use separate identities for dev and prod where appropriate.
- Restrict state backend access to Terraform automation and a small break-glass group.
- Apply environment-specific approvals before production apply.
- Scan code for hardcoded secrets and unsafe defaults.
🐛 Debugging Scenario
Problem: A pipeline can plan successfully in dev but fails with authorization errors in prod.
- Compare identity scope and role assignments between environments.
- Check whether the prod subscription or resource group permissions differ.
- Verify secret retrieval permissions for Key Vault or the pipeline secret store.
A successful Terraform run does not prove your permissions model is good. It may simply mean the automation identity is overprivileged.
📋 Interview Questions
Beginner
Because Terraform can make high-impact infrastructure changes and may handle sensitive values or privileged identities.
Keep them out of code, use secure secret stores or pipeline secrets, and mark variables sensitive where appropriate.
Because state can reveal infrastructure details and sometimes sensitive data, making it a critical asset.
Grant only the permissions Terraform needs for the target scope and nothing broader.
Because infrastructure changes deserve peer review, risk assessment, and auditability.
Intermediate
Tag enforcement, naming standards, module standards, environment approvals, policy checks, and access controls around state and execution.
Because it increases blast radius, hides poor security design, and allows a compromised pipeline to do far more damage.
It can store secrets that pipelines or other Azure identities retrieve securely rather than hardcoding them in configuration.
Because production infrastructure changes have higher risk and should require stronger control than lower environments.
They reduce auditability and may execute with broader privileges or inconsistent context compared to controlled automation.
Scenario-Based
Rotate the credential immediately, remove it from code, update the workflow to use a secure secret source, and assess exposure history.
That is usually a weak separation model. Environment boundaries should be reflected in identity and approval design wherever practical.
It is the set of controls that ensures infrastructure automation is consistent, approved, auditable, and aligned with enterprise security standards.
Either the module standards or policy enforcement is insufficient. Governance must be embedded, not assumed.
Because sensitivity controls output display, but it does not replace secure storage, least privilege, or backend protection.
🧾 Summary
Terraform security is not only about hiding secrets. It is about controlling execution, reducing blast radius, and governing infrastructure change so automation stays trustworthy as the platform grows.