متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 160IaC · Terraform

How to keep secrets out of Terraform

Database passwords and API keys end up in Terraform in three places: the code (.tf, .tfvars in Git), the plan output, and the state file — where Terraform stores resource attributes in plain text. This how-to keeps secrets out of Git, hides them in output with sensitive, reads them from a secret manager, uses Terraform's newer ephemeral and write-only features to keep them out of state where providers support it, and protects the state for the rest.

  • 5 الدروس
  • 6 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

160.1

The quick answer

variables.tf
variable "db_password" {
  type      = string
  sensitive = true
}
export TF_VAR_db_password="$(vault kv get -field=password secret/shop/db)"
terraform plan -out=tfplan

The value comes from a secret store at run time — never from a file in Git — and sensitive hides it in plan and output.

160.2

Step by step

  1. Never in Git — no secrets in .tf or .tfvars; add *.auto.tfvars with secrets to .gitignore; scan with Gitleaks/Trivy:
trivy config --scanners misconfig,secret .
  1. **sensitive = true** on variables and outputs — hides values in plan, apply and terraform output (they are still in the state):
outputs.tf
output "db_endpoint" { value = aws_db_instance.main.endpoint }
output "db_password" {
  value     = var.db_password
  sensitive = true
}
  1. Read secrets from a secret manager with a data source instead of passing them in:
secrets.tf
data "aws_secretsmanager_secret_version" "db" {
  secret_id = "shop/prod/db"
}
 
locals {
  db = jsondecode(data.aws_secretsmanager_secret_version.db.secret_string)
}

(On Azure: azurerm_key_vault_secret; with Vault: the vault provider.)

  1. Keep secrets out of state where possible:
  • Ephemeral values/resources (Terraform 1.10+) exist only during the run and are never written to plan or state.
  • Write-only arguments (Terraform 1.11+, for example password_wo on some AWS resources) send a value to the provider without storing it.
  • Or let the cloud manage the secret: for example RDS manage_master_user_password = true stores the password in Secrets Manager, not in your state.
  1. Protect the state anyway — remote backend with encryption, versioning, locking, and few people/roles with read access. Treat state like a password store.
  2. Generated secrets (random_password) are stored in state — fine only when the state is well protected; prefer cloud-managed secrets.

160.3

Check that it worked

git grep -i password finds no values, terraform plan shows (sensitive value), and — for ephemeral/write-only/managed secrets — terraform state pull | grep -c 'the-secret' returns 0.

160.4

Common problems

ProblemCauseFix
Password visible in CI logsOutput or variable not sensitive.Mark it sensitive; never echo it.
Output refers to sensitive values errorA non-sensitive output uses a sensitive value.Mark the output sensitive = true (or do not output it).
Secret found in the state fileNormal for most attributes.Restrict state access; use ephemeral/write-only/managed secrets where supported.
.tfvars with secrets committedConvenience file in the repo.Rotate the secret; remove from history; use TF_VAR_ from a secret store.
nonsensitive() used to "fix" an errorUnhides the secret.Do not; keep the value sensitive.
Different people need different secretsOne state with everything.Split states; least-privilege access per state.

الأوامر في هذا الدرس

160.5

Go further

1Does sensitive = true keep a value out of the state file?

No — it only hides it in plan and output; the state still has it.

2Name two ways to keep a secret out of state.

Ephemeral values/write-only arguments (newer Terraform, provider support needed), or a cloud-managed secret (for example RDS managed master password).

كل الأوامر في هذا الفصل