Hi everyone, I'm experiencing an issue where my Te...
# help
v
Hi everyone, I'm experiencing an issue where my Terraform plan (
tfplan.json
) shows changes in resources, but when I run
infracost diff
, the resulting
diff.json
doesn't show any differences in costs. Here's a summary of my setup and what I've tried so far: 1. Baseline: I have a baseline file (
infracost.json
) that I download from an S3 bucket. 2. Terraform Plan: I generate the Terraform plan and convert it to JSON using
terraform show -json
to create
tfplan.json
. 3. Ignored Resources: I have a list of resource types that I ignore (e.g.,
aws_security_group
,
null_resource
, etc.). 4. Commands: When I have a baseline, I run:
Copy code
infracost diff --show-skipped --path <path_to_tfplan.json> --compare-to <path_to_infracost.json> --format json --out-file diff.json
If no baseline is found, I run:
Copy code
infracost breakdown --show-skipped --path <path_to_tfplan.json> --format json --out-file breakdown.json
Problem: Despite the
tfplan.json
file showing resource changes that should affect costs, the
diff.json
output does not show any differences in cost. I have double-checked that both the baseline and plan files are correct and reflect the expected state. Questions: 1. What could cause
infracost diff
to not show any cost changes even when the Terraform plan (
tfplan.json
) indicates resource changes? (I've added aws rds instance as new resource) 2. Are there specific resource types or changes that
infracost
might not detect as cost-impacting? 3. Could there be any issues with how the baseline is set or formatted that would cause
infracost diff
to fail to detect changes? 4. Any other debugging steps or logging options I should enable to better understand why
infracost diff
isn’t reflecting the plan changes? I would appreciate any insights or suggestions on how to resolve this! Thank you!
l
@victorious-artist-11969 if you’re using a plan JSON file I would recommend just running
infracost diff
directly against it instead of using a baseline. Since the plan JSON file already has the state in it, Infracost shouldn’t need a baseline. See here for an example: https://www.infracost.io/docs/features/cli_commands/#option-2-terraform-plan-json-1
v
I want to explain what we're trying to achieve with Infracost in our GitLab pipelines and why we might be experiencing some issues. Here’s our current setup: 1. Initial Setup and Baseline Creation: ◦ We start with a new Terraform project in GitLab, where we have resources defined across AWS. ◦ After running
terraform plan
, we generate
*.tfplan.json
files for all resources. • Using a Bash loop, we run
infracost breakdown
for each plan file to create a detailed cost breakdown:
Copy code
infracost breakdown --show-skipped --path plan_file --format json --out-file /infracost/diffs/infracost-breakdown-${plan_name}.json
◦ These breakdowns are uploaded to S3 as our "master" baseline for the project. 1. Handling New Changes: ◦ When we add new resources, such as an AWS RDS instance, we run
terraform plan
again to generate updated plan files. • We then download the "master" baseline from S3 and use
infracost diff
to compare the new plan files with the baseline:
Copy code
infracost diff --show-skipped --path $tf_plan --compare-to baseline.json --format json --out-file diffs/infracost-diff-${plan_name}.json
◦ This process produces several
infracost-diff-*
files, as we have multiple pipelines with different resources. • Finally, we merge these diff files and post them as comments on the GitLab PR:
Copy code
infracost output --path /infracost/diffs/* --format json --out-file infracost-diff.json

infracost comment gitlab --path=infracost-diff.json --behavior=update
1. The Issue: ◦ We’re seeing cases where new resources, like an AWS RDS instance, do not appear in the Infracost output, and it reports 0 changes in infrastructure costs. Could you help us understand why this might be happening or if there’s anything we need to adjust in our approach?
l
@victorious-artist-11969 is there a specific reason you need to do step 1? If step 2 runs the following, it will do the diff entirely from the plans:
Copy code
infracost diff --show-skipped --path $tf_plan --format json --out-file diffs/infracost-diff-${plan_name}.json
v
We start by running
infracost breakdown
to create an initial
baseline.json
. This baseline captures the cost state of all existing resources in the Terraform project at the start. For any subsequent changes (e.g., adding new resources), we run
infracost diff
to compare the new
*.tfplan.json
files against this
baseline.json
. This helps us understand the cost impact of the changes relative to the initial setup. After the initial comparison, we update the baseline to reflect the latest state by using the output from
infracost diff
. This updated baseline is then used for future comparisons. In essence, we use
infracost breakdown
to establish the starting point, and then
infracost diff
to track and update changes, ensuring that our baseline remains current and accurately reflects the cost state of the infrastructure. Is there a more efficient approach to managing and updating the baseline, or any best practices we should follow to ensure we are capturing cost changes accurately? Thank you for your assistance!
l
There’s 2 options for running Infracost in a pipeline. 1. Running against the code directly. This is the recommended approach since it is normally faster and doesn’t require any cloud credentials, so if it’s possible to switch to this approach I would suggested. With this case you need to generate a baseline first similar to what you are doing, but instead of passing the plan file to the commands, you set the path to be the code directory. Since this approach is fast it can normally be run against the whole repo at once, so doesn’t need any results to be combined. 2. Running against the plan JSON. In this case you shouldn’t need to create a baseline first. When
infracost diff
is run against the plan it automatically uses the state within the plan as the baseline.
w
@victorious-artist-11969 I’ll just add that if you can, we recommend using the GitLab App as that means no changes are needed to your gitlab-ci.yml files. But if that’s not an option, then I suggest using our gitlab-ci.yml example here: https://gitlab.com/infracost/infracost-gitlab-ci That parses the HCL code directly as Alistair mentioned. I’d be happy to screenshare and help with any issues, email me ali.hosseini@infracost.io if you’d like to set this up.
v
What is the purpose of the
infracost breakdown
command if
infracost diff
can automatically use the state within the
tfplan.json
as the baseline? When should we use
infracost breakdown
instead of
infracost diff
?
Also, can You please explain, how Baseline cost differs everytime? Attaching image from terraform json plan of infracost diff, this has added rds instance t3.db.2xlarge, and baseline cost is 470$, if I add xlarge, it's - 120$, but terraform is not applied, and RDS instance is not existing. Also, prices are different, because this environment in different regions? 826$ < 944$
w
With plan JSON file, you should only use
infracost diff
in the CI/CD pipeline.
breakdown
is useful when parsing Terraform HCL. Re your second question: it’s hard to debug from that message as I don’t know what’s in the plan JSON file. Like we mentioned before, the majority of users use the Terraform HCL parsing option so the baseline will show whatever is on the default branch in PRs.
v
I am currently using only
infracost diff
for all of my
tfplan.json
files. After that, I merge the results using
infracost output
and add comments with
infracost comment gitlab
. I have a few questions regarding how the costs are calculated: 1. I've added a new resource, an AWS RDS instance, which is not yet applied; it only exists in the plan JSON. Initially, Infracost shows one baseline cost, but after I change the instance size from
xlarge
to
2xlarge
, the baseline cost changes. Could you explain why the baseline cost changes in this case? 2. How are the "New monthly costs" calculated? When I add the baseline cost and usage cost, the sum does not match the total that Infracost calculates in the pull request. Could you please help me understand these calculations? Thank you!
Hi @little-author-61621 Could you help me understand how Infracost calculates the costs? Thank you!
w
@victorious-artist-11969 the baseline should not be changing in the scenario you described. Can you try the HCL parsing option to see if that works ok?
v
Yeah, that's why I'm confused how it calculates. @white-airport-8778, do You mean to use
infracost breakdown
?