Better than running linter tests: pre-commit

I wrote a blog post about how I write Terraform templates which includes styling as well but I was not happy how I code templates. Having OCD does not mean I don’t do any mistakes and I commit code with indentation mistakes and I do mistakes on syntax as well.

Primarily, I do find very distractive and waste of time running linters after the code committed and pushed to upstream repository. To me, right approach is, not be able to commit in the first place and you never need to worry about styling issues then.

Most importantly, my cloud provisioning code should not depend on any resource from the infrastructure such as a Jenkins instance on the cloud.

I also mentioned in earlier posts I do love automate things instead doing manually and I decided to having checks and a validation steps by git hooks.

Regarding this, I wanted to create a git pre-commit hooks that check syntax and validate templates before I commit, but, I can’t be copying over those scripts to every new project and/or especially if I do updates on a script, I don’t want to upgrade all the projects with the newer version.

So that, I wanted to make them portable, source controlled, and, should be able to consume with a desired state such as git tags.

While I was googling for additional ideas, I ended up on pre-commit website. I was fascinated with what Yelp engineers had done!

What pre-commit is a command line tool which manages source controlled community driven git hooks in your repository. After you installed the command line tool, you can setup your git hooks with a YAML configuration file in your repository.

You can install pre-commit by;

After the installation, you will need the config regarding your needs. For my needs for Terraforming better, I started to use the configuration below.

- repo: git://
sha: v0.9.1
- id: trailing-whitespace
- repo: git://
sha: v1.0.1
- id: forbid-tabs
- repo: git://
sha: v0.0.2
- id: terraform_fmt
- id: terraform_validate

After creating .pre-commit-config.yaml in my repo as above, I only need to run pre-commit install initially and I’m done with all the styling checks and so.

If someone in the team upgrades sha ‘s value in the config file, your next commit will trigger an update on hooks as well.

So, not only for Terraform but you can use pre-commit with pretty much everything regarding your needs. Here is the ready-to-use list of hooks.

If you don’t have what you need, you can contribute with putting your hooks on git and raising a PR against to pre-commit’s website repo. They also have a contribution guideline as well.

Show your support

Clapping shows how much you appreciated Levent Yalcin’s story.