Infrastructure as Code Security

The $50,000 Mistake: When a Public S3 Bucket Cost More Than Money

3:42 AM. AWS GuardDuty alert: "Unusual API activity detected." Then another. And another. By the time I woke up at 7 AM, I had 247 security alerts and a $50,000 surprise AWS bill.

A Terraform misconfiguration had left an S3 bucket publicly accessible. In it? Database backups. Customer data. API keys. Everything. For six hours, attackers had been exfiltrating data and spinning up EC2 instances for cryptominingβ€”all billed to us.

The root cause? One line in a Terraform file:

acl = "public-read"  # ← This destroyed us

Someone copy-pasted example code from a blog post. It passed code review. It deployed to production. Nobody caught it until AWS sent the bill.

That incident cost us$50,000 in AWS charges, weeks of incident response, mandatory breach notifications to 12,000 customers, and regulatory fines. But the worst part? It was 100% preventable. Infrastructure as Code security scanning would have caught it before it ever reached Git.

This article covers the IaC security strategy I built from the ashes of that disaster.

What You'll Learn

  • Infrastructure as Code security fundamentals

  • Terraform security scanning with Checkov, tfsec, and Terrascan

  • CloudFormation and ARM template security

  • Policy as Code with Open Policy Agent

  • IaC security in CI/CD pipelines

  • Cloud security posture management

  • Drift detection and remediation

The IaC Security Challenge

Infrastructure as Code has revolutionized how we manage cloud resources. But it's also codeβ€”and code has vulnerabilities.

spinner

IaC security concerns:

  • Misconfigurations (public buckets, open security groups)

  • Insufficient encryption

  • Overly permissive IAM policies

  • Missing logging and monitoring

  • Non-compliant configurations

  • Hardcoded secrets

  • Resource exposure

Terraform Security with Checkov

Checkov is my preferred IaC scannerβ€”comprehensive, actively maintained, and supports multiple IaC frameworks.

Basic Checkov Scanning

GitLab CI Integration

Real Vulnerabilities Caught by Checkov

1. Public S3 Bucket (The One That Cost Us)

Fixed version:

2. Overly Permissive Security Group

Fixed version:

3. Unencrypted RDS Instance

Fixed version:

tfsec for Fast Terraform Scanning

tfsec is incredibly fast and great for pre-commit hooks.

Pre-commit Hook

Terrascan for Policy-Based Scanning

Terrascan supports OPA-based custom policies.

Custom Terrascan Policies

CloudFormation Security

For AWS CloudFormation templates:

CloudFormation Guard

Policy as Code with OPA

Open Policy Agent provides flexible policy enforcement.

OPA Integration

Drift Detection

Detect when infrastructure diverges from IaC definitions.

AWS Config for Drift Detection

Terraform Drift Detection

Best Practices

1. Scan Before Commit

2. Multiple Scanning Tools

Different tools catch different issues:

3. Policy as Code

4. Continuous Scanning

5. Automated Remediation

Key Takeaways

βœ… Scan IaC before deployment - Catch misconfigurations early βœ… Use multiple tools - Checkov, tfsec, Terrascan complement each other βœ… Enforce policies - OPA for custom security requirements βœ… Detect drift - Monitor for manual changes βœ… Automate remediation - Fix common issues automatically βœ… Continuous monitoring - Security doesn't end at deployment

What's Next

Secure infrastructure needs secure secrets. In the next article, we'll cover secrets managementβ€”HashiCorp Vault integration, secret rotation, and building a zero-trust secrets architecture.


Next Article: Secrets Management and Credential Security β†’


Part of the DevSecOps 101 Series

Last updated