Estimating costs can be a nightmare for many enterprises. Cloud cost estimation is important for organizations to plan their budget and forecast expenses accurately. It is essential to monitor and analyze cloud usage regularly to optimize cloud spending and avoid unexpected expenses.
This process is very time-consuming, so there was a need for change. With Terraform, you can easily estimate cloud costs by leveraging Infracost, and you can easily compare potential bills between different vendors.
Working with cloud providers and DevOps is all about speed, efficiency, and cost management.
Infracost is a tool that is used to figure out how much the cloud resources will cost.
What is Infracost?
Infracost is a super cool tool that lets you calculate the cost of your Terraform resources on AWS, GCP, or Microsoft Azure before you even hit deploy. This enables you to see cloud cost estimates for Terraform in pull requests.
- Infracost can even scan modules within your project and pull all the pricing details in seconds.
- Public modules are automatically downloaded, but you need to setup access for private modules so Infracost can process them.
If you store your private modules in an S3 bucket, you need to provide readonly AWS IAM credentials, so the CLI can download them and estimate their costs.
Running Infracost in module repos
You can run infracost breakdown –path modules/my-module to get a cost estimate for an individual module. Module variables can be passed in using the –terraform-var-file / terraform-var flags.
version: 0.10.22 projects: - path: my-s3-module terraform_var_files: - my-s3.tfvars
Use Cases of Infracost
Cost change understanding: This helps you better understand when & where cost changes were introduced, as well as upcoming changes.
Cost Awareness: Infracost Increased cost awareness and shown the estimated cost of infrastructure upfront, before any resources are launched or changed.
Aligned budgets and costs: If the changes to infrastructure or the launch of a new product break budgets, communication can happen in advance, not after the bill arrives.
As Infrastructure as Code is written, the cost estimation and forecasting of the project is automated and delivered with no overhead. As changes are proposed, cost estimates are kept up to date and live.
Installing Infracost and Registering for API key
1. Install Infracost
The easiest way is to use our install script, This will install the binary for infracost and will also add the same to path (/usr/local/bin/infracost).
curl -fsSL https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh | sh
Check that it works correctly
2. Get API Key
Register for a free API key, used by the CLI to authenticate/retrieve prices from our Cloud Pricing API. Run infracost auth login to get a free API key.
infracost auth login
- After this step, your API key will be stored in credentials.yml file and Infracost will be all set.
- To be able to run Infracost locally or in CI you will need to save the API key in an environment variable
- Infracost also has an optional Infracost Cloud, which comes with a paid tier and includes features like Jira integration, custom price books, and a dashboard to keep track of costs over time.
3. Output showing cost addition
Below screenshot shows infra cost that will be added to the AWS bill because new infra being created.
Generate a report of all the estimations.
You can also save this output in a json/Html/table file by running.
infracost breakdown --path . --format json --out-file infracost-base.json
infracost breakdown --path . --format table --out-file infracost-base.txt
The best part of infracost is that at last, it helps to generate reports. The report is in html form. Let’s generate the report now.
Use the below command to generate the report.
infracost breakdown --path . --format html > report.html
The below image shows the file.
So in the above blog, I discussed how to generate AWS cost estimation by using infracost tool and generated output in different forms. Infracost helps organizations and individuals estimate and manage the cost of their cloud infrastructure.
Overall, Infracost is a valuable tool It provides a simple and effective way to estimate and manage costs, identify cost-saving opportunities, and make informed decisions about infrastructure changes.
Blog Pundits: Bhupender Rawat and Sandeep Rawat
OpsTree is an End-to-End DevOps Solution Provider.
Connect with Us