SECURITY ENGINEERING · PART 9 · DEVSECOPS AND SUPPLY CHAIN · IAC · 131 OF 229

IaC Security: Scan Terraform With Checkov, Trivy

When infrastructure is code, its misconfigurations are code too, and you can scan for them before they ever reach the cloud. This episode covers infrastructure-as-code security: common misconfigurations (public buckets, open security groups, no encryption), scanning Terraform and manifests with Checkov and Trivy, scanning the plan versus the code, drift, and fixing issues at the source.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 10

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Misconfig is code now
  • scan Terraform, K8s, Dockerfiles
  • scan the plan too
  • fix in modules
  • detect drift

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.

What goes wrong
  • 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.

IaC scanning
  • 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.

Two things to scan
  • 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.

Config drift
  • 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.

The leverage of IaC
  • 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.

In the pipeline
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1What is infrastructure-as-code security scanning?

Analysing infrastructure definitions such as Terraform, Kubernetes manifests, CloudFormation and Dockerfiles against security policies to find misconfigurations before resources are created, for example public buckets, open security groups or missing encryption.

Q2What are the most common cloud misconfigurations?

Publicly accessible storage buckets, security groups open to the internet on sensitive ports, encryption not enabled at rest or in transit, over-broad IAM permissions, missing logging, and public database snapshots. The same handful of patterns recur, making them ideal for automated rules.

Q3Which tools scan infrastructure as code?

Checkov, Trivy, tfsec, Terrascan and KICS are common, shipping with hundreds of built-in policies for major clouds and supporting custom rules. They run in pre-commit hooks and CI and comment on pull requests.

Q4Why scan the Terraform plan and not just the code?

Because some misconfigurations only become visible once variables, modules and data sources are resolved. The plan shows what will actually be created with real values, catching issues, such as an open range supplied by a variable, that look fine in the raw code.

Q5What is configuration drift?

When the running infrastructure no longer matches the code, typically because someone changed a resource manually in the console. Scans of the code can then pass while the live environment is insecure, so drift must be detected and corrected by re-applying from code.

Q6Why fix misconfigurations in modules rather than live resources?

Because a fix in a shared module propagates to every environment that uses it, while fixing a live resource leaves the insecure pattern in the code to reappear on the next apply. Golden modules with secure defaults make the secure configuration the default.

Q7How should IaC scan results be gated?

By scanning in pre-commit and CI, blocking the apply on new high-severity misconfigurations such as public buckets and open admin ports, baselining existing findings, allowing justified reviewed exceptions in code, and enforcing policy at deploy with an engine like OPA.

Q8How does IaC scanning relate to CSPM?

IaC scanning checks the definitions before deployment (shift left), while Cloud Security Posture Management scans the live cloud environment for misconfigurations and drift. They are complementary: one prevents issues at the source, the other catches what reaches production.