pre-commit hooks for Terraform

pre-commit hooks for Terraform

As you work on the large codebase, you may want to run a set of checks before you submit a pull request for code review. This is a great way to ensure that your code is ready for review. Git hooks are a great way to do this. The pre-commit hook is a framework for creating git hooks that can be used to run a set of checks before you commit the code into a repository.

pre-commit hooks automatically scan the codebase and point out the issues with a code such as linting errors, style violations, missing semicolons, etc. This helps you to ensure that the reviewers can focus on the business logic and not on the trivial details. The pre-commit framework supports multiple programming languages and can be used to run a set of checks for multiple programming languages.

Install pre-commit

The pre-commit package manager can be installed using the pip command:

pip install pre-commit

Once done, verify that the pre-commit is installed by running the following command:

pre-commit --version

How to use pre-commit hooks

The pre-commit hooks can be used in any programming language. The only requirement is to have a .pre-commit-config.yaml file in the root of the repository. Inside this file, you can specify the hooks that you want to run.

  • The hooks are defined as a list of dictionaries. Each dictionary contains the following keys: repos is the root key that contains the repository of the hook. rev is the revision of the hook. hooks is the list of hooks that you want to run.

  • Each hook is defined as a dictionary. The hook contains the following keys: id is the id of the hook. name is the name of the hook. entry is the entry point of the hook. language is the programming language of the hook.

- repo:
  rev: v3.2.0
    - id: check-yaml
    - id: end-of-file-fixer
    - id: trailing-whitespace
- repo:
  rev: 19.3b0
    - id: black

If we want to run pre-commit hooks on every commit, we need to install the pre-commit hook using the following command:

pre-commit install

We can also run the pre-commit against all the files in the repository using the following command:

pre-commit run --all-files

How to use pre-commit hooks in Terraform

Let us now see how to use pre-commit hooks in Terraform. The project pre-commit-terraform already has the pre-commit hooks to take care of Terraform files.

Please Check out the code from the GitHub repository TerraformCode and then add the file .pre-commit-config.yaml to the root of the repository with the following content:

  - repo:
    rev: v1.62.3
      - id: terraform_fmt
      - id: terraform_validate

We are using the pre-commit-terraform repository to run the hooks such as terraform fmt and terraform validate. terraform fmt is a tool that formats the Terraform files. terraform validate is a tool that validates the Terraform files. These two hooks are run on every commit.

We can install the pre-commit hooks using the following command:

pre-commit install

Now, for every commit you make locally, it will check the above two hooks for formatting the terraform files and validating the terraform files. In this way, we can ensure that the terraform files are formatted and validated before we commit them. There are various hooks supported by pre-commit-terraform. Please refer to the pre-commit-terraform repository for more details.

Github Link:

Did you find this article valuable?

Support Rahul Lokurte by becoming a sponsor. Any amount is appreciated!