Skip to main content

Free 30-min security demo  — We'll scan your real code and show live findings, no commitment Book Now

Offensive360
Academy Infrastructure-as-Code Security
Intermediate · 20 min

Infrastructure-as-Code Security

Learn how open S3 buckets, unrestricted security groups, and hardcoded passwords in Terraform create cloud breaches.

1 IaC Security Misconfigurations

Infrastructure-as-Code (Terraform, CloudFormation, Pulumi) defines cloud resources in code. Security misconfigurations in IaC are deployed at scale and often persist undetected.

Open S3 bucket (Terraform):

resource "aws_s3_bucket" "data" {
  bucket = "company-sensitive-data"
}

resource "aws_s3_bucket_acl" "data_acl" {
  bucket = aws_s3_bucket.data.id
  acl    = "public-read"  # DANGEROUS: anyone can read all objects!
}

Unrestricted security group:

resource "aws_security_group" "database" {
  ingress {
    from_port   = 5432
    to_port     = 5432
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  # PostgreSQL open to entire internet!
  }
}

Hardcoded password in IaC:

resource "aws_db_instance" "main" {
  password = "my-db-password"  # Committed to git in plain text!
}

2 IaC Security Scanning and Best Practices

Scan IaC files before deployment to catch misconfigurations early, and use proper variable references for secrets.

Secure Terraform patterns:

# Secure S3 bucket
resource "aws_s3_bucket_public_access_block" "data" {
  bucket                  = aws_s3_bucket.data.id
  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}

# Restricted security group
resource "aws_security_group" "database" {
  ingress {
    from_port       = 5432
    to_port         = 5432
    protocol        = "tcp"
    security_groups = [aws_security_group.app.id]  # App tier only!
  }
}

# Secrets from SSM Parameter Store
data "aws_ssm_parameter" "db_password" {
  name = "/prod/db/password"
}

resource "aws_db_instance" "main" {
  password = data.aws_ssm_parameter.db_password.value
}

IaC scanning tools:

  • Checkov: Open-source, supports Terraform, CloudFormation, Kubernetes
  • tfsec: Terraform-specific, fast static analysis
  • Snyk IaC: Commercial with free tier
  • AWS Config / Security Hub: Runtime compliance checking

Knowledge Check

0/3 correct
Q1

Why are IaC security misconfigurations particularly dangerous compared to application code bugs?

Q2

What is the correct way to provide database passwords in Terraform without hardcoding them?

Q3

When should IaC security scanning ideally occur?

Code Exercise

Block Public S3 Bucket Access

The Terraform config creates an S3 bucket with public-read ACL. Add public access block configuration to prevent any public access.

hcl