"How to use Infracost to save your Money on Cloud Infrastructure"

Tushar KumarTushar Kumar
3 min read

Introduction:

In the world of cloud infrastructure management, cost optimization is a crucial aspect for businesses of all sizes. Overspending on cloud resources can significantly impact a company's budget. That's where Infracost comes into play. Infracost is a powerful open-source tool that allows you to estimate and manage infrastructure costs effortlessly.

In this blog post, we will delve into Infracost, covering its installation on Windows using Chocolatey, acquiring API keys, local configuration, and integrating it into your pipeline. So let's get started!

Also if you want to use the code shared in the blog, use the GitHub repo which I have shared in the end.

Installing Infracost with Chocolatey on Windows Machine:

To begin utilizing Infracost on your Windows machine, you can leverage Chocolatey, a package manager for Windows. Here's a step-by-step guide to installing Infracost using Chocolatey:

If you don’t have a Chocolatey on your Windows machine follow these steps:

Installing Chocolatey on a Windows machine. Here's a simple step-by-step guide:

Now once Chocolatey is successfully installed on your Windows machine. Open your command prompt or PowerShell as an administrator.

To check the version of your Infracost type - “Infracost –version”

Acquiring API Keys and Local Configuration:

Before diving into cost estimation, you'll need to acquire API keys for Infracost. Here's a guide to obtaining and configuring your API keys:

  • Visit the Infracost website and create an account if you haven't already.

  • After logging in, navigate to your org account settings and locate the API keys section.

  • Generate a new API key.

That’s it, it is that simple.

Running Infracost Locally on your Machine

Once you have Infracost installed and configured, you can run it locally to get a cost estimate for your Terraform code.

To do this, you can use the following command where your terraform code is there:

Here I am using the path to my terraform code.

This will generate a cost estimate in JSON format. You can then open the JSON file in a text editor to view the cost breakdown.

A similar result will appear if you follow the above-given commands correctly.

Integrating Infracost into Your Github Pipeline:

If you want to incorporate Infracost into your pipeline for continuous cost monitoring, you can use a tool like GitHub Actions.

It is not compulsory to perform this part, you can run the Infracost in your locally also but if you want to perform continuous deployment and see the estimated cost of your cloud infrastructure you can integrate Infracost into your pipeline in this example it is GitHub action however you can do it on any CI/CD tool of your preference.

Don’t worry If you don’t know anything about GitHub Actions, I shared a blog earlier on that only it is very simple.

Check this out:

https://infrasityblog.hashnode.dev/automate-cloud-infrastructure-with-github-actions-and-terraform-a-step-by-step-guide

After reading this I hope everything is very clear to you. Now we can move forward.

After pushing the code to Github you will see that the pipeline will automatically run and check all the parameters written in the script.

For your help, I have pasted the specific result of Infracost which got printed while the pipeline worked successfully.

That’s it you have now learned to integrate Infracost in your pipeline, check out the pipeline and see how it works in an actual environment

Conclusion:

Infracost is a game-changer when it comes to managing infrastructure costs efficiently. By following the steps outlined in this blog post, you can easily install Infracost on your Windows machine, acquire the necessary API keys, configure them locally, and incorporate Infracost into your pipeline. With Infracost, you'll have a comprehensive understanding of your infrastructure expenses, enabling you to make informed decisions and optimize costs effectively.

0
Subscribe to my newsletter

Read articles from Tushar Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tushar Kumar
Tushar Kumar

Tushar | DevOps Engineer 👨‍💻 | Theatre Enthusiast 🎭 Passionate DevOps Engineer skilled in CI/CD, infrastructure-as-code, and cloud solutions. Experience with Docker, Kubernetes, AWS, Git, linux and Terraform. Committed to optimizing workflows and driving innovation. A theatre enthusiast with strong communication skills gained through active participation in college theatre society.