هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
Step by step
- Never in Git — no secrets in
.tfor.tfvars; add*.auto.tfvarswith secrets to.gitignore; scan with Gitleaks/Trivy:
trivy config --scanners misconfig,secret .- **
sensitive = true** on variables and outputs — hides values in plan, apply andterraform output(they are still in the state):
output "db_endpoint" { value = aws_db_instance.main.endpoint }
output "db_password" {
value = var.db_password
sensitive = true
}- Read secrets from a secret manager with a data source instead of passing them in:
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.)
- 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_woon 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 = truestores the password in Secrets Manager, not in your state.
- Protect the state anyway — remote backend with encryption, versioning, locking, and few people/roles with read access. Treat state like a password store.
- Generated secrets (
random_password) are stored in state — fine only when the state is well protected; prefer cloud-managed secrets.
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.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Password visible in CI logs | Output or variable not sensitive. | Mark it sensitive; never echo it. |
Output refers to sensitive values error | A non-sensitive output uses a sensitive value. | Mark the output sensitive = true (or do not output it). |
| Secret found in the state file | Normal for most attributes. | Restrict state access; use ephemeral/write-only/managed secrets where supported. |
.tfvars with secrets committed | Convenience file in the repo. | Rotate the secret; remove from history; use TF_VAR_ from a secret store. |
nonsensitive() used to "fix" an error | Unhides the secret. | Do not; keep the value sensitive. |
| Different people need different secrets | One state with everything. | Split states; least-privilege access per state. |
Go further
- Command pages: terraform, vault, trivy.
- Next how-tos: Generate a strong password or secret, Store Terraform state in S3 with locking, Remove a secret from Git history.
- Learn it: Terraform — Sensitive values, No secrets in code, Vault, Secrets in state, Scanning.
- Full guides: Vault, Checkov.
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).