Terraform Security
Secure Terraform state, providers, modules and plan/apply workflows.
IntermediateTerraform SecurityInfrastructureCloud
Where it fits in the lifecycle
- Plan
- Code
- Build
- Test
- Release
- Deploy
- Operate
- Monitor
- Code Static analysis, secret detection and secure coding practices.
- Deploy Policy enforcement, admission control and infrastructure security.
Overview
Terraform risk concentrates in three places: state files containing sensitive values, apply credentials with broad permissions, and unpinned modules or providers.
Why it matters
State is effectively a credential store, and apply permissions are usually close to administrative.
How it works
- 01Remote state is encrypted, versioned and access-controlled with locking.
- 02Plan runs with read-only credentials; apply is a separate, approved job.
- 03Modules and providers are version-constrained and checksum-locked.
Common tools
CheckovtfsecTerraform CloudOPATerraformAWSAzureGitLab
Implementation examples
hclEncrypted remote state
terraform { required_version = "~> 1.9" backend "s3" { bucket = "acme-tfstate" key = "prod/network.tfstate" region = "eu-west-1" encrypt = true kms_key_id = "arn:aws:kms:eu-west-1:111122223333:key/..." dynamodb_table = "tf-locks" }}Best practices
- Never commit state or `.tfvars` with secrets.
- Separate plan and apply credentials.
- Pin providers in the lock file.
Common mistakes
- Using a single admin credential for both plan and apply.
Hands-on labs
- Lab 03 — Secure Terraform with Checkov
Find and fix insecure Terraform configuration before applying it.