Migration guide
See the GitHub Actions migration guide in the docs for migrating the Infracost GitHub Action v1 to v2.
What's new?
Back in February 2022, we started an experiment to estimate costs by parsing Terraform HCL code directly. We're excited to announce that the experiment worked! So in v0.10 we've removed the experimental --terraform-parse-hcl
flag and made HCL parsing the default behavior.
Going forward, we'll support two ways to run Infracost with Terraform via --path
:
- Parsing HCL code (directory): this is the default and recommended option as it has the following 4 key benefits.
# Terraform variables can be set using --terraform-var-file or --terraform-var infracost breakdown --path /code
- Parsing plan JSON file: this will continue to work as before.
cd /code terraform init terraform plan -out tfplan.binary terraform show -json tfplan.binary > plan.json infracost breakdown --path plan.json
1. Faster CLI
Infracost can now generate cost estimates without needing a Terraform plan. This removes our dependency on the Terraform binary altogether, which means no more terraform init
or terraform plan
.
That, in turn, means a super-fast CLI: Infracost used to take around 50 seconds to run on our internal Terraform mono-repo, that's now reduced to 2 seconds. 🚀
2. No cloud creds needed
Running terraform plan
requires users to have cloud credentials and Terraform secrets. The main reason we created the Infracost CLI first, instead of a web API, was so it could parse the Terraform plan JSON locally to extract cost-related parameters (e.g. instance type). Thus credentials and secrets were not sent anywhere.
Whilst this worked and was safe, it still posed a question: is there a way to avoid setting credentials or secrets altogether? Removing our dependency on terraform plan
gave us a way to do that.
3. Cost estimates everywhere
Not needing cloud credentials, or even knowledge of how to generate a Terraform plan, means that any engineer who has access to code repos can generate cost estimates!
This also opens the door for cost estimates to be put everywhere: Infra-as-Code repo readmes, Terraform module readmes, Visual Studio, and even in continuous integration systems where a Terraform plan does not exist (not everyone runs Terraform via continuous deployment systems).
To make infracost diff
work without a Terraform plan, we introduced a new --compare-to infracost-base.json
flag. This enables a git-based cost diff to be produced, e.g.:
git checkout main
infracost breakdown --path /code --format json --out-file infracost-base.json
git checkout my-branch
infracost diff --path /code --compare-to infracost-base.json
4. Compare Infracost runs
The infracost diff
command can now also be used to compare Infracost runs. Assuming you generated the files infracost-last-week.json
and infracost-today.json
using the infracost breakdown --path /code --format json
command, you can compare them using:
infracost diff --path infracost-today.json --compare-to infracost-last-week.json
5. Detect multi-project repos
Setting the --path
flag to a top-level repo directory will now attempt to process all projects automatically by:
- Looking at the specified path or in any of the subdirectories with a depth less than 5.
- Processing Terraform variable files with the
.auto.tfvars
extension (similar to what Terraform does). - Processing environment variables with a
TF_VAR_
prefix (similar to what Terraform does).
If this does not work for your use-case, use a config-file and run infracost breakdown --config-file=infracost.yml
, for example:
# infracost.yml
version: 0.1
projects:
- path: prod
terraform_var_files:
- prod.tfvars
- us-east.tfvars
- path: dev
terraform_var_files:
- dev.tfvars
Removed functionality
If you previously used the following two options to see the cost breakdown of the current Terraform state, you can now just run infracost breakdown --path /code
against the already-deployed branch.
- The
--terraform-use-state
flag has been removed. - Running Infracost against a Terraform state JSON file is no longer supported.
If you previously ran Infracost with a Terraform plan binary file, you should now generate a plan JSON file and use that instead:
terraform show -json tfplan.binary > plan.json
infracost breakdown --path plan.json
Known issues
- The
INFRACOST_TERRAGRUNT_FLAGS
environment variable is no longer supported as Infracost parses HCL code directly. Comment on this issue if you'd like a way to exclude certain directories. - HCL parsing does not work with private remote modules, subscribe to this issue for updates.
As a workaround you can still generate plan JSONs for these projects and pass the plan JSON to Infracost to get a cost estimate. We are looking to fix these issues in upcoming patch releases.
Full Changelog: v1...v2.0.0