You already have fmt and validate catching basic errors. But those commands check that the code is correct, not that it is secure. Perfectly valid code can create dangerous infrastructure: an S3 bucket open to the world, an unencrypted database, a Security Group with SSH exposed. To detect this, there is static security analysis, with tools like Checkov and tfsec.

The problem: valid but insecure code

validate (subchapter 21.1) would tell you this code is "fine":

resource "aws_s3_bucket" "datos" {
  bucket = "datos-confidenciales"
}

resource "aws_s3_bucket_public_access_block" "datos" {
  bucket                  = aws_s3_bucket.datos.id
  block_public_acls       = false   # ⚠️ allows public access!
  block_public_policy     = false   # ⚠️ dangerous!
}

Syntactically, it is correct. But you are creating a bucket with confidential data open to the internet (remember the danger from subchapter 5.4). validate does not detect this, because it is not a code error: it is a security error. You need something that knows best practices and warns you.

What is static security analysis

Static analysis examines your code without executing it (without creating anything in AWS) and compares it to a database of security rules and best practices. If it finds a dangerous configuration, it warns you by pointing out exactly what is wrong and why.

Your Terraform code
       │
       ▼
  Analyzer (Checkov / tfsec)  ──compares with hundreds of security rules──►
       │
       ├─ ✓ everything secure
       └─ ✗ "The S3 bucket allows public access (line 5)"
          "The database does not have encryption enabled (line 20)"

Analogy: it's like a safety inspector reviewing the blueprints of a building before it is built. They don't wait for the building to be finished to discover that a fire exit is missing: they detect it in the blueprint and force you to fix it first. Static analysis reviews the "blueprints" (your code) and catches security problems before deployment.

The tools: Checkov and tfsec

There are several tools for this; the two best known in the Terraform world are:

Checkov

Checkov (from the company Prisma/Bridgecrew) is a very popular and comprehensive tool. It comes with hundreds of predefined rules that check security best practices and compliance in AWS (and other clouds). It detects things like public buckets, unencrypted resources, dangerous open ports, missing logs, etc.

tfsec

tfsec is another widely used tool, specifically focused on Terraform and security. It is fast and easy to integrate. (It is worth knowing that tfsec has been integrated with Trivy, another tool in the same field; the ecosystem evolves, but the idea is the same.)

Both do a similar job: they analyze your code and list the security problems found. Many teams use one or the other (or both).

checkov -d .       # analyzes the current directory
tfsec .            # analyzes the current directory
   → both list the detected security issues

What kind of problems do they detect

These tools catch exactly the security errors we have mentioned throughout the book:

Problem detected We saw it in...
S3 bucket with public access Subchapter 5.4
SSH open to 0.0.0.0/0 Subchapter 4.2 / 12.3
Unencrypted database Chapter 8
Resources without required tags Best practices
Missing logs / auditing Chapter 24
IAM permissions too broad Chapter 7 (least privilege)

It's like having a security expert reviewing every change, who never gets tired or distracted, consistently applying hundreds of rules that a person could not remember all at once.

Integration in CI: automatic security

Just like fmt and validate (subchapter 21.1), these tools are run in CI, automatically, on every Pull Request:

Pull Request opened
   ├─ terraform fmt -check
   ├─ terraform validate
   ├─ checkov / tfsec        ← security analysis
   │     → if it finds a serious problem → BLOCKS the change ✗
   └─ terraform plan

This way, no insecure change can reach production without someone consciously approving it. If a developer, by mistake, writes a public bucket, the CI detects it and blocks the PR. This is "shift left": detecting security problems as early as possible (in the code), not when they are already in production and an attacker finds them before you do.

Real world example: a developer, in a hurry, configures an unencrypted database for a test and, without realizing it, that code ends up in a PR to production. The CI runs Checkov, which detects "RDS database without encryption at rest" and blocks the PR with a clear message. The developer adds encryption, resubmits, and now it passes. The security flaw never reached production, and all automatically.

A note on false positives

Sometimes these tools flag something that, in your specific case, is intentional and acceptable (for example, a bucket that must be public because it serves a static website, subchapter 5.5). For those cases, you can exclude specific rules in a justified way (with annotations in the code or in the tool's configuration). But do it consciously and with documentation, not just to silence annoying warnings without thinking: every exception must have a clear reason.

What you should remember

  • fmt and validate check that the code is correct, but not that it is secure: valid code can create dangerous infrastructure (public buckets, unencrypted DBs, open SSH).
  • Static security analysis examines your code without executing it and compares it to hundreds of best practice rules, warning you of dangerous configurations. Like an inspector reviewing blueprints before building.
  • The main tools are Checkov (very comprehensive, multi-cloud) and tfsec (focused on Terraform, now linked to Trivy). They do a similar job.
  • They integrate into CI to automatically block insecure changes in every PR: this is the "shift left" principle (catching problems as early as possible).
  • Manage false positives by excluding specific rules in a justified and documented way, not by silencing warnings lightly.

In the next subchapter, we will move on to more comprehensive tests: integration tests with Terratest, which create real infrastructure and verify that it works.

Cloud, AWS & Terraform — From Zero to Expert

Chapter 1 · What is cloud computing

Chapter 2 · The cloud market and major providers

Chapter 3 · Regions, availability zones and edge

Chapter 4 · Compute: EC2

Chapter 5 · Storage: S3

Chapter 6 · Networking: VPC

Chapter 7 · Identity and access: IAM

Chapter 8 · Managed databases

Chapter 9 · Why Infrastructure as Code

Chapter 10 · HCL: the Terraform language

Chapter 11 · Providers and state

Chapter 12 · Your first real infrastructure in Terraform

Chapter 13 · Load balancing and auto scaling

Chapter 14 · Serverless with Lambda

Chapter 15 · Messaging and events

Chapter 16 · Content delivery and DNS

Chapter 17 · Containers on AWS

Chapter 18 · Modules: reuse and composition

Chapter 19 · Workspaces and environment management

Chapter 20 · Remote backends and locking

Chapter 21 · Infrastructure testing

Chapter 22 · Terraform in CI/CD

Chapter 23 · Defense in depth

Chapter 24 · Observability: logs, metrics and traces

Chapter 25 · Cost optimization

Chapter 26 · High availability and disaster recovery

Chapter 27 · AWS Well-Architected Framework

Chapter 28 · Serverless architectures at scale

Chapter 29 · Data platforms on AWS

Chapter 30 · Multi-account and landing zones

Chapter 31 · Platform Engineering and Internal Developer Platform

Chapter 32 · Relevant AWS certifications

Chapter 33 · Projects to consolidate what you've learned

Chapter 34 · Resources and community

© Copyright 2024. All rights reserved