Skip to content

Instantly share code, notes, and snippets.

@atheiman
Last active February 23, 2026 21:41
Show Gist options
  • Select an option

  • Save atheiman/2b56eb7b6942d7403bede7964d46680e to your computer and use it in GitHub Desktop.

Select an option

Save atheiman/2b56eb7b6942d7403bede7964d46680e to your computer and use it in GitHub Desktop.
Configure a terraform repo to update README.md with `terraform-docs` in a `pre-commit` Git hook
  1. Save latest terraform-docs pre-built binary into $PATH from terraform-docs GitHub Releases.
    terraform-docs --version
    # terraform-docs version v0.21.0 ...
  2. Configure .terraform-docs.yml to update README.md appropriately. Usually this file would be saved in the root of the repo. If you have a modules directory containing many modules with READMEs to update, you can add the recursive argument. See .terraform-docs.yml options docs.
    # https://terraform-docs.io/user-guide/configuration/
    
    formatter: markdown table
    
    output:
      file: README.md
    
    sections:
      show:
        - requirements
        - resources
        - inputs
        - outputs
    
    sort:
      enabled: true
      by: required
    
    # Recursive config for subdirectories with modules
    # https://terraform-docs.io/user-guide/configuration/recursive/
    #recursive:
    #  enabled: true
    #  path: modules
    #  include-main: true
  3. Ensure terraform-docs . or a similar command from the root of the repository updates README.md file(s) as desired.
  4. Configure git pre-commit hook in file .git/hooks/pre-commit. Git hooks run from the root of the repository, so specify relative paths from there. See Git hooks docs.
    #!/bin/sh
    # https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
    
    echo "[Git hook: pre-commit] Updating README.md file(s) with terraform-docs"
    terraform-docs .
    git add README.md
    # Option for recursive module subdirectories
    #git add README.md '**/README.md'
  5. Update a variable, output, etc. and see the README.md update on commit:
    $ git commit -am 'Update variables documentation'
    [Git hook: pre-commit] Updating README.md file(s) with terraform-docs
    README.md updated successfully
    [main 8095528] Update variables documentation
     2 files changed, 15 insertions(+), 17 deletions(-)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment