0
0
TerraformHow-ToBeginner · 3 min read

How to Use Pre-commit Hooks with Terraform for Code Quality

To use pre-commit hooks with Terraform, install the pre-commit tool and configure a .pre-commit-config.yaml file specifying Terraform hooks like terraform_fmt and terraform_validate. This setup automatically formats and checks your Terraform code before each commit, ensuring clean and error-free infrastructure code.
📐

Syntax

The .pre-commit-config.yaml file defines which hooks run before commits. Each hook has a repository URL, a hook id, and optional files to target. For Terraform, common hooks include terraform_fmt to format code and terraform_validate to check syntax.

yaml
repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.73.0
    hooks:
      - id: terraform_fmt
      - id: terraform_validate
💻

Example

This example shows how to set up pre-commit with Terraform hooks. It formats Terraform files and validates them before each commit, preventing bad code from entering your repository.

bash
# 1. Install pre-commit tool (if not installed):
#    pip install pre-commit

# 2. Create .pre-commit-config.yaml with Terraform hooks:
repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.73.0
    hooks:
      - id: terraform_fmt
      - id: terraform_validate

# 3. Install the git hook scripts:
#    pre-commit install

# 4. Now, when you run 'git commit', pre-commit will:
#    - Format Terraform files with 'terraform fmt'
#    - Validate Terraform files with 'terraform validate'

# 5. To manually run hooks on all files:
#    pre-commit run --all-files
Output
Terraform files formatted successfully. Terraform validation passed.
⚠️

Common Pitfalls

  • Not installing pre-commit: You must install the pre-commit tool separately before using hooks.
  • Wrong hook versions: Always use a stable rev version in .pre-commit-config.yaml to avoid unexpected errors.
  • Missing hook installation: Run pre-commit install to activate hooks in your git repository.
  • Ignoring hook failures: If hooks fail, fix the Terraform code instead of bypassing the hook to keep code quality.
bash
# Wrong way: skipping hook installation
# Just adding .pre-commit-config.yaml but not running:
# pre-commit install

# Right way:
pre-commit install
📊

Quick Reference

CommandDescription
pip install pre-commitInstall the pre-commit tool
pre-commit installInstall git hooks in your repo
pre-commit runRun hooks on changed files
pre-commit run --all-filesRun hooks on all files
terraform fmtFormat Terraform code
terraform validateCheck Terraform syntax

Key Takeaways

Install the pre-commit tool and configure .pre-commit-config.yaml with Terraform hooks.
Run pre-commit install to activate hooks before git commits.
Use terraform_fmt and terraform_validate hooks to keep code clean and error-free.
Always fix hook errors instead of bypassing them to maintain code quality.
Run pre-commit run --all-files to check all Terraform files anytime.