Hello! Maybe there's some instability in the CLI f...
# general
Hello! Maybe there's some instability in the CLI flags? I've opened an issue, every
Copy code
infracost breakdown --path
is returning the above for me:
Copy code
No such file or directory
Hi, trying to reproduce with v0.9.1 but haven't been able to yet.
No such file or directory
shows if we can't find the path and
Could not detect path type
shows if the path exists but we don't recognise the type. What type of path are you passing?
Its a json state file, curled from gitlab remote state backend I've tried: • infracost breakdown --path teste.json • infracost breakdown --path teste.state.json • infracost breakdown --path ./teste.json • infracost breakdown --path ./teste.state.json
Copy code
infracost breakdown --path ./terraform
pointing to the folder does indeed works
Was your statefile generated with
terraform show -json
? For statefiles we check for the following keys:
in the JSON.
No, I just download the JSON state file from gitlab. Should be like any JSON state file, right?
message has been deleted
So that looks like internal state snapshot format. Terraform also has the JSON output format which is what we support. This is the standard format that they recommend people use for integrating with since it doesn't change between versions. I think there's two things we should do here: 1. Update https://www.infracost.io/docs/guides/advanced_usage#terraform-state-json-file to clarify which format we support and details on how to generate it. 2. Look at if we can support the internal format. I know this probably isn't possible for remote backends, and might be more difficult because it's subject to change. In the meantime, you should be able to generate the state json by:
Copy code
cd /path/to/tf
terraform show -json > state.json
Oh yeah, I'm looking at the JSON output of terraform show and they're definitely different
Didn't know there was a difference between them, sorry.
I think updating the documentation would be a great first step!
Got another idea as well: 3. If we find we can't support the internal format, then at least we could detect it and show a better error in the CLI
👍 3
This will make my CI kinda tricky tho. I have 3 branches with 3 different states and i'm trying to generate a unique json report from all of them. In this case I was downloading the state files separatedly and generating a uniform report using
infracost output --path /tmp/state/*.json
I think I'm going to have to
git checkout
terraform plan
terraform output -json
every branch, and then generate the uniform report. Any better idea?
hmm interesting, how are you downloading the state files?
Unless we find a way to generate a JSON supported by infracost from the JSON snapshot state file.
Gitlab provides this endpoint:
curl --header 'Authorization:Bearer ${CI_JOB_TOKEN}' '<https://gitlab.com/api/v4/projects/xx/terraform/state/global>' -o /tmp/state/global.json
global is my branch/state name
Something like this might work, but I'm not sure it will in all cases:
Copy code
# Generate branch1 state JSON
mkdir -p /tmp/state/branch1
touch /tmp/state/branch1/main.tf
curl --header 'Authorization:Bearer ${CI_JOB_TOKEN}' '<https://gitlab.com/api/v4/projects/xx/terraform/state/branch1>' -o /tmp/state/branch1/state.json
terraform -chdir=/tmp/state/branch1/ init
terraform -chdir=/tmp/state/branch1/ show -json > /tmp/state/branch1.json

# Generate branch2 state JSON
mkdir -p /tmp/state/branch2
touch /tmp/state/branch2/main.tf
curl --header 'Authorization:Bearer ${CI_JOB_TOKEN}' '<https://gitlab.com/api/v4/projects/xx/terraform/state/branch12> -o /tmp/state/branch2/state.json
terraform -chdir=/tmp/state/branch2/ init
terraform -chdir=/tmp/state/branch2/ show -json > /tmp/state/branch2.json

# Run infracost
infracost breakdown --path=/tmp/state/*.json
I think your idea of checking out the code is probably the safest option as the moment. But I've added a ticket for supporting the internal state format, so we can start looking at that: https://github.com/infracost/infracost/issues/811. And also a PR for the docs: https://github.com/infracost/docs/pull/74
Dude, you're the best. I'll try the script you've write, thanks for being so fast in helping. Should I explain what we discussed in the issue or would you like to do it?
Yes please if possible! Thanks man simple smile let me know how you get on.
Will do! Thanks
The solution was very hacky, but hey, its working...
Copy code
    - mkdir -p /tmp/state
    - mkdir -p /tmp/json
    - mkdir -p /tmp/breakdown
    - cd /tmp/state
    - |
      cat > <http://main.tf|main.tf> << EOF
      terraform {
        required_providers {
          aws = {
            source  = "hashicorp/aws"
            version = "~> 3.38"
        backend "http" {
    - gitlab-terraform init
    - gitlab-terraform state pull > /tmp/state/global.json
    - gitlab-terraform show -json /tmp/state/global.json > /tmp/json/global.json
    - infracost breakdown --path /tmp/json/global.json --format json > /tmp/breakdown/global.json
    - infracost output --path /tmp/breakdown/*.json --format json > /tmp/report.json
    - INFRACOST_LOG_LEVEL=info infracost output --path /tmp/breakdown/*.json --format table
🙌 1
Awesome! Also, thanks for the tip that GitLab has a remote state backend - I never knew that before.
🎯 1
Yeah, its great! They even have this image that takes care of authenticating with the remote backend for you