Is there a way to have breakdown output be shown a...
# help
r
Is there a way to have breakdown output be shown and also saved to an output file? I want it to display the output in the build log and also output to JSON to upload to Cloud. Currently running breakdown command twice, which probably isn’t ideal 😆
w
Hey Ryan, yep, here’s what you want:
Copy code
# on PR branch
infracost breakdown --path . --format json --out-file infracost-base.json

# on main branch
infracost diff --path . --compare-to infracost-base.json --format json --out-file infracost.json

# The infracost.json is the key file you want

# Log the breakdown format:
infracost output --path infracost.json --format table

# Upload to infracost cloud
infracost upload --path infracost.json
if you’re already using
infracost comment
and have the Org Settings > Cost Monitoring Dashboard ticked on, you don’t need to manually run
upload
as the comment command does that when the dashboard is enabled.
r
Got it, so run breakdown to generate the JSON file, then use output to show it. Makes sense, thanks!
w
yep, and
diff
to compare the baseline with the PR branch. The GitLab CI example shows how we use them together to also do a diff: https://gitlab.com/infracost/infracost-gitlab-ci (this is parsing the HCL code directly, so no need for tf init/plan etc). let me know if you want to run infracost with a TF plan JSON and I’ll give you slightly different steps
r
I’m currently passing the tfplan output file into the breakdown command using —path
infracost breakdown --path ${namespace}_${project}_${environment}.tfplan --format json --out-file infracost.json
w
in that case, I’d recommend something like this, where you generate the TF plan JSON (using
terraform show -json
), then you run
Copy code
infracost diff --path tf-plan.json --format json --out-file infracost.json

# The infracost.json is the key file you want

# Log the breakdown format:
infracost output --path infracost.json --format table

# Log the diff format:
infracost output --path infracost.json --format diff

# Post an MR comment
infracost comment gitlab --path=infracost.json ...

# Upload to infracost cloud
infracost upload --path infracost.json
r
I’m actually using Terraform JSON configuration here instead of raw HCL
The TF JSON gets generated by automation and then gets excuted
If that changes anything
w
yep, notice the
infracost diff
command is reading the Terraform plan JSON in my second example above
Basically
infracost breakdown
is less useful in CI pipelines when you already have a TF plan JSON file, since
infracost diff
can read that TF plan JSON file directly and use it to get the baseline from the plan JSON (so no need to run breakdown at all)
One note about Infracost Cloud, if you notice that the dashboard isn’t showing the MR cost estimate, it’s probably because the CLI isn’t able to detect metadata such as the MR title/author from the env, so you can set these env vars just before you run
infracost diff
and it will use that metadata. If you
cat infracost.json
you should see the metadata from the env too.