Never commit secrets into version control. If you do, treat those credentials as compromised and rotate them immediately.
TF_VAR_<variable_name> to input variables in your configuration.
Local shell example:
sensitive = true. Do not set a default for secrets you expect to supply at runtime:
- Secrets aren’t stored as files on disk; they live only in memory for the shell session or CI job.
- They can’t accidentally be committed to Git, removing the most common leakage vector.
- CI/CD systems (GitHub Actions, GitLab CI, Jenkins, CircleCI, etc.) provide encrypted secret management and inject secrets as environment variables at runtime.
- Marking variables with
sensitive = truegives additional protection by hiding values in Terraform outputs and logs.
| Concern | Local environment variables | CI/CD secrets | Terraform state |
|---|---|---|---|
| Risk of being committed to Git | Low | Low (encrypted at rest) | High — state can contain secrets |
| Storage location | Memory of shell session | CI provider’s encrypted store | State file (remote or local) |
| Rotation / access control | Manual | Managed by provider (recommended) | Requires state handling best practices |

TF_VAR_* environment variables:
For production, store secrets in your CI/CD system or a managed secrets store (for example, AWS Secrets Manager or HashiCorp Vault) and inject them as environment variables at runtime.
- Using remote/state locking backends (e.g., Terraform Cloud, S3 with DynamoDB locking).
- Enabling encryption at rest for the backend.
- Limiting who can read the state.
- Avoiding placing secrets in resource arguments where possible — use external secret sources.
- Do not store secrets in Terraform variable files tracked by Git.
- Use environment variables (
TF_VAR_*) locally and inject secrets via CI/CD. - Mark sensitive variables with
sensitive = true. - Protect Terraform state separately (remote backend, encryption, access control).
- Consider external secret managers (Vault, AWS Secrets Manager, etc.) to avoid ever storing secrets in Terraform configurations or state.
- Terraform input variables: https://www.terraform.io/docs/language/values/variables.html
- GitHub Actions secrets: https://docs.github.com/en/actions/security-guides/encrypted-secrets
- AWS Secrets Manager: https://aws.amazon.com/secrets-manager/
- HashiCorp Vault: https://www.vaultproject.io/