This document contains instructions on configuring the pre-commit framework for this repository, with contain numerous terraform code examples.
Also, we will explore two open-sorce, community-driven static code analysis tool for scanning infrastructure as code (IaC) files for misconfigurations called Checkov and Terrascan.
This repository is a fork from alfonsof/terraform-azure-examples.
$ gh repo clone danielbmeireles/dvm-pre-commit
$ gh repo set-default
This command sets the default remote repository to use when querying the
GitHub API for the locally cloned repository.
gh uses the default repository for things like:
- viewing and creating pull requests
- viewing and creating issues
- viewing and creating releases
- working with GitHub Actions
- adding repository and environment secrets
? Which repository should be the default? danielbmeireles/dvm-pre-commit
✓ Set danielbmeireles/dvm-pre-commit as the default repository for the current directory
$ yq .pre-commit-config.yaml
Can you identify the three main sections of the file? How many repos are configured? And how many hooks?
$ pre-commit install --install-hooks
$ pre-commit autoupdate
Does any repository was updated?
At any time, you can manually run all pre‑commit hooks in a repository. For example, following some code modifications but prior to committing your changes, you can run the hooks to reveal any identified issues beforehand. Just run the following command:
$ pre-commit run
Bear in mind that this checks only for files added with git add
.
$ pre-commit run terraform-fmt
If you want to check all files in the repository, regardless of their state in the Git database, add the ‑‑all‑files
argument:
$ pre-commit run --all-files
This is always a good idea after adding a new hook. You can also combine this with the restriction to an individual hook:
$ pre-commit run terraform-fmt --all-files
$ SKIP=checkov git commit ‑m "Add foo"
$ SKIP=checkov,terrascan git commit ‑m "Add foo"
$ git commit ‑m "Add foo" ‑‑no‑verify
$ checkov -f main.tf
$ checkov -d /user/tf
$ terrascan init
Note: The init command is implicitly executed if the scan command does not find policies while executing.
$ terrascan scan
$ terrascan scan -t aws
Try to execute the same command but using the azure
cloud provider.
$ terrascan scan -i terraform
$ terrascan scan -t azure -r git -u [email protected]:danielbmeireles/dvm-pre-commit.git//code/01-hello-world