Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

License

Notifications You must be signed in to change notification settings

dinoshauer/actions

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

81 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Infracost GitHub Actions

This project provides a set of GitHub Actions 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. Retrieve your Infracost API key by running infracost configure get api_key. If you don't have one, download Infracost and run infracost register to get a free API key.

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

  3. Create required repo secrets for any cloud credentials that are needed for Terraform to run. If you have multiple projects/workspaces, consider using an Infracost config-file to define the projects.

    • Terraform Cloud/Enterprise users: if you use Remote Execution Mode, you should follow setup-terraform instructions to set the inputs cli_config_credentials_token, and cli_config_credentials_hostname for Terraform Enterprise.
    • AWS users: use aws-actions/configure-aws-credentials, the Terraform docs explain other options.
    • Azure users: the Terraform docs explain the options. The Azure/login GitHub Actions might also be useful; we haven't tested these with Terraform.
    • Google users: the Terraform docs explain the options, e.g. using GOOGLE_CREDENTIALS.
  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]
    jobs:
      infracost:
        runs-on: ubuntu-latest # The following are JavaScript actions (not Docker)
        env:
          working-directory: PATH/TO/TERRAFORM/CODE # Update this!
    
        name: Run Infracost
        steps:
          - name: Check out repository
            uses: actions/checkout@v2
    
          # Typically the Infracost actions will be used in conjunction with
          # https://github.com/hashicorp/setup-terraform. Subsequent steps
          # can run Terraform commands as they would in the shell.
          - name: Install terraform
            uses: hashicorp/setup-terraform@v1
            with:
              terraform_wrapper: false # This is required so the `terraform show` command outputs valid JSON
    
          # IMPORTANT: add any required steps here to setup cloud credentials so Terraform can run
    
          - name: Terraform init
            run: terraform init
            working-directory: ${{ env.working-directory }}
    
          - name: Terraform plan
            run: terraform plan -out tfplan.binary
            working-directory: ${{ env.working-directory }}
    
          - name: Terraform show
            run: terraform show -json tfplan.binary > plan.json
            working-directory: ${{ env.working-directory }}
    
          # Install the Infracost CLI, see https://github.com/infracost/actions/tree/master/setup
          # for other inputs such as version, and pricing-api-endpoint (for self-hosted users).
          - name: Setup Infracost
            uses: infracost/actions/setup@v1
            with:
              api-key: ${{ secrets.INFRACOST_API_KEY }}
    
          # Generate Infracost JSON output, the following docs might be useful:
          # Multi-project/workspaces: https://www.infracost.io/docs/multi_project/config_file
          # Combine Infracost JSON files: https://www.infracost.io/docs/multi_project/report
          - name: Generate Infracost JSON
            run: infracost breakdown --path plan.json --format json --out-file /tmp/infracost.json
            working-directory: ${{ env.working-directory }}
            # Env vars can be set using the usual GitHub Actions syntax
            # env:
            #   MY_ENV: ${{ secrets.MY_ENV }}
    
          # See https://github.com/infracost/actions/tree/master/comment
          # for other inputs such as target-type.
          - name: Post Infracost comment
            uses: infracost/actions/comment@v1
            with:
              path: /tmp/infracost.json
              # Choose the commenting behavior, 'update' is a good default:
              behavior: update # Create a single comment and update it. The "quietest" option.                 
              # behavior: delete-and-new # Delete previous comments and create a new one.
              # behavior: hide-and-new # Minimize previous comments and create a new one.
              # behavior: new # Create a new cost estimate comment on every push.
  5. πŸŽ‰ 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.

Examples

The examples directory demonstrates how these actions can be used in different workflows, including:

Actions

We recommend you use the above quick start guide and examples, which combine the following individual actions:

  • setup: downloads and installs the Infracost CLI in your GitHub Actions workflow.
  • comment: adds comments to pull requests.
  • get-comment: reads a comment from a pull request.

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

About

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

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 54.7%
  • TypeScript 42.6%
  • Shell 2.7%