Introduction Automating infrastructure provisioning with Terraform is nothing for many, but to truly harness IaC power, seamless integration with ... Read More
Terraform Live Cost Estimate using Infracost Free Plugin
The cloud offers unlimited scalability at lower costs with the available resources when companies enable their engineering teams to launch infrastructure as and when needed. However, since costs aren’t shown to engineers before Cloud resources are deployed, the real cost implications are unknown. That causes higher than expected costs and ballooning budgets over time, which is a nightmare for CFOs (Chief Financial Officers) considering Gartner estimated Cloud wasted spend exceeding $26.6 billion in 2021. Organizations blaming the Development Teams or business units after the damage is done is pointless. What then, should they nuke everything after $$$ overspend? which one to kill first?
Thus, the greatest challenge for every organization remains:
“How to proactively Reduce Cloud Wasted Spend?”
Today, we’ll explore an awesome solution that helps DevOps, SREs,& Devs continuously reduce their cloud costs.
This tool is called Infracost!
Table of contents
What is Infracost?
Infracost is a super cool tool that lives calculates the cost of your Terraform resources on Amazon Web Services (AWS), Google Cloud Platform (GCP), or MicrosoftAzure before you even hit deploy. All it needs is your terraform files and its CLI installed.
Infracost CLI fetches the prices from its Cloud Pricing API, which stores/updates the prices from AWS/Azure/GCP pricing APIs. Their GraphQL-based API gleans up to 3 million prices to return the cost.
VS Code extension: by far their slickest feature, where cost is displayed inside your TF code (See below)
Modules: Infracost can even scan modules within your project and pull all the pricing details in seconds.
How does Infracost work?
CLI Extracts cost-related parameters from parsing terraform files such as the instance type or disk size.
Retrieves prices from the Cloud Pricing API which returns the prices.
Calculates the monthly costs and outputs them in the table, JSON, or other formats.
No need to scroll long pricing pages, just pick a resource value in your *.tf file,& Infracost will get its cost for you.
Syntax (basic): the format and out-file arguments are optional
infracost breakdown --path myterraDir --format json --out-file infracost-base.json
Terraform variables can be set using –terraform-var-file or –terraform-var
Windows: Download and unzip the latest release
Note: In order to use the VS Code extension, you need to move the exe. file into the folder that’s in your
I use pathed to add its folder in my system %PATH% environment variable permanently in a terminal
C:\> PathEd.exe add "C:\InfraCost_path\"
Linux/macOS: The easiest way is to use the install script (in one line):
# Downloads the CLI based on your OS/arch and puts it in /usr/local/bin curl -fsSL https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh| sh
II. Setup and Authentication
Register for a free API key, used by the CLI to authenticate/retrieve prices from our Cloud Pricing API
C:\> infracost auth login We're redirecting you to our log in page, please complete that,and return here to continue using Infracost. Waiting...
On the login page, you have two options sign up via email or GitHub
- After this step, your API key will be stored in credentials.yml file and Infracost will be all set
# The API key was saved to C:\Users\brokedba\.config\infracost/credentials.yml Your account has been authenticated.
- You are now ready to use Infracost by navigating to any terraform project and running the CLI command
# check Authentication
C:\>infracost configure get api_ke y ico-7XXXXXXXXXXXXXXXXXXXXX
C:\>notepadC:\Users\brokedba\.config\infracost/credentials.yml version: "0.1" api_key: ico-7XXXXXXXXXXXXXXXXXXXXX pricing_api_endpoint: https://pricing.api.infracost.io
III. Use Infracost
Let’s see if we can use a simple terraform stack from my GitHub repo to check the cost of a VM compute
- Clone the repository containing 3 of my terraform stacks (Azure/AWS/GCP) in your machine
$ git clone https://github.com/brokedba/terraform-examples.git
1. Microsoft Azure Stack
- First, navigate to my azure launch instance subdirectory (see blog)
$ cd /terraform-examples/
- Here’s the content:
$ tree . |-- cloud-init ---> Subfolder containing cloudinit |-- compute.tf ---> Instance related terraform configuration |-- outputs.tf ---> displays the resources detail at the end of the deploy |-- variables.tf ---> Resource variables needed for the deploy |-- vnet.tf ---> VNet used by the compute
- Run Infracost
- I will use one more option to include services that don’t incur costs using –show-skipped
launch-instance/$ infracost breakdown –path . --show-skipped
Seems that our cheap static website will cost me USD$12/month
Of the 9 resources, 2 have a fixed pricing (VM Compute and Public IP) and one(os_disk) based on usage
7 are free which I wanted to display using –the show-skipped option
We will talk later about usage-based estimates
2. Amazon Web Services (AWS)
- Same stack here but different provider (see blog)
$ cd /terraform-examples/
terraform-provider-aws/launch-instance/$ launch-instance/$ infracost breakdown –path . --show-skipped
USD$10/month for a t2.micro compute. Notice there is no fee for the public IP because it’s FREE in AWS.
3. Google Cloud Platform (GCP)
- Last supported cloud provider (see blog)
$ cd /terraform-examples/
terraform-provider-gcp/launch-instance/$ launch-instance/$ infracost breakdown –path . --show-skipped
USD$6.51/m for the same simple web instance stack. Again, no fees for the public IP or usage-based storage. Although GCP usually charges 0.004/hour for Static IPs.
What if I want to show the cost in CAD$ or Euro? Well, all you need is to run the below configure command.
# List your preferred ISO 4217 currency and run the configure command $ Infracost configure set currency EUR
Check if your stack estimate currency has changed (you might need to restart Visual Code to see GUI change)
Add Infracost to your CI/CD pipeline, to see cost estimates in pull requests before making changes. It supports GitHub actions, GitLab CI, Circle CI, bitbucket, Jenkins & more
Infracost distinguishes the price of a resource from its cost
Price is a per-unit value published by cloud vendors (fixed)
Cost is equal to (resource’s price) x (its usage), see below AWS Lambda example
Name Quantity Unit Monthly Cost aws_lambda_function.hi ├─ Requests Cost depends on usage: $0.20 per 1M requests └─ Duration Cost depends on usage: $0.0000166667 per GB-seconds PROJECT TOTAL $0.00
There are two options for showing costs instead of prices:
Fetch usage from CloudWatch/cloud APIs: narrow usage-based costs (AWS only).
Specify usage manually:
Use –the sync-usage-file option to generate a new usage file
Edit the file with your usage estimates & run the below command (more details here)
$ infracost breakdown –path . --usage-file infracost-usage.yml
SaaS version which allows team lead visibility across all changes(i.e most expensive pull requests)
I had a free trial upon signing up & all my cost estimates were actually sent to my cloud account
Security and Privacy
Rest assured Infracost doesn’t touch secrets and only parses what pricing API needs to return the cost.
There you have it, I promised I’ll blog about this amazing solution & it’s been very easy and fun to explore
This was longer than expected lol, but I hope it was enough information for you to dive into Infracost
Bare I mind that I couldn’t list all the features which would require web scraping their whole website 😉
Infracost diff shows a diff of monthly costs between current and planned state (using usage.yml file)
Among Future features, we might have
Supporting Oracle Cloud: please upvote the issue-911 on GitHub
Point to state file and get the cost in a machine that doesn’t even have terraform installed
Hiding and Sorting resources by cost
Integrate Infracost with remote backend-based tfsates (S3)
More from this Author
Introduction Since the very beginning, everyone was introduced to Cloud Services through the console as it’s very quick. But the cloud CLI tooling ... Read More