Channels
  • Gabriel

    Gabriel

    1 year ago
    Hello! Maybe there's some instability in the CLI flags? I've opened an issue, every
    infracost breakdown --path
    is returning the above for me:
    No such file or directory
  • Alistair (Infracost)

    Alistair (Infracost)

    1 year ago
    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?
  • Gabriel

    Gabriel

    1 year ago
    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
    infracost breakdown --path ./terraform
    pointing to the folder does indeed works
  • Alistair (Infracost)

    Alistair (Infracost)

    1 year ago
    Was your statefile generated with
    terraform show -json
    ? For statefiles we check for the following keys:
    format_version
    and
    values
    in the JSON.
  • Gabriel

    Gabriel

    1 year ago
    No, I just download the JSON state file from gitlab. Should be like any JSON state file, right?
  • Alistair (Infracost)

    Alistair (Infracost)

    1 year ago
    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:
    cd /path/to/tf
    terraform show -json > state.json
  • Gabriel

    Gabriel

    1 year ago
    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!
  • Alistair (Infracost)

    Alistair (Infracost)

    1 year ago
    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
  • Gabriel

    Gabriel

    1 year ago
    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
    and
    terraform output -json
    every branch, and then generate the uniform report. Any better idea?
  • Alistair (Infracost)

    Alistair (Infracost)

    1 year ago
    hmm interesting, how are you downloading the state files?
  • Gabriel

    Gabriel

    1 year ago
    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
  • Alistair (Infracost)

    Alistair (Infracost)

    1 year ago
    Something like this might work, but I'm not sure it will in all cases:
    # 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
  • Gabriel

    Gabriel

    1 year ago
    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?
  • Alistair (Infracost)

    Alistair (Infracost)

    1 year ago
    Yes please if possible! Thanks man 😒imple_smile: let me know how you get on.
  • Gabriel

    Gabriel

    1 year ago
    Will do! Thanks
    The solution was very hacky, but hey, its working...
    script:  
        - 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" {
            }
          }
          EOF
        - 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
  • Alistair (Infracost)

    Alistair (Infracost)

    1 year ago
    Awesome! Also, thanks for the tip that GitLab has a remote state backend - I never knew that before.