Hi, I'm trying to run infracost in github actions ...
# help
n
Hi, I'm trying to run infracost in github actions with usage based files. My workflow is similar to this https://github.com/infracost/actions (mine is reusable workflow), just passed the usage file path while generating the infracost breakdown and infracost diff. Based on the usage file it's estimating the cost of usage based resources. But infracost diff assumes that the usage based resources that are already in the base branch is gonna deleted/modified. So for the old resources it's subtracting the cost and the difference amount is always in negative.
image.png
l
@narrow-city-27809 are you able to paste the workflow YAML you are using here?
n
Hi @little-author-61621 Please find the main snippet of the workflow - name: Setup Infracost uses: infracost/actions/setup@v3 with: api-key: ${{ steps.get_secrets.outputs.infracost_api_key }} - name: Checkout base branch uses: actions/checkout@v4 with: ref: ${{ github.event.pull_request.base.ref }} - name: Checkout usage file branch uses: actions/checkout@v4 with: repository: repo with/infracost-usage-file ref: ${{ inputs.infracost-usage-file-branch }} path: infracost-usage token: ${{ steps.get_secrets.outputs.github_access_token }} - name: Generate Infracost cost estimate baseline run: | infracost breakdown --path=${{ inputs.workdir }} \ --usage-file=infracost-usage/.infracost-usage/${{ inputs.infracost-usage-file }} \ --format=json \ --project-name=${{ inputs.environment }}/${{ inputs.workdir }} \ --terraform-var-file ${{ inputs.tf-var-file }} \ --out-file=/tmp/infracost-base-${{ inputs.environment }}.json - name: Checkout PR branch uses: actions/checkout@v4 with: ref: ${{ github.head_ref }} - name: Generate Infracost diff id: infracost_diff run: | infracost diff --path=${{ inputs.workdir }} \ --usage-file=infracost-usage/.infracost-usage/${{ inputs.infracost-usage-file }} \ --format=json \ --project-name=${{ inputs.environment }}/${{ inputs.workdir }} \ --terraform-var-file ${{ inputs.tf-var-file }} \ --compare-to=/tmp/infracost-base-${{ inputs.environment }}.json \ --out-file /tmp/infracost-diff-${{ inputs.environment }}.json - name: Update comment with latest plan id: update_comment run: | infracost comment github --path=/tmp/infracost-diff-${{ inputs.environment }}.json \ --repo=$GITHUB_REPOSITORY \ --github-token=${{ github.token }} \ --pull-request=${{ github.event.pull_request.number }} \ --behavior=update
c
Hi @narrow-city-27809, could you share the change between the base and head refs?
n
Hi @crooked-daybreak-55253, I've added a S3 bucket in the PR branch resource "aws_s3_bucket" "subrights_s3_test" { bucket = "infracost-test-bucket" tags = { Environment = var.environment Product = var.product Contact = var.contact } } Estimate details:
Copy code
Key: * usage cost, ~ changed, + added, - removed

──────────────────────────────────
Project: dev/maininfra

+ aws_s3_bucket.subrights_s3_test
  Monthly cost depends on usage

    + Standard

        + Storage
          Monthly cost depends on usage
            +$0.023 per GB

        + PUT, COPY, POST, LIST requests
          Monthly cost depends on usage
            +$0.005 per 1k requests

        + GET, SELECT, and all other requests
          Monthly cost depends on usage
            +$0.0004 per 1k requests

        + Select data scanned
          Monthly cost depends on usage
            +$0.002 per GB

        + Select data returned
          Monthly cost depends on usage
            +$0.0007 per GB

~ module.vpc.aws_nat_gateway.nat[0]
  -$5 ($40 → $35)

    ~ Data processed
      -$5, -111 GB*

~ module.vpc.aws_nat_gateway.nat[1]
  -$5 ($40 → $35)

    ~ Data processed
      -$5, -111 GB*

~ module.vpc.aws_route53_record.parent-ns
  -$15

    ~ Standard queries (first 1B)
      -$5, -12.5 1M queries*

    ~ Latency based routing queries (first 1B)
      -$5, -8.3333 1M queries*

    ~ Geo DNS queries (first 1B)
      -$5, -7.1429 1M queries*

Monthly cost change for dev/maininfra
Amount:  -$26 ($96 → $71)
Percent: -27%

──────────────────────────────────
Key: * usage cost, ~ changed, + added, - removed

*Usage costs can be estimated by updating Infracost Cloud settings, see docs for other options.

31 cloud resources were detected:
∙ 5 were estimated
∙ 26 were free
I'm using this usage file - https://github.com/infracost/infracost/blob/master/infracost-usage-defaults.small.yml
c
It seems like the usage file is not present after you checkout PR branch. Maybe because it’s only checked in on the base branch? You could try checking it in on the PR branch, or maybe copy it to a /tmp/ folder before you checkout the PR branch.
👀 1
n
Hi @crooked-daybreak-55253 It worked, Thank you 🙂
👍 1