gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
8.04k stars 975 forks source link

Piping output to file with terragrunt #965

Closed ivasilyev-servicetitan-com closed 4 years ago

ivasilyev-servicetitan-com commented 4 years ago

Hello.

I'm trying to get simplified output of terraform with the help of terraform-plan-reviewer and Terragrunt.

Basically, I'm running terragrunt plan-all -out=plan to generate plan files on every folder with TF files. Then I need to apply terraform show -json plan > plan.json but with the help of Terragrunt. I'm trying to run terragrunt "show -json plan > plan.json" at least in one folder but get the following

/data/dev/compute/create-scalesets-dev # TERRAGRUNT_DEBUG=true terragrunt "show -json plan > plan.json"
[terragrunt] 2019/11/29 07:54:35 Reading Terragrunt config file at /data/dev/compute/create-scalesets-dev/terragrunt.hcl
[terragrunt] [/data/dev/compute/create-scalesets-dev] 2019/11/29 07:54:35 Running command: terraform --version
[terragrunt] 2019/11/29 07:54:36 Running command: terraform show -json plan > plan.json
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
    destroy            Destroy Terraform-managed infrastructure
    env                Workspace management
    fmt                Rewrites config files to canonical format
    get                Download and install modules for the configuration
    graph              Create a visual graph of Terraform resources
    import             Import existing infrastructure into Terraform
    init               Initialize a Terraform working directory
    output             Read an output from a state file
    plan               Generate and show an execution plan
    providers          Prints a tree of the providers used in the configuration
    refresh            Update local state file against real resources
    show               Inspect Terraform state or plan
    taint              Manually mark a resource for recreation
    untaint            Manually unmark a resource as tainted
    validate           Validates the Terraform files
    version            Prints the Terraform version
    workspace          Workspace management

All other commands:
    0.12upgrade        Rewrites pre-0.12 module source code for v0.12
    debug              Debug output management (experimental)
    force-unlock       Manually unlock the terraform state
    push               Obsolete command for Terraform Enterprise legacy (v1)
    state              Advanced state management
[terragrunt] 2019/11/29 07:54:36 errors.MultiError Hit multiple errors:
exit status 127
/go/src/github.com/gruntwork-io/terragrunt/errors/multierror.go:24 (0x6c70b2)
/go/src/github.com/gruntwork-io/terragrunt/cli/cli_app.go:458 (0xd0053c)
/go/src/github.com/gruntwork-io/terragrunt/cli/cli_app.go:439 (0xd001f7)
/go/src/github.com/gruntwork-io/terragrunt/cli/cli_app.go:327 (0xcfeeeb)
/go/src/github.com/gruntwork-io/terragrunt/cli/cli_app.go:229 (0xcfe9a1)
/go/src/github.com/gruntwork-io/terragrunt/cli/cli_app.go:210 (0xcfe6ce)
/go/src/github.com/gruntwork-io/terragrunt/vendor/github.com/urfave/cli/app.go:490 (0xc9ba88)
/go/src/github.com/gruntwork-io/terragrunt/vendor/github.com/urfave/cli/app.go:264 (0xc99cfd)
/go/src/github.com/gruntwork-io/terragrunt/main.go:27 (0xd071b1)
/usr/local/go/src/runtime/proc.go:201 (0x42c1b7)
/usr/local/go/src/runtime/asm_amd64.s:1333 (0x457fe1)

Is there a way to do this with Terragrunt? I also looked at TG Hooks - but it doesn't look to be helpful in this scenario.

lorengordon commented 4 years ago

what happens if you just run terragrunt show -json plan > plan.json (without the quotes)?

ivasilyev-servicetitan-com commented 4 years ago

In that case it will run

[terragrunt] 2019/12/02 09:36:10 Running command: terraform show -json plan

and then will pipe terragrunt's output out into plan.json file - that's not what I need.

ivasilyev-servicetitan-com commented 4 years ago

I managed to do what I need with the following after_hook

after_hook "after_hook_plan" {
    commands     = ["plan"]
    execute      = ["sh", "-c", "terraform show -json plan > plan.json"]
  }
lorengordon commented 4 years ago

Terragrunt stdout is terraform stdout, so it should work. Actual terragrunt logging is wired to stderr... But, glad you got it working either way.

dverbeek84 commented 2 years ago

In the newer versions you can doe terragrunt run-all show -json plan

palmobar commented 2 years ago

@dverbeek84 it doesn't help to have all plans in one massive file.

abij commented 1 year ago

As a small addition you can scope this to run on non-windows, since sh is not supported. This is now possible using conditionals and get_platform.

after_hook "after_hook_plan" {
    # Windows does not support 'sh' commands, skip this step
    commands = get_platform() != "windows" ? ["plan"] : []
    execute  = ["sh", "-c", "terraform show -json plan > plan.json"]
}