Git Product home page Git Product logo

infracost-estimate-cloud-costs's Introduction

infracost-estimate-cloud-costs

🚀 Cloud cost estimates for Terraform in pull requests💰📉 Love your cloud bill! 🚀

https://github.com/coding-to-music/infracost-estimate-cloud-costs

From / By InfraCost https://github.com/infracost/infracost

https://github.com/infracost/infracost

https://www.infracost.io/

Environment variables:

GitHub

git init
git add .
git remote remove origin
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:coding-to-music/infracost-estimate-cloud-costs.git
git push -u origin main

Infracost breakdown command

Infracost shows cloud cost estimates for Terraform. It lets DevOps, SRE and engineers see a cost breakdown and understand costs before making changes, either in the terminal or pull requests.

Docs Docker pulls Community Slack channel tweet

Get started

Follow our quick start guide to get started 🚀

Infracost also has many CI/CD integrations so you can easily post cost estimates in pull requests. This provides your team with a safety net as people can discuss costs as part of the workflow.

Post cost estimates in pull requests

Infracost in GitHub Actions

Output of infracost breakdown

Infracost breakdown command

infracost diff shows diff of monthly costs between current and planned state

Infracost diff command

Supported clouds and resources

Infracost supports over 230 Terraform resources across AWS, Azure and Google. Other IaC tools, such as Pulumi, AWS CloudFormation/CDK and Azure ARM/Bicep are on our roadmap.

Infracost can also estimate usage-based resources such as AWS S3 or Lambda!

Community and contributing

Join our community Slack channel to learn more about cost estimation, Infracost, and to connect with other users and contributors. Checkout the pinned issues for our next community call or our YouTube for previous calls.

We ❤️ contributions big or small. For development details, see the contributing guide. For major changes, including CLI interface changes, please open an issue first to discuss what you would like to change.

Thanks to all the people who have contributed, including bug reports, code, feedback and suggestions!

License

Apache License 2.0

Get started

https://raw.githubusercontent.com/infracost/docs/master/docs/getting_started.md

Get started with Infracost in your Terraform workflow, integrate it into your CI pipeline and view cost estimates for your AWS/Azure/Google infrastructure.

Infracost shows cloud cost estimates for Terraform. It lets DevOps, SRE and engineers see a cost breakdown and understand costs before making changes, either in the terminal or pull requests. This provides your team with a safety net as people can discuss costs as part of the workflow.

If you're upgrading from an older version, see the v0.10 migration guide.

1. Install Infracost

macOS / Linux manual

The easiest way is to use our install script:

Downloads the CLI based on your OS/arch and puts it in /usr/local/bin

curl -fsSL https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh | sh

Or you can install it manually:

  • Download the archive for your platform from our releases.
  • Unarchive and copy it to one of the directories in your $PATH, e.g. /usr/local/bin:
tar xzf infracost-linux-amd64.tar.gz -C /tmp
mv /tmp/infracost-linux-amd64 /usr/local/bin/infracost

Check that it works correctly:

infracost --version # Should show 0.10.10

Get the latest Infracost release using docker:

docker pull infracost/infracost:ci-latest

docker run --rm \
  -e INFRACOST_API_KEY=see_following_step_on_how_to_get_this \
  -v $PWD/:/code/ infracost/infracost:ci-latest breakdown --path /code/
  # Add other required flags/envs, e.g. --terraform-var-file or --terraform-var

docker run --rm \
  -e INFRACOST_API_KEY=$INFRACOST_API_KEY \
  -v $PWD/:/code/ infracost/infracost:ci-latest breakdown --path /code/

Output


2. Get API key

Register for a free API key, which is used by the CLI to retrieve prices from our Cloud Pricing API, e.g. get prices for instance types.

note

  • No cloud credentials or secrets are sent to the API and you can also self-host it.
  • Infracost does not make any changes to your Terraform state or cloud resources.
infracost auth login

# Or

infracost configure set api_key MY_API_KEY

The key can be retrieved with infracost configure get api_key.

Generating plan JSON files​

By default, the Infracost CLI parses Terraform HCL code to estimate costs. If that does not work for your use-case, or you already have a Terraform plan JSON file, Infracost can also parse that. See the relevant Terraform CLI, Terraform Cloud or Terragrunt sections below on how to generate plans. These bash scripts can be modified and used in your CI/CD pipelines.

Terraform CLI​ The Terraform CLI can be used to produce a plan JSON file as shown below:

Single project​

cd path/to/code

