Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to use of terragrunt instead of terraform #224

Closed
ctrlaltdev opened this issue Nov 16, 2020 · 11 comments
Closed

Enable to use of terragrunt instead of terraform #224

ctrlaltdev opened this issue Nov 16, 2020 · 11 comments
Assignees
Labels
enhancement New feature or request
Projects

Comments

@ctrlaltdev
Copy link

I always use terragrunt as a wrapper around terraform because I reuse terraform modules between environments or accounts - and it also allows me to dynamically configure the tf backend and provider I use.

So technically, I don't have a tfdir to provide infracost, as terragrunt only 'compiles' it when I run it.

The good news is most of the terraform commands works with terragrunt (terragrunt show for instance)

So I think something like --use-tg that would substitute terraform with terragrunt would be great!

@ctrlaltdev
Copy link
Author

If you want an example of what my IAC looks like with terragrunt - based on terragrunt recommended structure: https://github.com/ctrlaltdev/ctrlalt.dev/tree/main/infra

Under modules, you will find the TF modules, and under config, you will find the terragrunt settings to use those modules.

@alikhajeh1 alikhajeh1 added the enhancement New feature or request label Nov 16, 2020
@alikhajeh1
Copy link
Member

The following is copied from https://gitlab.com/infracost/infracost-gitlab-ci/-/issues/1#note_448546649 as it's also a recent and relevant request:

Ayrton: Also, do you know anyone that is using infracost with terragrunt? My new issue is that terragrunt creates separated tfstate for each module (which is good) but for infracost I can only use one tfstate file at time, so the only way I found was creating new pipeline steps for each module which made the pipeline huge (I have more than 20 modules). I'm trying to find a way of using terragrunt for consolidating all the states in a single file just for infracost.

@ctrlaltdev
Copy link
Author

So maybe mimick terragrunt *-all command behavior

In terragrunt, you can either go in a module and run a command like terragrunt plan or you can go to the parent folder that holds multiple modules and run terragrunt plan-all and will run terragrunt plan in the modules in parallel.

Maybe infracost could have a similar way of running for terragrunt, that will look recursively in the children folders for terragrunt modules and run against them?

@robh007
Copy link
Contributor

robh007 commented Nov 17, 2020

With terragrunt you have an option of a pre & post hook, maybe that's an option for infracost to run at either one of those points?

https://terragrunt.gruntwork.io/docs/features/before-and-after-hooks/

@ayr-ton
Copy link

ayr-ton commented Nov 17, 2020

@robh007 Hmmm, before apply seems a good one. And it could run for each directory, but probably the output will be messed up. Will try that just for seeing how things will break.

@robh007
Copy link
Contributor

robh007 commented Nov 17, 2020

Yea @ayr-ton I've had a little play with some code today.

  before_hook "run_infracost" {
    commands = ["apply", "plan"]
    execute = ["infracost"]
  }

So the above works but as you say the output is generated per directory, the output is to stdout & doesn't look any different. You could also perhaps output to a json file then use the hcl functions to merge the JSON files together.

The main issue I see is that the TF source is nested within a random directory structure. What you can also do with terragrunt is use a parameter download_dir & perhaps if infracost could then traverse directories finding .tf files as appose to always expecting the tf files at the top level that may work better. It would also mean that infracost could then use one location to find the TF code as appose to traversing directory structures / parsing hcl files & looking for 'hints' to where all the TF code might be.

@ayr-ton
Copy link

ayr-ton commented Nov 18, 2020

@robh007 Yeah, the output is uncomprehensive. :-(

@alikhajeh1 alikhajeh1 added this to To do (prioritized top to bottom) in Main Nov 19, 2020
@alikhajeh1
Copy link
Member

alikhajeh1 commented Nov 19, 2020

@ctrlaltdev and @ayr-ton, just FYI that @robh007, @aliscott, myself and another user were online on Discord#terragrunt so we had a spontaneous call to discuss the options. @aliscott is digging into this and we'll post updates here. Feel free to jump into #terragrunt if you prefer to have longer discussions there.

@alikhajeh1 alikhajeh1 moved this from To do (prioritized top to bottom) to In progress in Main Nov 20, 2020
@aliscott
Copy link
Member

We've added an infracost report command which might help with some Terragrunt use cases. It combines multiple infracost JSON files into a single table.

So at the moment for our own infrastructure I can get a table of all the costs by running a script like this:
https://github.com/infracost/infracost/blob/master/scripts/terragrunt/report_all.sh
This runs plan-all, saves all the plan files and then combines them into one table view.

This is now released in v0.7.4. Please try this out and let us know if it can help with your use cases.

@alikhajeh1
Copy link
Member

Given that we now have https://www.infracost.io/docs/#terragrunt-users, should we close this issue? People can still leave feedback here or open another ticket

Main automation moved this from In progress to Merged to master Dec 2, 2020
@alikhajeh1 alikhajeh1 moved this from Merged to master to Released (in Homebrew) in Main Dec 3, 2020
@ayr-ton
Copy link

ayr-ton commented Dec 3, 2020

@alikhajeh1 I liked it a lot. :-)
Thanks for your time collecting feedback regarding this feature. The infracost report did the thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Main
Released
Development

No branches or pull requests

5 participants