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
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 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.
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 breakdown
Output of
infracost diff
shows diff of monthly costs between current and planned state
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
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 .
:::tip Infracost can also estimate usage-based resources such as AWS S3 or Lambda :::
4. Show cost estimate diff
- Generate an Infracost JSON file as the baseline:
- Edit your Terraform project. If you're using our example project, try changing the instance type:
- Generate a diff by comparing the latest code change with the baseline:
infracost breakdown --path . --format json --out-file infracost-base.json
vim main.tf
infracost diff --path . --compare-to infracost-base.json
5. Monitor cost estimates
- 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.
- Log in to Infracost Cloud > Projects page to see the cost estimate.
INFRACOST_ENABLE_CLOUD=true infracost diff \
--path . --compare-to infracost-base.json
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 😄
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.
-
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. -
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.