terraform-config-inspect

repository·master·Indexed 19 days ago

https://github.com/hashicorp/terraform-config-inspect

A Go library and CLI tool for extracting high-level metadata from Terraform modules and stacks. It supports outputting variables, outputs, required providers, and managed resources in Markdown or JSON formats. The tool includes specialized modes for stack inspection via the --stack flag and post-initialization configuration parsing via the --after-init flag.

Tokens
1.1K
Snippets
7
Records
7
Agent score
16%

What's inside terraform-config-inspect

  1. Configure the data directory for post-init inspection

    master

    When using the --after-init flag, the tool looks for the Terraform data directory to inspect the configuration.

    1. It first checks the TF_DATA_DIR environment variable.
    2. If TF_DATA_DIR is not set, it defaults to the .terraform directory within the target directory.

    You can control this behavior by setting the TF_DATA_DIR environment variable before running the command.

    export TF_DATA_DIR=/path/to/custom/terraform/data
    terraform-config-inspect --after-init --json .
  2. Install the terraform-config-inspect CLI

    master

    You can install the terraform-config-inspect command-line tool using go install. This tool allows you to view Terraform module information in Markdown or JSON formats, and supports parsing Terraform stacks.

    $ go install github.com/hashicorp/terraform-config-inspect@latest
  3. Use terraform-config-inspect as a Go library

    master

    The primary use case for this repository is as a Go library. You can use the tfconfig.LoadModule function to extract high-level metadata from a Terraform module directory. The library is designed to be broadly compatible with many Terraform versions by focusing on a subset of the language.

    import "github.com/hashicorp/terraform-config-inspect/tfconfig"
    
    // ...
    
    module, diags := tfconfig.LoadModule(dir)
    
    // ...
  4. terraform-config-inspect CLI flags

    master

    The following flags are available for the terraform-config-inspect command:

    FlagTypeDescription
    --jsonbooleanProduce JSON-formatted output instead of Markdown.
    --stackbooleanParse a Terraform stack instead of a single module.
    --after-initbooleanParse a Terraform configuration after init. Requires --json flag.
    --json
    --stack
    --after-init
  5. Use the terraform-config-inspect CLI

    master

    The terraform-config-inspect CLI tool allows you to inspect Terraform modules, stacks, or post-initialization configurations. It can output the inspection results in either human-readable Markdown or machine-readable JSON.

    By default, the tool inspects a Terraform module in the current directory. You can provide a specific directory as a positional argument.

    Usage Modes:

    • Module Inspection (Default): Inspects a single Terraform module.
    • Stack Inspection: Use --stack to parse a Terraform stack.
    • Post-Init Inspection: Use --after-init to parse a configuration after terraform init has been run. Note that this mode requires the --json flag.
    # Inspect a module in the current directory (Markdown output)
    terraform-config-inspect .
    
    # Inspect a module and output JSON
    terraform-config-inspect --json .
    
    # Inspect a Terraform stack
    terraform-config-inspect --stack .
    
    # Inspect a Terraform configuration after init (requires --json)
    terraform-config-inspect --after-init --json .
  6. Inspect a Terraform module via CLI

    master

    Use the terraform-config-inspect CLI to inspect a single Terraform module. By default, it outputs information in a Markdown-like format. You can use the --json flag to get a machine-readable JSON representation of the module's variables, outputs, required providers, and managed resources.

    # View module info in Markdown
    $ terraform-config-inspect path/to/module
    
    # View module info in JSON
    $ terraform-config-inspect --json path/to/module
  7. Inspect a Terraform stack via CLI

    master

    The CLI supports parsing Terraform stacks using the --stack flag. This allows you to view information about components, variables, outputs, and required providers within a stack. You can also request JSON output for stacks using the --json flag.

    # View stack info in Markdown
    $ terraform-config-inspect --stack path/to/stack
    
    # View stack info in JSON
    $ terraform-config-inspect --stack --json path/to/stack