Skip to content

Latest commit

 

History

History
170 lines (128 loc) · 4.14 KB

usage.md

File metadata and controls

170 lines (128 loc) · 4.14 KB

Table of Contents generated with DocToc

Usage

Summarize

terraform plan -out plan.tfplan
terraform show -json plan.tfplan | tftools summarize --show-tags --compact

Or if you have the file already in json:

terraform plan -out plan.tfplan
terraform show -json plan.tfplan > demo.json
cat plan.json | tftools summarize
# or
tftools summarize --compact --show-tags <demo.json

Function for zsh

Edit your ~/.zshrc

function tfsum() {
  if [ -z "$1" ];
  then
    echo "You should type 'tfsum terraform|terragrunt'"
  else
    echo -en "Starting tf summary... Please wait"
    # If you want to print full plan output: $1 plan -out plan.tfplan
    $1 plan -out plan.tfplan 1> /dev/null
    $1 show -json plan.tfplan | tftools summarize --show-tags
    # Delete plan out file to avoid git tracking (although is included in .gitignore)
    if [ -f "plan.tfplan" ]; then rm plan.tfplan; fi
  fi
}

Function for fish

Edit your ~/.config/fish/config.fish or create a new file inside ~/.config/fish/functions/tfsum.fish

function tfsum
    if test -z $argv[1]
        echo "You should type 'tfsum terraform|terragrunt'"
    else
        echo -en "Starting tf summary... Please wait"
        # If you want to print full plan output: $argv[1] plan -out plan.tfplan
        $argv[1] plan -out plan.tfplan 1> /dev/null
        $argv[1] show -json plan.tfplan | tftools summarize --show-tags
        # Delete plan out file to avoid git tracking (although is included in .gitignore)
        if test -f "plan.tfplan"; rm plan.tfplan; end
    end
end

Function for bash

Edit your ~/.bashrc

tfsum() {
  (
    # Enable pipefail within the subshell
    set -o pipefail

    # Create plan and pass through any arguments
    # Make a random tfplan filename in /tmp
    TMP_FILE=$(mktemp /tmp/tfplan.XXXXXX)

    # Execute terraform plan and other commands
    terraform plan -lock=false -compact-warnings -out=${TMP_FILE} "$@" |
      # Remove the line mentioning where the plan was saved
      awk '!/Saved the plan to/{print;next} /Saved the plan to/{exit}' &&
        terraform show -json ${TMP_FILE} |
          tftools summarize --show-tags --show-unchanged --compact &&
            rm ${TMP_FILE}
  )
}

Warning

Adapt the rest of zsh, fish or bash functions according to your needs.

Load new functions

source ~/.zshrc
source ~/.bashrc
source ~/.config/fish/config.fish

Using tfsum as a custom binary

Copy tfsum to /usr/local/bin/tfsum

sudo cp scripts/tfsum.sh /usr/local/bin/tfsum

/usr/local/bin or other directory included in your path

Example

cd my-terraform-project/
tfsum terraform

Then, you will see the summarized output with the corresponding targets.

The example:

Note

The following example is using the full output command

tftools summarize --show-tags --show-unchanged --compact <demo.json

example

Terragrunt is also supported

$ tfsum terragrunt

JSON output support

JSON output with arns

tftools summarize --json --pretty-json <demo.json

JSON output only metrics

tftools summarize --json --metrics <demo.json

JSON output pretty

tftools summarize --json --metrics --pretty-json <demo.json

example-json-outputs