Notes
The common mistakes
The misconfigurations that cause real incidents are a short, repeating list. Storage buckets open to the public. Security groups that let the whole internet reach sensitive ports like the database or SSH. Encryption not enabled, at rest or in transit. Over-broad permissions, such as wildcard actions or admin roles given to things that do not need them. No logging. Public database snapshots. Because the same handful of patterns recur everywhere, they are exactly what automated rules catch well.
- storage buckets open to the public
- security groups open to 0.0.0.0/0 on sensitive ports
- no encryption at rest or in transit
- over-broad IAM (wildcard actions, admin roles)
- no logging; public snapshots
- these are a handful of patterns, and they repeat
Scan the code
IaC scanning parses your infrastructure definitions, Terraform, manifests, CloudFormation, Dockerfiles, and checks them against a library of policies: is this bucket public, this security group open, encryption off? The tools, Checkov, Trivy, tfsec, Terrascan and KICS, ship with hundreds of built-in rules and let you add your own. They run in pre-commit and CI and comment on the pull request. This is security shifted all the way left: you catch the public bucket in review, before it exists.
- parse Terraform, K8s YAML, CloudFormation, Dockerfiles
- check against a library of policies (public bucket, open SG, no encryption)
- tools: Checkov, Trivy, tfsec, Terrascan, KICS
- runs in pre-commit and CI, comments on the PR
- fast and fully left-shifted
Code vs plan
There is a subtlety worth knowing. Scanning the raw code is fast and early, but some misconfigurations only become visible once variables, modules and data sources are resolved. So the stronger check is to scan the plan, the output of terraform plan, which shows what will actually be created with all values filled in. A security group whose open range comes from a variable looks fine in the code but dangerous in the plan. Scan the code for fast feedback, the plan for accuracy; do both where you can.
- scan the code: fast, early, catches static misconfig
- scan the plan (terraform plan): sees resolved values, variables, modules, data sources
- the plan shows what will ACTUALLY be created
- some issues only appear once variables are resolved
- do both where you can
Drift
IaC scanning checks the code, but the code is only the truth if nobody changes things by hand. When someone edits a resource directly in the cloud console, the running infrastructure drifts from the code, and your scans pass while reality is insecure. So you also detect drift: terraform plan shows differences, and cloud posture tools, covered in Part ten on cloud security as CSPM, scan the live environment. The fix is to re-apply from code and forbid manual changes, so the code stays authoritative.
- someone changes a resource by hand in the console
- now the cloud no longer matches the code
- your scans pass, but reality is insecure
- detect drift: terraform plan, cloud config tools, CSPM
- fix: re-apply from code; forbid manual changes
Fix at the source
Here is the real leverage of infrastructure as code. When a scanner finds a misconfiguration, you fix it in the module, not each live resource, and every environment that uses that module inherits the fix. So the mature move is shared golden modules with secure defaults baked in, encryption on, private by default, logging enabled, plus custom policies for your org's standards. Fixing the pattern once in a module beats fixing the same bucket in fifty projects, and makes the secure path the default.
- fix the Terraform module, not each live resource
- every environment that uses it inherits the fix
- secure defaults in shared modules: encryption on, private by default
- write custom policies for your org standards
- golden modules beat fixing the same thing 50 times
Gating
In the pipeline, treat IaC findings like any security gate. Scan in pre-commit and on the pull request, commenting inline on the changed resources. Block the apply on new high-severity issues, a newly public bucket, an open admin port, while baselining the existing ones. Allow justified exceptions, suppressed in code with a reviewed reason. And for real enforcement at deploy time, pair scanning with a policy engine like Open Policy Agent, which the next episode covers.
- scan in pre-commit and on the PR; comment inline
- block the apply on new high severity (public bucket, open admin port)
- baseline existing findings
- allow justified, reviewed exceptions in code
- pair with a policy engine (OPA, next episode) for enforcement
Common mistakes
Mistakes. Scanning the code but never checking the live environment for drift, so manual changes go unnoticed. Scanning only the raw code and missing issues that appear once variables resolve in the plan. Fixing a misconfiguration on the live resource instead of in the module, so it returns next apply. Allowing manual console changes, which makes the code untrustworthy. Hundreds of rules with no baseline, so developers ignore the flood. And no custom rules for your organisation's standards.
- scanning code but ignoring live drift
- scanning code only, never the resolved plan
- fixing live resources, not the module
- allowing manual console changes
- hundreds of rules, no baseline, all ignored
- no custom org rules
Say it like this
In the interview: I scan infrastructure definitions, Terraform, manifests, Dockerfiles, with Checkov or Trivy, in pre-commit and CI. I scan the terraform plan as well as the code, because some issues only appear once variables resolve. I block new high-severity misconfigurations like public buckets and open admin ports, and baseline the rest. I fix issues in shared golden modules with secure defaults, detect drift against the live environment and forbid manual changes, and enforce at deploy with Open Policy Agent.
- "Scan Terraform and manifests
- with Checkov or Trivy in pre-commit and CI, and scan
- the plan for resolved values.
- Block new highs, baseline the rest. Fix in shared modules
- with secure defaults, detect
- drift against live, forbid
- manual changes, and enforce at deploy with a policy engine."