Greetings Infracost... I am getting the error (:th...
# help
f
Greetings Infracost... I am getting the error (🧵 ) when running
infracost diff --path=. --format=json --compare-to=infracost-base.json --out-file=infracost.json
where
infracost-base.json
is from
production/base
branch and
infracost.json
is from the
current
branch to be merged to
production
branch.
Copy code
+ export 'INFRACOST_API_KEY=****'
+ infracost diff '--path=.' '--format=json' '--compare-to=infracost-base.json' '--out-file=infracost.json'
time="2023-08-17T15:06:39Z" level=info msg="Enabled tag policies"
time="2023-08-17T15:06:40Z" level=info msg="Evaluating Terraform directory at ."
Detected Terraform project at .
Detected Terraform project at .
time="2023-08-17T15:06:40Z" level=info msg="Starting: Downloading Terraform modules"
time="2023-08-17T15:06:40Z" level=info msg="Starting: Downloading Terraform modules"
time="2023-08-17T15:06:40Z" level=info msg="Starting: Retrieving cloud prices to calculate costs"
Error: Invalid --compare-to Infracost JSON, found duplicate project name org/repo
l
Hi @famous-eve-48967. In the
infracost-base.json
are there any projects that have the same name?
f
I think let me share the
groovy
code/stages for more context
Copy code
stage(name: "infracost-base.json - production") {
                        sh """
                        export INFRACOST_API_KEY=${JENKINS_INFRACOST_API_KEY}
                        rm -rf /tmp/base/

                        git clone https://${GITHUB_TOKEN}@github.com/org/${githubRepo}.git --branch='master' --single-branch /tmp/base
                        infracost breakdown --path /tmp/base --format json \
                                --out-file /tmp/infracost-base.json
                        """
                    }
                    stage(name: "Infracost - Compare Costs"){
                        sh """
                        export INFRACOST_API_KEY=${JENKINS_INFRACOST_API_KEY}
                        infracost diff --path=. --format=json \
                                --compare-to=infracost-base.json \
                                --out-file=infracost.json
                        """
                    }
                    stage(name: "Infracost - Push Costs (PR)"){
                        sh """
                        infracost comment github --path=infracost.json \
                                --repo="org/${githubRepo}" \
                                --pull-request="${githubPullRequestNumber}" \
                                --github-token="${GITHUB_TOKEN}" \
                                --behavior=update
                        """
                        sh 'rm -rf /tmp/infracost*'
                    }
Hi @famous-eve-48967. In the
infracost-base.json
are there any projects that have the same name?
@little-author-61621, it's the same project
l
Okay thanks, are you able to check the contents of the
infracost-base.json
file? The issue might be because for some reason there’s identical projects in there.
f
Let me check it out
Copy code
{"version":"0.2","metadata":{"infracostCommand":"breakdown","vcsBranch":"master","vcsCommitSha":"begt3","vcsCommitAuthorName":"user","vcsCommitAuthorEmail":"<mailto:1313+user@users.noreply.github.com|1313+user@users.noreply.github.com>","vcsCommitTimestamp":"2023-08-17T14:22:20Z","vcsCommitMessage":"commit msg"vcsRepositoryUrl":"<https://github.com/org/repo.git>"},"currency":"USD","projects":[{"name":"org/repo","metadata":{"path":"/tmp/base","type":"terraform_dir","errors":[{"code":5,"message":"Failed to download remote module \"secor-service-account\" - could not download module git@github.com:org/terraform?ref=0636270 to cache error downloading '<ssh://git@github.com/org/terraform?ref=231343>': /usr/bin/git exited with 128: Cloning into '/tmp/base/.infracost/terraform_modules/9287349'…\nNo user exists for uid 1022\r\nfatal: Could not read from remote repository.\n\nPlease make sure you have the correct access rights\nand the repository exists.\n","data":{"source":"ssh"}}]},"pastBreakdown":{"resources":[],"totalHourlyCost":"0","totalMonthlyCost":"0"},"breakdown":{"resources":[],"totalHourlyCost":"0","totalMonthlyCost":"0"},"diff":{"resources":[],"totalHourlyCost":"0","totalMonthlyCost":"0"},"summary":{"totalDetectedResources":0,"totalSupportedResources":0,"totalUnsupportedResources":0,"totalUsageBasedResources":0,"totalNoPriceResources":0,"unsupportedResourceCounts":{},"noPriceResourceCounts":{}}},{"name":"org/repo","metadata":{"path":"/tmp/base","type":"terraform_dir","errors":[{"code":2,"message":"Error loading Terraform modules: could not load modules for path /tmp/base failed to inspect module path /tmp/base/.infracost/terraform_modules/1313/modules/service-accounts diag: Failed to read module directory: Module directory /tmp/base/.infracost/terraform_modules/13313/modules/service-accounts does not exist or cannot be read.","data":null}]},"pastBreakdown":{"resources":[],"totalHourlyCost":"0","totalMonthlyCost":"0"},"breakdown":{"resources":[],"totalHourlyCost":"0","totalMonthlyCost":"0"},"diff":{"resources":[],"totalHourlyCost":"0","totalMonthlyCost":"0"},"summary":{"totalDetectedResources":0,"totalSupportedResources":0,"totalUnsupportedResources":0,"totalUsageBasedResources":0,"totalNoPriceResources":0,"unsupportedResourceCounts":{},"noPriceResourceCounts":{}}}],"totalHourlyCost":"0","totalMonthlyCost":"0","pastTotalHourlyCost":"0","pastTotalMonthlyCost":"0","diffTotalHourlyCost":"0","diffTotalMonthlyCost":"0","timeGenerated":"2023-08-17T15:06:35.818194102Z","summary":{"totalDetectedResources":0,"totalSupportedResources":0,"totalUnsupportedResources":0,"totalUsageBasedResources":0,"totalNoPriceResources":0,"unsupportedResourceCounts":{},"noPriceResourceCounts":{}}}
l
Ok, so the odd thing is the duplicate
"name": "org/repo",
values in that file.
f
So how do I get red of it ??? since it's generated by the
infracost breakdown ..
cmd ??
l
Do you use any *.tfvar files?
f
@little-author-61621 Yes
Copy code
tree | grep tfvar 
├── ter.auto.tfvars
├── terraform.tfvars
l
It’s hard to tell exactly what the issue is at the moment, but the best thing to do at the moment is create a
infracost.yml
config file to specify the projects manually.
Copy code
version: 0.1
projects:
  - path: org/repo
    name: org-repo
    terraform_var_files:
      - ter.auto.tfvars
      - terraform.tfvars
And then run
infracost
against that config file:
Copy code
infracost breakdown --config-file=infracost.yml --format json --out-file /tmp/infracost-base.json
...
infracost diff --config-file=infracost.yml  --format=json --compare-to=infracost-base.json --out-file=infracost.json
f
On it 🦾
l
I’m wonder if the underlying issue is related to https://github.com/infracost/infracost/issues/2624. So would be interested in knowing if this still happens if the .auto.tfvars is removed temporarily.
f
@little-author-61621, that would make more sense actually 🤔
@little-author-61621, I was able to fix the error as per your suggestion, however now it seems it doesn't calculate the cost difference, code:
Copy code
stage(name: "infracost-base.json - production") {
                        
                        sh """
                        export INFRACOST_API_KEY=${JENKINS_INFRACOST_API_KEY}
                        
                        git checkout master

                        infracost breakdown --config-file /tmp/base/infracost.yaml --format json \
                                --out-file /tmp/base/infracost-base.json
                        """
                    }

                    stage(name: "Infracost - Compare Costs"){
                        sh """
                        export INFRACOST_API_KEY=${JENKINS_INFRACOST_API_KEY}
                        
                        git checkout ${env.BRANCH_NAME}

                        infracost diff --config-file=/tmp/base/infracost.yaml --format=json \
                                --compare-to=/tmp/base/infracost-base.json \
                                --out-file=/tmp/base/infracost.json
                        """
                    }

                    stage(name: "Infracost - Push Costs (PR)"){
                        sh """
                        infracost comment github --path=/tmp/base/infracost.json \
                                --repo="TAKEALOT/${githubRepo}" \
                                --pull-request="${githubPullRequestNumber}" \
                                --github-token="${GITHUB_TOKEN}" \
                                --behavior=update
                        """
                    }