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 running infracost against a terraform.tfstate file #90

Closed
digitalronin opened this issue Sep 10, 2020 · 7 comments
Closed

Enable running infracost against a terraform.tfstate file #90

digitalronin opened this issue Sep 10, 2020 · 7 comments
Assignees
Labels
enhancement New feature or request

Comments

@digitalronin
Copy link

To get costs of an existing infrastructure setup, we need to run something like:

terraform init
infracost --tfdir .

So infracost does a terraform plan internally, and then uses the output of that to generate a report. Alternatively, we can do the terraform plan manually, and then supply the output to infracost.

Either way is quite slow.

All of the information required to report on existing infrastructure will be in the terraform.tfstate JSON file for the project, so if it were possible to run something like:

infracost --terraform-statefile=/path/to/my/project/terraform.tfstate

This could enable cost reports to be generated much more quickly - I'd guess a few seconds rather than several minutes, since it shouldn't be necessary to run terraform init beforehand either.

For many use-cases, this may not be an issue, but in my particular case, with 200+ terraform projects to analyse, it makes a big difference, and I imagine some others will be in a similar situation.

@aliscott aliscott added the enhancement New feature or request label Sep 10, 2020
@aliscott aliscott added this to To do in infracost Sep 14, 2020
@ctrlaltdev
Copy link

Bump for that issue - I'm using TerraGrunt and split my infra in a lot of modules and the work around needed are quite painful. Using the tfstate would really help.

@enribd
Copy link

enribd commented Nov 5, 2020

Since separating the infrastructure in several tfstates is a best practice in Terraform, it could be nice to process a set of tfstates instead of just one. Also reading them from cloud object storages like a S3 bucket.

@aliscott
Copy link
Member

aliscott commented Nov 6, 2020

#203 adds a --use-tfstate flag, which might solve this use case.

So in master you can run it with

# if running dev:
make run ARGS="--tfdir=/path/to/code --use-tfstate"

# if running binary:
infracost --tfdir=/path/to/code --use-tfstate

This still needs the terraform binary to run terraform show internally instead of directly reading the state file, which is the recommended way of interacting with the state (https://www.terraform.io/docs/state/index.html#format) and has the added benefit of working with both local and remote states.

From my testing, this is a lot faster than running terraform plan each time 😄

Let me know if this helps.

@ayr-ton
Copy link

ayr-ton commented Nov 10, 2020

I've created a new related issue here:

https://gitlab.com/infracost/infracost-gitlab-ci/-/issues/1

@alikhajeh1 alikhajeh1 moved this from To do to In progress in infracost Nov 13, 2020
@alikhajeh1 alikhajeh1 added this to In progress in Main Nov 13, 2020
@alikhajeh1
Copy link
Member

alikhajeh1 commented Nov 13, 2020

@ctrlaltdev / @enribd can you possibly try v0.7.2 (available on brew too), and see if the infracost --tfdir=/path/to/code --use-tfstate option is good enough to close this issue? It works with remote state. I haven't used TerraGrunt much so it would be great if you can create issues with any difficulties you hit with that.

Regarding several tfstates, maybe we can discuss it in #188 as it seems somewhat related.

@ctrlaltdev
Copy link

@alikhajeh1 It works fine!
Just used on a couple of modules and I get a nice output back!

Regarding terragrunt, I'll open an issue for that.

@alikhajeh1 alikhajeh1 self-assigned this Nov 19, 2020
@alikhajeh1
Copy link
Member

@ctrlaltdev thanks for creating #224, given the interest in that, I've put it at the top of the todo pile in https://github.com/infracost/infracost/projects/2

Closing this issue, new issues can be created for leftovers.

Main automation moved this from In progress to Merged to master Nov 19, 2020
@alikhajeh1 alikhajeh1 moved this from Merged to master to Released (in Homebrew) in Main Nov 19, 2020
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
infracost
  
In progress
Development

No branches or pull requests

6 participants