terraform init
### Customize this to how you run Terraform
### e.g. if you are using variables you can pass them with -var or -var-file

terraform plan -out tfplan.binary
terraform show -json tfplan.binary > plan.json

infracost breakdown --path plan.json

3. Show cost estimate breakdown

Infracost parses the project locally to determine resource types and quantities needed to calculate costs. The --path flag can point to a Terraform directory or plan JSON file.

# You can also: git clone https://github.com/infracost/example-terraform
cd my-terraform-project
# Terraform variables can be set using --terraform-var-file or --terraform-var
infracost breakdown --path .

Example output: Infracost breakdown command

:::tip Infracost can also estimate usage-based resources such as AWS S3 or Lambda :::


4. Show cost estimate diff

  1. Generate an Infracost JSON file as the baseline:
  2. infracost breakdown --path . --format json --out-file infracost-base.json
  3. Edit your Terraform project. If you're using our example project, try changing the instance type:
  4. vim main.tf
  5. Generate a diff by comparing the latest code change with the baseline:
  6. infracost diff --path . --compare-to infracost-base.json

Example output: Infracost diff command


5. Monitor cost estimates

  1. The following environment variable instructs the CLI to send its JSON output to Infracost Cloud. This is our SaaS product that builds on top of Infracost open source and works with CI/CD integrations (next step). It enables team leads, managers and FinOps practitioners to see all cost estimates from a central place so they can help guide the team.
  2. INFRACOST_ENABLE_CLOUD=true infracost diff \
        --path . --compare-to infracost-base.json
  3. Log in to Infracost Cloud > Projects page to see the cost estimate.

Infracost Cloud dashboard showing pull request costs over the last 30 days

6. Add to your CI/CD

Use our CI/CD integrations to add cost estimates to pull requests, it only takes 15 minutes. This provides your team with a safety net as people can understand cloud costs upfront, and discuss them as part of your workflow.

If you run into any issues, please join our community Slack channel, we'll help you very quickly 😄

Infracost pull request comment

Add to CI/CD

Use one of our many integrations so DevOps, SRE and engineers see a cost estimate in pull requests before making changes. This provides your team with a safety net as people can understand cloud costs upfront, and discuss them as part of your workflow.

Infracost can work with either a Terraform directory or Terraform plan JSON.

Screenshot of Infracost running in GitHub pull requests:

<img src={useBaseUrl("img/actions-pull-request.png")} alt="Infracost pull request comment" />

Other CI/CD systems

Infracost can be used in any CI/CD system using the following steps. Our GitLab CI integration follows these steps and has examples that you might find useful.

  1. Use one of the following options to add the Infracost CLI into your CI/CD:

    We maintain specific CI Docker images:

    infracost/infracost:ci-0.10 - Recommended, use latest 0.10.x version to pick up bug fixes and new resource costs
    infracost/infracost:ci-0.10.x - Lock the version, see versions in https://github.com/infracost/infracost/releases
    infracost/infracost:ci-latest - Use latest Infracost image, might break when new minor/major versions are released
    

    Use curl -O -L https://infracost.io/downloads/v0.10/infracost-linux-amd64.tar.gz to download the latest 0.10.x version to pick up bug fixes and new resource costs. You can also use lock the version using our releases, or use latest, which might break when new minor/major versions are released.

  2. If your infra code is in GitHub, GitLab and Azure Repos or Bitbucket, run the breakdown, diff and comment commands to generate a cost estimate and post a comment directly.

Otherwise, run the breakdown, diff and output commands to save the comment markdown (e.g. using --format github-comment) and post it from your CI/CD system using curl or other command line tools.

You might also find the environment variables page useful.

If you run into any issues, please join our community Slack channel, we'll help you very quickly 😄

Third-party integrations

Infracost can also be used alongside many other third-party systems including Terraform Cloud/Enterprise, Env0, Scalr and Spacelift.

infracost-estimate-cloud-costs's People

Contributors

actions-user avatar ajoulie avatar alikhajeh1 avatar aliscott avatar anton-yurchenko avatar chenrui333 avatar coding-to-music avatar dependabot[bot] avatar ekifel avatar fedeci avatar golgeek avatar hugorut avatar kevinkengne avatar krishnaindani avatar m-yosefpor avatar neverovds avatar nissessenap avatar patmyron avatar pauluson avatar peterdavehello avatar praveenghuge avatar robh007 avatar sashman avatar serverabuse avatar sinabakh avatar syphernl avatar theragus avatar tim775 avatar vdmgolub avatar xeger avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.