IaC security
Infrastructure as code (Terraform, CloudFormation, Ansible, Bicep) defines the cloud and systems in versioned files. That lets you scan infrastructure security before deploying it: a public bucket or an open security group is caught in the PR, not after the breach. It’s shift-left applied to infrastructure.
What it solves
Section titled “What it solves”Most cloud incidents aren’t sophisticated exploits but misconfigurations: a public S3 bucket, a database reachable from the internet, an IAM role with *:*. When infrastructure is defined in code, those errors can be reviewed and scanned just like application code — repeatably and before touching production.
- Consistency: the same scanner evaluates every environment with the same rules.
- Prevention: the flaw is fixed in the PR, when it costs minutes, not in production.
- Traceability: every infrastructure change has an author, a review, and a history.
How IaC scanning works
Section titled “How IaC scanning works”The scanner parses the declarative files, compares them against a policy catalog, and flags resources that break policy. It runs nothing: it analyzes the declared intent.
flowchart LR
DEV["IaC change (PR)"] --> SCAN["IaC scanner (policies)"]
SCAN -->|violates| FAIL["Fail the PR + finding"]
SCAN -->|passes| PLAN["terraform plan"]
PLAN --> REV{"Human review"}
REV -->|approved| APPLY["apply / deploy"]
APPLY --> DRIFT["Drift detection"]
DRIFT -. real config ≠ code .-> ALERT["Alert"]
Example of an insecure Terraform resource (public bucket) and its fixed version:
# BAD: publicly accessible bucketresource "aws_s3_bucket_public_access_block" "data" { bucket = aws_s3_bucket.data.id block_public_acls = false block_public_policy = false ignore_public_acls = false restrict_public_buckets = false}
# GOOD: full public-access blockresource "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}Scanning in the pipeline with industry tools:
# Checkovcheckov -d . --framework terraform --compact
# Trivy over config (IaC misconfig)trivy config --severity HIGH,CRITICAL .
# tfsectfsec . --minimum-severity HIGHIn CI, as a pre-merge gate:
name: iac-scanon: [pull_request]jobs: checkov: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: bridgecrewio/checkov-action@master with: directory: . framework: terraformRecurring flaw classes
Section titled “Recurring flaw classes”What these scanners find over and over:
- Public storage: buckets (S3), blobs, and disks exposed by accident.
- Open network: security groups with
0.0.0.0/0to sensitive ports (22, 3389, 3306). - Excessive IAM: policies with wildcard
Action: "*"orResource: "*". - Encryption disabled: disks, databases, or buckets without encryption at rest.
- Logging disabled: resources without audit trails (CloudTrail, flow logs).
- Secrets in code: credentials in Terraform variables or in the state (see secrets management).
State and drift
Section titled “State and drift”Two IaC-specific risks that policy scanning doesn’t cover:
- The state file (Terraform’s) stores the real state, including secrets in cleartext. It must live in a remote, encrypted backend with access control, never in the repo.
- Drift is the divergence between what the code says and what’s actually deployed (someone touched the console by hand). Drift breaks the “code is the truth” premise: you must detect it (
terraform plan, drift-detection tools) and reconcile it.
Red Team
Section titled “Red Team”- Hunt the forgotten resource: the attacker enumerates the target’s cloud looking for exactly what the scanners would flag — public buckets, open ports, over-permissioned roles.
- Read the IaC if reachable: a leaked Terraform repo is a map of the infrastructure; modules reveal resource names, networks, and sometimes secrets in default variables.
- Exposed state: a
terraform.tfstatefile in a poorly protected bucket hands over secrets and the full topology. - Exploit the chain: an oversized IAM role declared in IaC is an escalation path after compromising a resource.
Blue Team
Section titled “Blue Team”- Scan on the PR with policies; block the critical (public/open/wildcard IAM).
- Encrypted, access-restricted state backend; never cleartext secrets in the repo.
- Scheduled drift detection and reconciliation.
- Reusable, secure-by-default modules: instead of fixing each bucket, provide a module that’s already secure.
- Policy as code: define your org’s rules (OPA/Rego, custom Checkov) and version them.
- Least privilege in IAM declared from the start, not as a patch.
Checklist
Section titled “Checklist”- IaC scanning on every PR with the org’s policies.
- Gate blocking public storage, open network, and wildcard IAM.
- Remote, encrypted state backend with access control.
- Zero secrets in variables, code, or state.
- Encryption at rest and logging on by default.
- Scheduled drift detection.
- Secure-by-default base modules, reused.
- Policy as code versioned alongside the infrastructure.