My AWS Organization is managed with Terraform, using an open source landing zone module. That repo has admin reach over every account in the org, so I wanted checks that run before anything gets committed: secrets, state files, formatting, validation, and misconfigurations. That’s pre-commit, plus Checkov, plus a decision about what to do with Checkov’s findings.

Why check before the commit

Git history is permanent. A secret that lands in a commit is in every clone of that repo, even after you delete the file, so the only real fix is rotating the secret. Terraform state is a second risk: Terraform stores state in a plaintext file that includes any secret values in the configuration [1]. My state lives in S3, but a terraform state pull or a broken backend can leave a local copy, and git add -f skips .gitignore.

The third risk is supply chain. The landing zone module is someone else’s code, and it runs with admin rights over the whole organization.

The hooks

pre-commit runs hooks against the files you’re committing and blocks the commit if one fails. Every hook repo is pinned to a full commit SHA, with the release in a comment:

repos:
  - repo: https://github.com/gitleaks/gitleaks
    rev: 83d9cd684c87d95d656c1458ef04895a7f1cbd8e  # v8.30.1
    hooks:
      - id: gitleaks

  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: 3e8a8703264a2f4a69428a0aa4dcb512790b2c8c  # v6.0.0
    hooks:
      - id: detect-private-key
      - id: detect-aws-credentials
        args: [--allow-missing-credentials]
      - id: check-added-large-files
        args: [--maxkb=1000]
      - id: check-json
      - id: check-yaml
      - id: check-merge-conflict

  - repo: local
    hooks:
      - id: terraform-fmt
        name: terraform fmt
        entry: terraform fmt -recursive
        language: system
        files: '\.(tf|tfvars)$'
        pass_filenames: false
      - id: terraform-validate
        name: terraform validate
        entry: terraform validate -no-color
        language: system
        files: '\.tf$'
        pass_filenames: false
      - id: block-env-files
        name: block .env files
        entry: ".env files must never be committed."
        language: fail
        files: '(^|/)\.env(\..+)?$'
        exclude: '\.env\.example$'
      - id: block-terraform-state
        name: block Terraform state and plan files
        entry: "State and plan files can hold secrets in plaintext."
        language: fail
        files: '(\.tfstate(\.backup)?$|(^|/)tfplan$|\.tfplan$)'

  - repo: https://github.com/bridgecrewio/checkov
    rev: a8ee1c49faec5e324ddf27afb1a7d37a33d109e1  # 3.4.11
    hooks:
      - id: checkov
        args: [--quiet, --compact]

A few choices in there are worth explaining:

  • Local Terraform hooks. repo: local hooks call the terraform binary already on the machine [2]. The popular Terraform hook collection uses bash scripts, which can break on Windows. Newer pre-commit releases call language: system by a new name, unsupported, and keep the old name as an alias for now [2].
  • language: fail. This is pre-commit’s way to forbid files by name [2]. It blocks .env files and state files no matter what’s inside them. .gitignore is the first layer, and this is the second.
  • Gitleaks without Go installed. pre-commit bootstraps Go if it isn’t present [2]. The first run is slow, then it’s cached.

Run everything once against the whole repo, then let it run on each commit:

pre-commit install
pre-commit run --all-files

Pin modules by commit, not by tag

Checkov’s first finding was CKV_TF_1: the module source used ?ref=0.3.0, a tag. A tag is a pointer, and if a bad actor gets access to the module’s repo, they can move it to different code [3]. My next terraform init would then run their code with admin rights over the org. Terraform accepts a full commit SHA in ref [4], and a SHA can’t point at anything else.

The release I used is an annotated tag, so git ls-remote shows two hashes. The one ending in ^{} is the commit, and that’s the one Terraform needs:

git ls-remote --tags https://github.com/<module-owner>/<module-repo> <tag>
source = "github.com/<module-owner>/<module-repo>?ref=<commit-sha>"  # <tag>

Same commit as before, so nothing in AWS changes.

A passing hook isn’t the same as a scan

The Checkov hook passed, but its output had a line saying it couldn’t load the module. My repo only calls the module. The buckets, keys, SCPs and GuardDuty all live inside it, so the hook was checking almost none of the real infrastructure. pre-commit also hides a hook’s output when it passes, so that warning was easy to miss.

Checkov can download external modules and scan them, into a folder called .external_modules [5]. On Windows, the checkov launcher in my virtual environment flashed a window and wrote nothing, so I ran it through the venv’s Python instead:

& $HOME\.venvs\checkov\Scripts\python.exe -m checkov.main -d . --download-external-modules true --var-file prod.tfvars --compact

I added .external_modules/ to .gitignore so the downloaded module never gets committed.

Triage the findings before blocking anything

The module scan returned 205 passed, 51 failed and 3 skipped. I sorted every failure into a triage file in the repo instead of making them block commits:

Group Count Why
Not deployed 24 Features my variables turn off, like a data trail, VPC flow logs and Macie. Checkov reads code and doesn’t always work out that a resource count ends up 0
Accepted for a lab 24 Cross-region replication, S3 access logging, customer-managed KMS keys. Real best practices that cost money for little lab value
Real gaps, not urgent 3 CloudTrail not encrypted with a KMS key (a CIS item), a missing public access setting on one bucket, and a GuardDuty check that’s likely a false positive

The 3 skipped checks were the module author’s own #checkov:skip comments, each with a reason. That’s how a skip should look: in the code, explained, and reviewable.

51 failures turned out to be 27 real items and nothing urgent. A finding count is an input, not a risk score. And making a new scanner block commits before anyone has read its findings is how teams end up running --no-verify or deleting the tool.

One more thing the scan taught me

terraform init failed with “No valid credential sources found” after I opened a new terminal and lost my AWS profile. With no credentials set, the AWS SDKs check a series of sources in order, and EC2 instance metadata is one of them [6]. That’s the address at 169.254.169.254, a classic server-side request forgery (SSRF) target, and the reason IMDSv2 requires a session token from a PUT request first [7]. The fix on my side was just signing in again:

aws sso login --profile <profile>
$env:AWS_PROFILE = "<profile>"
aws sts get-caller-identity

References

  1. Terraform: Manage sensitive data
  2. pre-commit
  3. GitHub Docs: Secure use reference
  4. Terraform: Module sources
  5. Checkov: Terraform scan examples
  6. AWS SDKs and Tools: standardized credential providers
  7. AWS Security Blog: Defense in depth for the EC2 Instance Metadata Service

Comment section

Sorry babes, you have to sign into Github to comment. Feel free to let me know what's on your mind!