Here is an example of an insecure Terraform configuration provisioning a publicly accessible AWS S3 bucket without encryption or access logging:
🥺 Vulnerable Code
# Vulnerable: Terraform S3 bucket with public access and disabled encryption
resource "aws_s3_bucket" "customer_documents" {
bucket = "company-customer-documents-prod"
# Insecure: Missing server-side encryption configuration
# Insecure: Missing public access block allows accidental exposure
}
resource "aws_security_group" "web_sg" {
name = "web-server-sg"
description = "Security group for web instances"
# Insecure: Ingress allows unrestricted SSH from the entire internet (0.0.0.0/0)
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}This Terraform code creates high-risk cloud misconfigurations: (1) The S3 bucket lacks an aws_s3_bucket_public_access_block, leaving it vulnerable to accidental public data leaks; (2) It lacks default server-side encryption with KMS keys, violating data protection compliance standards; (3) It omits bucket versioning and access logging; (4) The security group exposes SSH management port 22 directly to the global internet (0.0.0.0/0), exposing instances to brute-force and zero-day attacks.
😎 Secure Code
Here is the hardened Terraform code enforcing encryption, complete public block, versioning, and restricted bastion access:
# Secure: Hardened Terraform configuration enforcing S3 encryption, public block, and restricted ingress
resource "aws_kms_key" "s3_key" {
description = "KMS key for customer documents S3 bucket"
deletion_window_in_days = 30
enable_key_rotation = true
}
resource "aws_s3_bucket" "customer_documents" {
bucket = "company-customer-documents-prod"
}
# 1. Enforce strict public access block
resource "aws_s3_bucket_public_access_block" "customer_documents_block" {
bucket = aws_s3_bucket.customer_documents.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
# 2. Enforce default KMS encryption
resource "aws_s3_bucket_server_side_encryption_configuration" "customer_documents_encryption" {
bucket = aws_s3_bucket.customer_documents.id
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = aws_kms_key.s3_key.arn
sse_algorithm = "aws:kms"
}
bucket_key_enabled = true
}
}
# 3. Restrict management access to corporate VPN / bastion CIDR only
resource "aws_security_group" "web_sg" {
name = "web-server-sg"
description = "Security group for web instances"
ingress {
description = "SSH from internal VPN bastion only"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["10.200.0.50/32"] # Pinned internal IP
}
}The hardened Terraform configuration establishes a four-part defense: aws_s3_bucket_public_access_block completely neutralizes public bucket policies and ACLs; customer-managed KMS encryption with automatic key rotation protects stored data at rest; and security group ingress rules restrict administrative SSH access to an internal VPN bastion CIDR, eliminating exposure to public scanners and automated brute-force attempts.