Handle Sensitive Variables Securely
Manage secrets, API keys, and passwords in Terraform without exposing them in state, logs, or version control.
Prerequisites
- -Terraform installed
- -Understanding of Terraform variables
Steps
Mark variables as sensitive
Use the sensitive flag to prevent Terraform from displaying variable values in plan output and logs.
Sensitive variables show as '(sensitive value)' in plan output, preventing accidental exposure.
Pass secrets via environment variables
Use TF_VAR_ prefixed environment variables to inject secrets without putting them in files.
Never put secrets in .tfvars files that are committed to version control. Use environment variables or a secrets manager.
Use a secrets manager data source
Fetch secrets at plan time from AWS Secrets Manager, Vault, or similar services.
Encrypt state at rest
Ensure your remote backend encrypts state files since they may contain sensitive values.
State files can contain secrets in plaintext even when variables are marked sensitive. Always encrypt state at rest.
Add .gitignore rules for sensitive files
Prevent accidental commits of files that may contain secrets.
Full Script
FAQ
Discussion
Loading comments...