Managing Cloud Expenses with Infracost
Learn how to manage the cloud costs of the Cloud Infrastructure
Last updated
Learn how to manage the cloud costs of the Cloud Infrastructure
Last updated
When we integrate cloud services with Infrastructure as Code (IaC) tools like Terraform and continuous deployment, we gain the nearly magical ability to create resources on demand. Despite its many benefits, the cloud presents challenges, such as accurately estimating costs. Can this tedious task be automated?
Yes, it can, thanks to the open-source tool .
Ever tried to navigate the cost structures of cloud providers?
It’s like trying to solve a constantly changing puzzle. Take , for example. Did you know they offer over 700 types of Linux machines on EC2? Many of them have similar names and features. Consider “m6g.2xlarge” and “m6gd.2xlarge”: one includes an SSD, while the other does not. A simple mistake in your Terraform file can cause your bill to skyrocket unexpectedly. Sounds familiar?
You have the option to use the Infracost pricing API for free, or you can host it yourself. If you opt for the paid tier, you'll also gain access to a cloud dashboard that helps you track cost changes over time.
Using this extension, the estimates for the cost are seen right in the VS Code console.
This tool does not come with multiple commands to be executes, just a simple single line command run brings the tool to your environment.
To use Infracost, you will require an API Key which can be easily retrieved using the below command. Just execute on the shell or CLI.
And also have some Terraform configuration files ready to be analyze.
Ready to get started? Let's dive in with the infracost breakdown
command. This command analyzes your Terraform plans and provides a cost estimate. Make sure the --path
variable points to the folder containing your Terraform files.
Let us consider, you want to provision an "a1.medium" EC2 instance, here's what you need to do:
When we run infracost breakdown --path .
, we receive a cost estimate for the specified resources in our Terraform configuration.
If we add an additional 600GB of EBS storage, the cost increases to $89, as shown below:
Infracost can calculate costs for usage-based resources like AWS Lambda. Let's explore what happens when we replace the EC2 instance with serverless functions, but running infracost breakdown
yields a total cost of 0 dollars in this case. As there are no requests coming to the Lambda function, not incurring and cost for the usage if this Lambda function.
We can now provide estimates by editing usage.yml
. Here's an example where we estimate 5 million requests with an average runtime of 300 milliseconds:
Now we are required to use this usage.yml file to understand the right estimates for our requirements. The below command helps us achieve this.
Now if you compare the results with an empty run for Lambda, the difference is evident.
Much better!
Just remember, the accuracy hinges on the precision of our usage file. If you're not entirely sure, why not spice things up by integrating Infracost with your cloud provider? That way, you can fetch utilization metrics straight from the source for more dependable estimates!
Now, we can compare changes using infracost diff
. Let's observe what happens if the Lambda execution time increases from 300 to 350 milliseconds.
Infracost is an project designed to help us understand our cloud spending. It provides a detailed breakdown of our infrastructure costs and calculates the financial impact of any changes. In essence, Infracost acts as a "git diff" for cloud billing.
Infracost comes in two flavors: a handy and a versatile command-line program. Curious about what they do? Both versions parse your Terraform or Terragrunt files, fetch the latest price points from the Infracost cloud pricing API, and give you an estimate of your costs. Whether you prefer a graphical interface or the command line, Infracost has you covered!
So let us begin with the of Infracost on a linux machine:
To understand costing of a function, we can provide usage estimates for our Lambda function using a usage file. Here's the command to create and configure the usage file.
To estimate the impact of changes, allows us to save results in JSON format using the --format json
and --out-file
options. This provides a baseline file that we can include in our source control:
The ability to provision resources instantly is a double-edged sword: a simple typo in a Terraform file can lead to expensive mistakes. If you're already automating deployments and managing services with Terraform, integrating into your workflow can provide valuable insights and prevent unexpected costs. Setting it up is quick and easy, taking just a few minutes, yet it can potentially save thousands of dollars in the long run.
Article by -
Credits - Infracost: Spacelift: