Security
A comprehensive guide to securing your Terraform configurations and infrastructure deployments.
Provider Authentication
Secure Credentials Management
- Never Store Credentials in Code
# DON'T do this provider "aws" { access_key = "AKIAIOSFODNN7EXAMPLE" # WRONG! secret_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" # WRONG! }Instead, use:
- Environment variables
- Instance profiles/managed identities
- Vault integration
- Cloud-native credential management
-
Use Provider Authentication Best Practices
AWS Example:
provider "aws" { region = "us-west-2" assume_role { role_arn = "arn:aws:iam::ACCOUNT_ID:role/ROLE_NAME" } }Azure Example:
provider "azurerm" { features {} use_msi = true }
Infrastructure Security
Network Security
- Default Security Groups
resource "aws_security_group" "default" { ingress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] # WRONG! } }Instead:
resource "aws_security_group" "secure" { ingress { from_port = 443 to_port = 443 protocol = "tcp" cidr_blocks = [var.allowed_cidr] } } - Network Isolation
- Use private subnets for resources
- Implement proper network segmentation
- Use VPC endpoints where possible
Access Management
- IAM Best Practices
- Use least privilege principle
- Implement role-based access control
- Regular rotation of access keys
- Enable MFA for user accounts
- Resource Policies
resource "aws_s3_bucket" "secure_bucket" { bucket = "my-secure-bucket" versioning { enabled = true } server_side_encryption_configuration { rule { apply_server_side_encryption_by_default { sse_algorithm = "AES256" } } } }
Code Security
Secret Management
- Use Secret Management Tools
- HashiCorp Vault
- AWS Secrets Manager
- Azure Key Vault
- Google Secret Manager
- Sensitive Data Handling
variable "database_password" { type = string sensitive = true }
Module Security
- Module Source Control
module "secure_vpc" { source = "git::https://github.com/example/terraform-modules.git//vpc?ref=v1.2.3" } - Version Pinning
- Pin provider versions
- Pin module versions
- Use checksums for external modules
Compliance and Auditing
Compliance Controls
- Resource Tagging
resource "aws_instance" "example" { tags = { Environment = var.environment Owner = var.owner CostCenter = var.cost_center Compliance = var.compliance_level } } - Compliance Validation
- Use terraform-compliance
- Implement OPA/Conftest
- Regular security scanning
Audit Logging
- Enable Provider Logging
- AWS CloudTrail
- Azure Activity Logs
- GCP Audit Logs
- Infrastructure Changes Tracking
- Use detailed commit messages
- Implement change management
- Track state changes
Security Testing
Automated Security Checks
- Static Analysis
- tfsec
- checkov
- terrascan
- Dynamic Testing
- Inspec
- ServerSpec
- Custom validation scripts
Incident Response
Security Incident Handling
- Preparation
- Document emergency procedures
- Maintain backup states
- Keep destruction procedures ready
- Recovery
- State recovery procedures
- Infrastructure rebuild process
- Secure state restoration
Security Checklist
- Secure credential management implemented
- Network security controls in place
- IAM policies follow least privilege
- Secret management solution integrated
- Module sources verified and pinned
- Compliance controls implemented
- Audit logging enabled
- Security testing automated
- Incident response procedures documented
- Regular security reviews scheduled