Hello, trying to set up infracost but struggling a...
# general
g
Hello, trying to set up infracost but struggling a little. Using it as part of the CI/CD integration. Have a repo with multiple services and within those services I have nested directories - service/env/env. Within those you can find the tf files which references private modules and terragrunt.hcl (this references another hcl file located in the root directory). These modules are downloaded locally (same level as the env directory. eg. service/.downloaded/modules. I don't use any tfvars file as all values used are set as default values within the variable file. However, if values do need to be overriden then they will be added into the tf files where I reference the module. Is there any pointers on how to use infracost in such a set up?
m
Hi Henry this sounds like a very sane setup that Infracost project autodetect should work out the box with, are you using the GitHub app?
g
Initially, yes. But when testing by creating aws resources the infracost bot did not seem to pick up on any new changes
When using the GitHub app it says it cannot find:
Copy code
Unsupported attribute; This object does not have an attribute named "aws".
This object does not have an attribute named "terraform_version".
This object does not have an attribute named "dependencies".
m
Ok can you post the organisation id from Infracost cloud that you are running these tests on and I’ll look into this for you
g
sent DM, thanks!
Good morning, sent a DM @mysterious-teacher-68276 with the organisation id from infraclost cloud. Please let me know the update when you can, thanks!
m
morning @gentle-animal-85879, ah I did not see that on friday - let me look into it this morning
g
Hi @mysterious-teacher-68276 I tried using infracost with the terraform plan approach mentioned here: https://www.infracost.io/docs/features/cli_commands/#option-2-terraform-plan-json-1 The infracost breakdown works fine for the individual json files -
infracost-baseline.json
and
infracost.json
. But, when i use
infracost diff
with the two files I get this error:
Copy code
Error loading infracost.json used by --compare-to flag. invalid Infracost JSON file invalid character 'P' looking for beginning of value, generate it by running the following command then try again:
infracost breakdown --path /code --format json --out-file infracost-base.json
`infracost breakdown`does not work for me as it shows the errors mentioned earlier in the thread:
Copy code
Unsupported attribute; This object does not have an attribute named "aws".
This object does not have an attribute named "terraform_version".
This object does not have an attribute named "dependencies".
Got a new error message:
Copy code
Error: Error loading tfplan-new.json used by --compare-to flag. invalid Infracost JSON file version. Supported versions are 0.2 ≤ x ≤ 0.2
m
I’m looking at your errors now henry
Unsupported attribute; This object does not have an attribute named “aws”.
This object does not have an attribute named “terraform_version”.
This object does not have an attribute named “dependencies”.
Hi henry this error looks like to comes from the fact that your using terragrunt
run_cmd
to run terragrunt and fetch certain config files. Infracost does not support this with our HCL parsing approach as we do not exec any external binaries in our ephemeral runners. For this to work you’ll need to rely on the plan JSON, which you are already attempting. Can I request you paste the exact command you are using to : 1. generate a terraform plan json 2. run a diff From the error that you have sent it seems that the plan JSON is malformed somehow
g
Hi hugo, thank you for clarifying that with the
run_cmd
command.
Copy code
terragrunt plan -out tfplan-baseline.binary
terraform show -json tfplan-baseline.binary >tfplan-baseline.json
infracost breakdown --path tfplan-baseline.json
terragrunt plan -out tfplan-new.binary
terraform show -json tfplan-new.binary >tfplan-new.json
infracost breakdown --path tfplan-new.json
infracost diff --path tfplan-baseline.json --compare-to tfplan-new.json
These are the exact commands i am running. with the `infracost breakdown`command it successfully runs and displays the cost based off the terraform plan
m
the
--compare-to
is not needed with a tf plan json, this is solely for the HCL version, so what happens if you run
Copy code
infracost diff --path tfplan-baseline.json
g
Copy code
INFO Autodetected 1 Terraform plan JSON file project across 1 root module
INFO Found Terraform plan JSON file project tfplan-baseline.json at directory tfplan-baseline.json

──────────────────────────────────

9 cloud resources were detected:
∙ 1 was estimated
∙ 8 were free
m
ok looks good
and what does
infracost breakdown --path tfplan-baseline.json
give
g
i do get a cost estimation with
Copy code
infracost diff --path tfplan-new.json
m
yeah you will
as I’m assuming you’ve added something
g
If we are not supplying the baseline.json to infracost. How would it know what to compare the new json file to?
m
the compare to is not needed for the plan json method as we use the terraform state to calculate the diff of resources
g
That makes sense. Thank you for the assistance on this!
Also, just wanted to see if the `run_cmd`will be supported by infracost in the future? Thanks
m
it will not be added to our hosted services for the near term, as it is something we do not want to run unless in strict sandboxed environment. However, you can use any of our CI/CD integrations https://www.infracost.io/docs/integrations/cicd/ with the plan JSON integration and use all our hosted dashboard features