# 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:

```shell
pip install pre-commit
```

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

```shell
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.

```yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
  rev: v3.2.0
  hooks:
    - id: check-yaml
    - id: end-of-file-fixer
    - id: trailing-whitespace
- repo: https://github.com/psf/black
  rev: 19.3b0
  hooks:
    - 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:
```shell
pre-commit install
```

We can also run the pre-commit against all the files in the repository using the following command:
```shell
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](https://github.com/rahulmlokurte/aws-usage/tree/main/terraform/terraform-modules) and then add the file `.pre-commit-config.yaml` to the root of the repository with the following content:

```yaml
repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.62.3
    hooks:
      - 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:
```shell
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](https://github.com/antonbabenko/pre-commit-terraform) for more details.

Github Link: [https://github.com/rahulmlokurte/aws-usage/tree/main/terraform/terraform-modules](https://github.com/rahulmlokurte/aws-usage/tree/main/terraform/terraform-modules)

