Cost Estimate by Infracost using Terraform

Cloud providers play a crucial role in the modern technology landscape and offer numerous benefits to businesses and individuals. Cloud solutions refer to the various services and technologies provided by cloud computing providers to enable businesses and individuals to leverage the benefits of cloud computing, making it easier to scale up the infrastructure as per our needs. There are multiple tools that support infrastructure creation in cloud environments and Terraform is one of the most popular IaC tools.

I wish to share my research in this blog and the benefits of the Infracost tool I’ve used. This blog seeks to provide you with a thorough knowledge of the Infracost Tool and its significance, regardless of whether you’re a tech enthusiast, a business owner looking for new prospects, or simply curious about the technologies influencing our digital landscape.

What is Infracost?

Before any resources are launched, Infracost, an open-source tool, looks for Terraform code modifications and verifies over 3 million prices to get a clear, understandable cost estimate.

DevOps and cloud engineers may estimate the cost of deploying cloud-based infrastructure using Terraform with the aid of the Infracost tool. Before making changes, DevOps and cloud engineers can view a cost breakdown and comprehend charges using Infracost, either in the terminal or through pull requests.

Comparison of Infracost calculators and cloud native calculators 

Feature Cloud Native Cost Calculator Infracost
Generates cost estimates Manually Automatically
Works with infra-as-code No Yes
Integrations None Supports below tools.

  • GitHub, GitLab, Azure Repos, Bitbucket
  • VSCode
  • CI/CD tools
  • Jira
  • Terraform
Usage profile design Yes, one time Yes, reusable
Clouds supported Supports only calculator specific to cloud provider AWS, Azure, Google Cloud
Cost Free Free, Open Source and Paid offerings

 

Are you prepared to save time and resources and ready to Eestimate your Cloud Costs? Then we are there to help you.

Contact us

Advantages of using Infracost

• Manual effort was reduced by 80%, and manual errors were avoided.
• Faster delivery of cost estimates as Infrastructure as Code is written.
• The cost estimation and forecasting of the project are automated.

Prerequisites:

• Terraform is installed on your server.
• Terraform project with scripts consisting of cloud resources or modules.

Installation of Infracost in Linux:

You can use the below URL to download the Infracost package, and the prebuild shell script will automatically install infracost on your Linux machine.

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

Use the below command to confirm the Infracost installation.

infracost –version

Generate an API key for Infracost authentication.  

To generate an API key, use the below commands: This key helps Infracost fetch all the pricing details of resources that are about to be created using the Terraform script.

Infracost register

Registration with Infracost for an API key can be done on the official website of Infracost. You can follow the steps below to complete the web authentication process.

Web-based Authentication

  1. 1. We can register or log in using the Infracost Cloud.
  2. 2. Decide on the ideal organisation. For personal use, each Infracost user has a default organisation. Utilising the organisation option at the top of the screen, you should create a new organisation for your business.

  1. 1. Under the Organisation Settings page, you can copy your API key.
  2. 2. Set the CI/CD, INFRACOST_API_KEY environment variable.
  3. 3. To use it in the CLI, run infracost configure set api_key MY_API_KEY.

In the terminal, you can see the path of the API key, which is stored using the below command.

cat  ~/.config/infracost/credentials.yml

Estimate the infrastructure cost using Infracost

Below are a few pre-required steps.

  • Run the below command to initiate Terraform in your project directory.

terraform init

  1. 1. Use the optional -out=FILE option to save the generated plan to a file on disc. The plan command to generate a tfplan.binary does the job.

terraform plan -out tfplan.binary

  1. 2. We need to generate a JSON file using the terraform command, 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 be further used by Infracost to examine the cost of the setup. It will generate full details of the estimation for your terraform resources.

Breakdown of costs for the cloud resources from the JSON file

The below command is used to estimate the full breakdown of the cost for the terraform plan saved as a JSON file.

infracost breakdown –path plan.json

The above image shows how Infracost displays the cost breakdown for the terraform plan that we did. We can change the terraform code and resources to reduce the cost based on need and cost optimisation.

Cost comparison using diff for your terraform plans

The diff command is used to compare costs. The diff helps us compare and display the cost difference between two different plans.

Once you have changes in the resources related to the environment, you can re-run the terraform plan command and generate a new plan file and export it to JSON format as done earlier.

Once the new plan file is generated, use the below command to compare the cost changes between the old plan and the new plan.

infracost diff –path .

Cost Estimation as report

Infracost helps us generate reports from the JSON file. The report will be created in HTML form, which helps us share the cost estimate with other team members or the client.

Use the below command to generate the report.

infracost breakdown –path . –format html > report.html

Comparison of Infracost with other Tools – 

While Infracost is a popular tool for cloud cost estimation and optimization, there are other tools available in the market that offer similar capabilities. I have listed a few examples of other cost comparison tools:

CloudHealth by VMware: CloudHealth provides cost management and optimization solutions for multi-cloud environments. It offers features such as cost visibility, budgeting, and resource optimization across different cloud providers. CloudHealth also provides recommendations to optimize cloud spending based on usage patterns and offers robust reporting and analytics.

Cloudability: Cloudability is a cloud cost management platform that helps organizations optimize their cloud spending. It provides insights into cloud costs, usage, and resource allocation across different cloud providers. Cloudability offers features such as budgeting, cost allocation, and showback/chargeback capabilities.

AWS Cost Explorer: AWS Cost Explorer is a built-in tool within the Amazon Web Services (AWS) console. It provides cost visibility and analysis for AWS services. It offers features such as cost breakdowns, forecasting, and cost anomaly detection. While it is specific to AWS, it provides detailed insights into AWS costs and can be useful for organizations primarily using AWS.

Google Cloud Pricing Calculator: The Google Cloud Pricing Calculator is a tool provided by Google Cloud Platform (GCP) to estimate costs for GCP services. It allows users to configure their cloud resources and provides real-time cost estimates based on the selected configurations. The tool enables users to compare costs across different regions, machine types, and usage patterns.

Azure Cost Management and Billing: Azure Cost Management and Billing is a native tool in the Microsoft Azure portal that helps manage and optimize Azure costs. It provides cost visibility, budgeting, and reporting capabilities. It also offers features like cost alerts, cost forecasts, and recommendations for optimizing Azure resource spending.

Each tool may have its own unique features, integrations, and pricing structures. It’s important to evaluate your specific requirements and compare the features and costs of different tools to determine which one best aligns with your needs and cloud infrastructure environment.

Ready to Get a Clearer Picture of Your Costs? Try Infracost with Terraform now to gain insights into accurate cost estimation, optimization tips, and financial control.

Click here

Conclusion

In the above blog, we discussed how to use Infracost to calculate and compare the cost of the infrastructure that we are building on cloud platforms using Terraform. By providing cost visibility, optimisation recommendations, and cost monitoring capabilities, it helps drive efficiency, cost savings, and financial control in cloud deployments. This helps the engineers have a quick estimate and cost comparison for the resources that we are planning to deploy, eventually saving a lot of manual work.