• Stars
    star
    224
  • Rank 177,792 (Top 4 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created about 3 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

GitHub Action for Infracost. See cloud cost estimates for Terraform in pull requests. πŸ’°πŸ“‰ Love your cloud bill!

Infracost GitHub Actions

Try the GitHub App

πŸ‘‰πŸ‘‰ We recommend using the free Infracost GitHub App instead as it has many benefits over GitHub Actions


This project provide a GitHub Action and examples for Infracost, so you can see cloud cost estimates for Terraform in pull requests πŸ’°

Example screenshot

Quick start

The following steps assume a simple Terraform directory is being used, we recommend you use a more relevant example if required.

  1. If you haven't done so already, download Infracost and run infracost auth login to get a free API key.

  2. Retrieve your Infracost API key by running infracost configure get api_key.

  3. Create a repo secret called INFRACOST_API_KEY with your API key.

  4. Create a new file in .github/workflows/infracost.yml in your repo with the following content.

# The GitHub Actions docs (https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#on)
# describe other options for 'on', 'pull_request' is a good default.
on: [pull_request]
env:
  # If you use private modules you'll need this env variable to use
  # the same ssh-agent socket value across all jobs & steps.
  SSH_AUTH_SOCK: /tmp/ssh_agent.sock
jobs:
  infracost:
    name: Infracost
    runs-on: ubuntu-latest
    permissions:
      contents: read
      # Required to post comments
      pull-requests: write

    env:
      TF_ROOT: examples/terraform-project/code
      # If you're using Terraform Cloud/Enterprise and have variables or private modules stored
      # on there, specify the following to automatically retrieve the variables:
      #   INFRACOST_TERRAFORM_CLOUD_TOKEN: ${{ secrets.TFC_TOKEN }}
      #   INFRACOST_TERRAFORM_CLOUD_HOST: app.terraform.io # Change this if you're using Terraform Enterprise

    steps:
      # If you use private modules, add an environment variable or secret
      # called GIT_SSH_KEY with your private key, so Infracost can access
      # private repositories (similar to how Terraform/Terragrunt does).
      # - name: add GIT_SSH_KEY
      #   run: |
      #     ssh-agent -a $SSH_AUTH_SOCK
      #     mkdir -p ~/.ssh
      #     echo "${{ secrets.GIT_SSH_KEY }}" | tr -d '\r' | ssh-add -
      #     ssh-keyscan github.com >> ~/.ssh/known_hosts

      - name: Setup Infracost
        uses: infracost/actions/setup@v2
        # See https://github.com/infracost/actions/tree/master/setup for other inputs
        # If you can't use this action, see Docker images in https://infracost.io/cicd
        with:
          api-key: ${{ secrets.INFRACOST_API_KEY }}

      # Checkout the base branch of the pull request (e.g. main/master).
      - name: Checkout base branch
        uses: actions/checkout@v3
        with:
          ref: '${{ github.event.pull_request.base.ref }}'

      # Generate Infracost JSON file as the baseline.
      - name: Generate Infracost cost estimate baseline
        run: |
          infracost breakdown --path=${TF_ROOT} \
                              --format=json \
                              --out-file=/tmp/infracost-base.json

      # Checkout the current PR branch so we can create a diff.
      - name: Checkout PR branch
        uses: actions/checkout@v3

      # Generate an Infracost diff and save it to a JSON file.
      - name: Generate Infracost diff
        run: |
          infracost diff --path=${TF_ROOT} \
                          --format=json \
                          --compare-to=/tmp/infracost-base.json \
                          --out-file=/tmp/infracost.json

      # 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.
      - name: Post Infracost comment
        run: |
            infracost comment github --path=/tmp/infracost.json \
                                     --repo=$GITHUB_REPOSITORY \
                                     --github-token=${{github.token}} \
                                     --pull-request=${{github.event.pull_request.number}} \
                                     --behavior=update
  1. πŸŽ‰ That's it! Send a new pull request to change something in Terraform that costs money. You should see a pull request comment that gets updated, e.g. the πŸ“‰ and πŸ“ˆ emojis will update as changes are pushed!

    If there are issues, check the GitHub Actions logs and this page.

    Example pull request
  2. In Infracost Cloud, go to Org Settings and enable the dashboard, then trigger your CI/CD pipeline again. This causes the CLI to send its JSON output to your dashboard; the JSON does not contain any cloud credentials or secrets, see the FAQ for more information.

    This is our SaaS product that builds on top of Infracost open source. It enables team leads, managers and FinOps practitioners to setup tagging policies, guardrails and best practices to help guide the team. For example, you can check for required tag keys/values, or suggest switching AWS GP2 volumes to GP3 as they are more performant and cheaper.

    Infracost Cloud gives team leads, managers and FinOps practitioners visibility across all cost estimates in CI/CDCommunicate and enforce FinOps tags in pull requests.

Troubleshooting

Permissions issue

If you receive an error when running the infracost comment command in your pipeline, it's probably related to ${{ github.token }}. This is the default GitHub token available to actions and is used to post comments. The default token permissions are read-only by default and pull-requests: write is required. If you are using SAML single sign-on, you must first authorize the token.

The add GIT_SSH_KEY step fails

If you are using private modules and receive a option requires an argument -- a error in the add GIT_SSH_KEY step:

  1. Make sure you have the following set in your workflow SSH_AUTH_SOCK:
    env:
      SSH_AUTH_SOCK: /tmp/ssh_agent.sock
  2. Try changing the ssh-agent -a $SSH_AUTH_SOCK line to the following:
    ssh-agent -a "${{ env.SSH_AUTH_SOCK }}"

Examples

The examples directory demonstrates how these actions can be used for different projects. They all work by using the default Infracost CLI option that parses HCL, thus a Terraform plan JSON is not needed.

For advanced use cases where the estimate needs to be generated from Terraform plan JSON files, see the plan JSON examples here.

Contributing

Issues and pull requests are welcome! For development details, see the contributing guide. For major changes, including interface changes, please open an issue first to discuss what you would like to change. Join our community Slack channel, we are a friendly bunch and happy to help you get started :)

