How to do cost estimation of infra using Infracost

Reading Time: 5 minutes

Cloud computing has made it really easier to grow and scale up infrastructure. The infrastructure setup has made it really easier to work with microservices but what about the cost.

The elasticity and flexibility has made the life easier. But it comes at a cost which we cant ignore.Cloud infrastructure has a cost which shows at the end of usage.

Most of the time, infrastructure as code (IaC) is hired to preserve and facilitate the evolution of this underlying. Like another kind of language, it need to comply with the identical true practices and byskip the code assessment step with different contributors. As referred to before, cloud infrastructure has a cost, and invoicing isn’t always continually highlighted at this stage. Contributors need to confer with outside sources and make estimations themselves. It isn’t always automatic and can be bulky over time.

Infrastructure based on cost-driven :- INFRACOST

What is infracost ?

Infracost helps to estimate cost of cloud based deployment. It supports terraform as IAC projects.

Infracost helps to determine the cost of developing , building , constructing the infrastructure through terraform.

” It lets DevOps, SRE and engineers see a cost breakdown and understand costs before making changes, either in the terminal or pull requests. “

Which tool does infracost support ?

  • Terraform

Installation of Infracost using terracost.

The steps to install infracost are given below.

1- Firstly install the package using the below command.

curl -fsSL | sh

2- Secondly check the version :-

infracost --version


Register for generation of API key.

1- To generate API key follow the below steps.

Run the below command to authenticate the process.

infracost register
Registeration of infracost for API key

The terminal shows the path of api key which is stored in the path shown.


Using infracost for estimation of the infrastructure cost.


  • A terraform project for the current infrastructure setup.
  • Terraform installed.

Below are the few pre-required steps before

1 – Firstly run the below command.

terraform init

This will initialize the terraform repo with and further run the plan to generate a tfplan.binary.

terraform plan -out tfplan.binary

2 – Next we have to generate a json file which is passed in the infracost command for cost estimation.

terraform show -json tfplan.binary > plan.json

The above command will generate a json file which will further be used by infracost to examine the cost of the setup. It will generate the a full flow of the estimation.

Lets take a deep dive into it . Before moving forward complete the above steps and follow the next steps.

Examination of cost using infracost.

Now the below steps will guide as how to generate the cost.

Estimate full breakdown of costs

To estimate the full breakdown of the cost run the below command.

infracost breakdown --path plan.json

NOTE : The plain.json is generated in the same directory which is initialized as terraform directory.

Full breakdown of cost

The above image shows how infracost shows the cost breakdown. One can play around with it by modifying the code and re-running it. This helps to reduce the cost and deal with the complexity.

Estimation of cost comparison using diff

The diff command is the best command to compare cost.The diff usage is perfect to be added in complement of a code review to justify and argue the cost of a change. Whenever one changes the code as per requirement , the comparison can be done between the former and latter based on diff command.

The below command helps to achieve that .

infracost diff --path .

The output is shown below:-

The cost comparison using diff command.

Making cost estimation refined with usage file.

Usage file

A usage file is a yml file which helps to refine cost. It builds up the resources that needs to refined. It is file that contains all the declaration of resources that sets up the infra. The below code block shows the file.

 version: 0.1
    instances: 15 # Number of instances in the autoscaling group.
    operating_system: linux # Override the operating system of the instance,    
                       can be: linux, windows, suse, rhel.
    reserved_instance_type: standard # Offering class for Reserved Instances. 
    monthly_cpu_credit_hrs: 350 # Number of hours in the month where the instance is expected to burst.
    vcpu_count: 2 # Number of the vCPUs for the instance type.

    new_connections: 20000    # Number of newly established connections per second on average.
    active_connections: 30000 # Number of active connections per minute on average.

So the above file is the usage file. It is passed as below.

To pass the above file just follow the below command.

infracost breakdown --path . --usage-file=usageinfra.yml

Below is the output.

Cost estimation with usage file

Generate report of all the estimation.

The best part of infracost is that at last ithelps to generate report . The report is in html form.Lets 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 use infracost. Basically this is the best practise these days for planning infrastructure. The best part is that it gives the pre and post cost estimation of infra. So for setting up infrastructure this is a best practise .


To learn more follow the link –

Written by 

Rishivant is a enthusiastic devops learner at Knoldus. He believes in going 1% up everyday and showcases his learning in his work.

1 thought on “How to do cost estimation of infra using Infracost6 min read

Comments are closed.