dineshba / tf-summarize

A command-line utility to print the summary of the terraform plan
MIT License
534 stars 38 forks source link

Terragrunt support #65

Open Alexey-Tsarev opened 10 months ago

Alexey-Tsarev commented 10 months ago

Hi! You have an interesting util, but suddenly it does not work with Terragrunt.

I tried, but I got the following error:

terragrunt run-all plan --terragrunt-non-interactive --terragrunt-working-dir ./vpc | tf-summarize
INFO[0000] The stack at ./vpc will be processed in the following order for command plan:
Group 1
- Module .../vpc

error when parsing input: invalid character '\x1b' looking for beginning of value
yyarmoshyk commented 9 months ago

The following approach can be used

  1. Update global terragrunt.hcl with the following:

    terraform {
    extra_arguments "auto_plan_file" {
    commands = [
      "plan",
    ]
    
    arguments = ["-out=tfplan.output"]
    }
    }

    Next run the following:

    find . -name tfplan.output -exec tf-summarize {} \;

But I don't really like the output. I think there is a need to add some filtering for resources that are not being updated:

+--------+----------+
| CHANGE | RESOURCE |
+--------+----------+
+--------+----------+
+--------+----------+
| CHANGE | RESOURCE |
+--------+----------+
+--------+----------+
+--------+----------+
| CHANGE | RESOURCE |
+--------+----------+
+--------+----------+
+--------+----------+
| CHANGE | RESOURCE |
+--------+----------+
+--------+----------+
armenr commented 8 months ago

+1 to terragrunt support. This would be a huge DX and quality-of-life improvement. I'd donate money to see that developed.

thiagomaeda commented 8 months ago

It would be great to have terragrunt support, the logs there are far from the ideal size.

tide-jamiegwatkin commented 1 month ago

Likewise we'd love terragrunt support!

rmittal123 commented 1 month ago

+1 for terragrunt support.

prabhu43 commented 4 weeks ago

tf-summarize works on plan generated from terragrunt plan -out tg_plan.txt.

the issue here is terragrunt run-all command cannot generate a single plan output file. it will generate plan for each and every module in its respective folder

Alternate solution:

#!/bin/bash

# Find all files named "tgplan" and iterate over each
find . -type f -name "tgplan" | while read -r tgplan_file; do
  # Get the directory of the tgplan file
  dir=$(dirname "$tgplan_file")

  cd "$dir" || exit

  echo "Running tf-summarize on $tgplan_file in directory $dir"
  tf-summarize tgplan

  cd -
done
dineshba commented 3 weeks ago

Thanks @prabhu43 and @yyarmoshyk for the comments with steps to use tf-summarize with terragrunt.

Is this helpful?

Or do you expect different support in tf-summarize? if yes, you can please provide details on the requirement. cc: @rmittal123 @tide-jamiegwatkin @thiagomaeda @armenr @Alexey-Tsarev

LEI commented 1 week ago

While this works for individual plans, it seems to lack a way to execute a final command after all plans to summarize without a second command invocation:

terraform {
  extra_arguments "auto_plan_file" {
    commands  = ["plan"]
    arguments = ["-out=$TF_PLAN_NAME"]
  }

  after_hook "after_hook" {
    commands = ["plan"]
    execute  = ["sh", "-c", "find . -name $TF_PLAN_NAME -exec tf-summarize {} \\;"]
  }

  extra_arguments "summarize" {
    commands = ["plan"]
    env_vars = {
      TF_BINARY    = "tofu"
      TF_PLAN_NAME = "plan.cache"
    }
  }
}

Currently this has to be scripted out of Terragrunt:

export TF_BINARY=tofu TF_PLAN_NAME=plan.cache
find . -name plan.cache -exec sh -c 'plan="{}"; cd "${plan%'"$TF_PLAN_NAME"'}" && tf-summarize -json '"$TF_PLAN_NAME" \; | jq '.[] | select(keys | length > 0)'

Maybe this could be solved in Terragrunt with a new after_all hook, or a final_hook similar to error_hook.

Note: the output of run-all plan could be directly piped to jq for formatting, though I am trying to reduce plumbing around each command.