License

Apache License 2.0

More Repositories

1

infracost

Cloud cost estimates for Terraform in pull requestsπŸ’°πŸ“‰ Shift FinOps Left!
Go
10,979
star
2

vscode-infracost

See cost estimates for Terraform right in your editorπŸ’°πŸ“‰
TypeScript
1,792
star
3

cloud-pricing-api

GraphQL API for cloud pricing. Contains over 3M public prices from AWS, Azure and GCP. Self-updates prices via an automated weekly job.
TypeScript
348
star
4

infracost-atlantis

Atlantis integration for Infracost. Shows cloud cost estimates for Terraform in pull requests.
Dockerfile
129
star
5

infracost-gh-action

GitHub Action for Infracost. Shows cloud cost estimates for Terraform in pull requests.
120
star
6

infracost-azure-devops

Azure DevOps integration for Infracost. Shows cloud cost estimates for Terraform in pull requests for Azure DevOps repos and GitHub repos.
TypeScript
30
star
7

helm-charts

Official Infracost Helm charts
Smarty
29
star
8

compost

Post pull request comments from multiple CI platforms
TypeScript
29
star
9

terraform-provider-infracost

This repo is deprecated, please use the https://infracost.io/usage-file/ instead.
Go
20
star
10

infracost-jenkins

Jenkins integration for Infracost. Shows cloud cost estimates for Terraform.
19
star
11

docs

Infracost docs
JavaScript
12
star
12

infracost-orb

CircleCI Orb for Infracost. Shows cloud cost estimates for Terraform in GitHub and BitBucket pull requests.
11
star
13

gh-actions-demo

Demo repo for Infracost GitHub Action
HCL
10
star
14

docusaurus-plugin-plausible

Docusaurus plugin for Plausible analytics
JavaScript
9
star
15

infracost-circleci

Atlantis integration for Bitbucket. Shows cloud cost estimates for Terraform in pull requests.
8
star
16

example-terraform

Example Terraform project
HCL
6
star
17

jetbrains-infracost

See cost estimates for Terraform right in your editorπŸ’°πŸ“‰
Kotlin
5
star
18

setup-infracost

Use https://github.com/infracost/actions
TypeScript
4
star
19

compost-action

Post pull request comments from multiple CI platforms
TypeScript
4
star
20

atlantis-demo

Demo repo for Infracost Atlantis integration
HCL
4
star
21

circleci-github-demo

Demo repo for Infracost running in CircleCI with GitHub
HCL
3
star
22

compost-go

Post pull request comments to multiple VCSs
Go
3
star
23

compost-e2e-tests-template

Testing repo for Compost E2E tests
2
star
24

infracost-gitlab-ci

GitLab CI examples for Infracost. Shows cloud cost estimates for Terraform in GitLab merge requests
2
star
25

cross-account-link

A Terraform module to set up an AWS cross-account link for Infracost Cloud.
HCL
1
star
26

jenkins-demo

Demo repo for Infracost running in Jenkins
HCL
1
star
27

compost-e2e-tests

Compost E2E test repo
1
star
28

azure-devops-github-demo

Demo repo for Infracost Azure DevOps integration
HCL
1
star
29

demo-repo

Demo repo for Infracost Cloud
HCL
1
star