This message was deleted.
# general
b
This message was deleted.
🎉 3
💯 1
👏 1
🎯 1
d
Hi @mysterious-teacher-68276, Thanks for rolling out this. Today i tried HCL parsing as most of the our project is taking too much time when running
terraform plan
. i am following below steps in my project
Copy code
1. infracost breakdown --path . --terraform-parse-hcl  --format json --out-file /tmp/infracost.json
2. ./infracost comment github --path /tmp/infracost.json --repo $GITHUB_REPOSITORY --github-token ${{ secrets.GITHUB_TOKEN }} --pull-request ${{github.event.pull_request.number}} --github-api-url https://<internal_git_repo> --behavior update --policy-path cost-policy.rego
but i am seeing the original cost is showing zero value . Looks like i am missing somethings. Could you please validate the above steps are correct to bypass execution terraform steps. FYI: when running with terraform, everything coming properly
m
Morning @delightful-market-27721 , just in a meeting at the moment, but let me get back to you ASAP once it’s over
d
Sure no hurry, take your time. Anyway i am going to look this tomorrow only 👍
m
Hey @delightful-market-27721 , yes unfortunately parsing HCL doesn’t support past state yet. As outlined by the issue you referenced https://github.com/infracost/infracost/issues/1567. However, we have an open PR for this https://github.com/infracost/infracost/pull/1570 and I expect this feature to be in the next release
d
Hi @mysterious-teacher-68276, looks like Infracost v0.9.23 is live! Can you please confirm if i can start using HCL parsing and avoid using terraform init and plan steps.
m
Hey @delightful-market-27721, yes, this functionality is live. To get this working on your PRs, you’ll need to generate a cost snapshot first from the base branch and then use that snapshot as a reference in the PR. Here’s a working example using github actions:
Copy code
name: Terraform directory
on: [pull_request]

jobs:
  terraform-directory:
    name: Terraform directory
    runs-on: ubuntu-latest

    steps:
      # Checkout the branch you want Infracost to compare costs against. This example is using the 
      # target PR branch.
      - name: Checkout base branch
        uses: actions/checkout@v2
        with:
          ref: '${{ github.event.pull_request.base.ref }}'

      - name: Setup Infracost
        uses: infracost/actions/setup@v1
        with:
          api-key: ${{ secrets.INFRACOST_API_KEY }}

      # Generate an Infracost output JSON from the comparison branch, so that Infracost can compare the cost difference.
      - name: Generate Infracost cost snapshot
        run: |
          infracost breakdown --terraform-parse-hcl --path examples/terraform-directory/code \
                              --format json \
                              --out-file /tmp/prior.json
          
      # Checkout the PR branch with your infrastructure changes.
      - uses: actions/checkout@v2

      - name: Run Infracost
        run: |
          infracost breakdown --terraform-parse-hcl --path examples/terraform-directory/code \
                              --format json \
                              --compare-to /tmp/prior.json \
                              --out-file /tmp/infracost.json

      - name: Post Infracost comment
        run: |
          # Posts a comment to the PR using the 'update' behavior.
          # This creates a single comment and updates it. The "quietest" option.
          # The other valid behaviors are:
          #   delete-and-new - Delete previous comments and create a new one.
          #   hide-and-new - Minimize previous comments and create a new one.
          #   new - Create a new cost estimate comment on every push.
          # See <https://www.infracost.io/docs/features/cli_commands/#comment-on-pull-requests> for other options.
          infracost comment github --path /tmp/infracost.json \
                                   --repo $GITHUB_REPOSITORY \
                                   --github-token ${{github.token}} \
                                   --pull-request ${{github.event.pull_request.number}} \
                                   --behavior update
