NinjaOps Guides › Infrastructure & Security
dev0ps-20).
Terraform state files (terraform.tfstate) contain every sensitive attribute managed across your cloud infrastructure — database passwords, API keys, private TLS certificates, and IAM credentials. Leaving state files in plain text on local drives or unencrypted storage buckets is one of the leading causes of cloud credential exposure.
This ultimate guide details the step-by-step playbook for securing Terraform state files and managing pipeline secrets across GitHub Actions, GitLab CI, and AWS.
Never commit state files to git repositories. Configure an encrypted remote backend using AWS S3 with server-side encryption (KMS) and DynamoDB state locking to prevent concurrent apply race conditions (such as Terraform state lock acquisition errors).
terraform {
backend "s3" {
bucket = "prod-terraform-state-ninjaops"
key = "global/s3/terraform.tfstate"
region = "us-east-1"
encrypt = true
kms_key_id = "arn:aws:kms:us-east-1:123456789012:key/abc-123"
dynamodb_table = "terraform-state-locks"
}
}
Hardcoding plain-text secrets in .tfvars files violates least-privilege security controls. Instead, fetch ephemeral credentials dynamically at runtime using HashiCorp Vault or OIDC token exchange in CI/CD pipelines.
Master Terraform state management and security architecture with these industry standards:
When running terraform plan inside GitHub Actions or GitLab CI runners, mark sensitive outputs using sensitive = true in your Terraform code. Additionally, enforce automated pre-commit hooks using trufflehog or checkov to block hardcoded keys prior to pushing.
Connect this guide with our step-by-step fix guides and infrastructure hubs: