pre-commit Hooks

Since v0.12.0

With pre-commit, you can ensure your Terraform module documentation is kept up-to-date each time you make a commit.

  1. simply create or update a .pre-commit-config.yaml in the root of your Git repo with at least the following content:

      - repo:
        rev: "<VERSION, TAG, OR SHA TO USE>"             # e.g. "v0.11.2"
          - id: terraform-docs-go
            args: ["ARGS", "TO PASS", "INCLUDING PATH"]  # e.g. ["--output-file", "", "./mymodule/path"]
  2. install pre-commit and run pre-commit to activate the hooks.

  3. make a Terraform change, git add and git commit. pre-commit will regenerate your Terraform docs, after which you can rerun git add and git commit to commit the code and doc changes together.

You can also regenerate the docs manually by running pre-commit -a terraform-docs.

pre-commit via Docker

The pre-commit hook can also be run via Docker, for those who don’t have Go installed. Just use id: terraform-docs-docker in the previous example.

This will build the Docker image from the repo, which can be quite slow. To download the pre-built image instead, change your .pre-commit-config.yaml to:

  - repo: local
      - id: terraform-docs
        name: terraform-docs
        language: docker_image
        entry:  # or, change latest to pin to a specific version
        args: ["ARGS", "TO PASS", "INCLUDING PATH"]          # e.g. ["--output-file", "", "./mymodule/path"]
        pass_filenames: false

Git Hook

A simple git hook (.git/hooks/pre-commit) added to your local terraform repository can keep your Terraform module documentation up to date whenever you make a commit. See also git hooks documentation.


# Keep module docs up to date
for d in modules/*; do
  if terraform-docs md "$d" > "$d/"; then
    git add "./$d/"

Edit on GitHub