You can read more about the functionality here: https://www.infracost.io/docs/features/cli_commands/#option-1-terraform-directory
d
nice . Thanks @mysterious-teacher-68276 👍
Copy code
i am getting error `unknown flag: --compare-to
below is the command
Copy code
docker run --rm -e INFRACOST_API_KEY=<key>  -v $PWD/:/code/ infracost/infracost breakdown --terraform-parse-hcl --path /code/infrastructure --format json --compare-to /tmp/prior.json  --out-file /tmp/infracost.json
Error: unknown flag: --compare-to
Copy code
infracost version is v0.9.24
Copy code
docker run --rm -e INFRACOST_API_KEY=<key> -e -v $PWD/:/code/ infracost/infracost --version

Infracost v0.9.24
c
Hi, can you try changing that second
infracost breakdown …
command to
infracost diff …
. We modified the interface so that
--compare-to
work with
infracost diff
because people were finding it confusing to run breakdown twice.
d
ohh okay Thanks @crooked-daybreak-55253
yup that works 👍
👍 1
@crooked-daybreak-55253 we have to use
--compare-to
option with
breakdown
otherwise output will be not json format and
infracost comment
will fail . I am not sure why first time it failed saying
unknown flag: --compare-to
when used with
breakdown
. But all good now and things are working .
@mysterious-teacher-68276 it looks like we are still running terraform
init
and
show
as part of
breakdown
and
--compare-to
combination . I was in impression we wont required running terraform
init
and
show
and still required AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY . So it is not helping my use-case . i al looking to skip running terraform
init
and
plan
all together, so that i don't need to pass AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. if i ran
infracost diff
, which is not generating json output so can not use
infracost comment
.
screen shot when running breakdown with compare-to and have to pass AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
Copy code
infracost breakdown --terraform-parse-hcl --path examples/terraform-directory/code \
                              --format json \
                              --compare-to /tmp/prior.json \
                              --out-file /tmp/infracost.json
m
Hey @delightful-market-27721 this shouldn't be running terraform init as you've included the terraform-parse-hcl flag. This should just sidestep the terraform binary and read the raw values. Not sure what's going on here. I'm on holiday at the moment, so I'll ask one of the other guys to look at this. cc @busy-agent-35515, @little-author-61621
d
ohh sorry to bother you , enjoy your vacation . yeah unfortunately it is running terraform init and forcing me to put AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
Also when i tried diff command as per the documents, https://www.infracost.io/docs/features/cli_commands/#option-3-terraform-cli to generate json format. but infra-cost diff thrown an error saying
Copy code
USAGE
  infracost diff [flags]

Error: unknown flag: --format
Just in summary for any one from infra-cost as this is now big thread 🙂 1.
infracost breakdown with --terraform-parse-hcl
, still running terraform init, see https://infracost-community.slack.com/archives/C01G63Q4HS7/p1651814027730029?thread_ts=1649852581.140919&amp;cid=C01G63Q4HS7 2. infracost diff command with --format json throwing error unknown flag: --format , but document say we can use it , see https://infracost-community.slack.com/archives/C01G63Q4HS7/p1651816224568659?thread_ts=1649852581.140919&amp;cid=C01G63Q4HS7
and my use-case is, i am looking to remove dependencies on terraform init or plan because of two reasons 1. We have big terraform repos and running init and plan is taking too much time for each PR. 2. Forcing me to put AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
l
Hi @delightful-market-27721 I just confirmed this is working for me with v0.9.24 and returns a JSON output
Copy code
infracost diff --path=. --compare-to=prior.json --terraform-parse-hcl --format=json
I’m also not able to reproduce
infracost breakdown with --terraform-parse-hcl
still running terraform init. I wonder if somehow it’s pulling an old version of Infracost.
d
hmm , i am running these command as docker and pulling infra-cost docker image
l
Hmm.. can you run
docker run --rm -e INFRACOST_API_KEY=<key> -e -v $PWD/:/code/ infracost/infracost --version
again just to double check?
d
when i ran in local system i am seeing latest version docker run --rm -e INFRACOST_API_KEY=<key> -v $PWD/:/code/ infracost/infracost --version Infracost v0.9.24 looks like in github action some caching issue
i will debug it and let you know if in github action it is picking old docker image version
l
Thanks 🙏
d
Thanks for checking in your end
l
@delightful-market-27721 let me know how you get on and if I can help in any way