Terraform AWS IAM - Roles, Policies, and Best Practices
How to manage AWS IAM roles, policies, and permissions with Terraform following security best practices. Step-by-step guide with code examples and best pract...
Guides
Best practices for managing secrets, passwords, and sensitive data in Terraform configurations. Step-by-step guide with code examples and best practices for ...
Terraform state files contain sensitive data in plain text. API keys, database passwords, and certificates stored in state are readable by anyone with state access. Here's how to handle this securely.
# BAD - secret in code
resource "aws_db_instance" "db" {
password = "super-secret-password" # This ends up in state AND git
}
# GOOD - use a variable
resource "aws_db_instance" "db" {
password = var.db_password
}export TF_VAR_db_password="super-secret-password"
terraform applydata "aws_secretsmanager_secret_version" "db_password" {
secret_id = "prod/database/password"
}
resource "aws_db_instance" "db" {
password = data.aws_secretsmanager_secret_version.db_password.secret_string
}provider "vault" {
address = "https://vault.example.com"
}
data "vault_generic_secret" "db" {
path = "secret/data/prod/database"
}
resource "aws_db_instance" "db" {
password = data.vault_generic_secret.db.data["password"]
}# Encrypt a tfvars file
sops --encrypt --in-place secrets.tfvars
# Decrypt and apply
sops -d secrets.tfvars | terraform apply -var-file=/dev/stdinoutput "db_connection_string" {
value = "postgresql://${var.db_user}:${var.db_password}@${aws_db_instance.db.endpoint}"
sensitive = true
}
variable "db_password" {
type = string
sensitive = true # Prevents value from showing in plan output
}Even with these methods, sensitive values still exist in state. Protect state by:
*.tfstate to .gitignore.tf filessensitive = trueHow to manage AWS IAM roles, policies, and permissions with Terraform following security best practices. Step-by-step guide with code examples and best pract...
Fix the Terraform output refers to sensitive values error. Covers sensitive outputs, nonsensitive function, module outputs, and secrets management patterns.
Practical Terraform patterns to reduce AWS costs: right-sizing, spot instances, scheduling, and reserved capacity. Step-by-step guide with code examples and ...
How to achieve zero-downtime deployments with Terraform using blue-green, rolling updates, and create_before_destroy. Step-by-step guide with code examples a...