Master LLMs with our FREE course in collaboration with Activeloop & Intel Disruptor Initiative. Join now!

Publication

How To Make Cloud Cost Estimation With Terraform
Latest   Machine Learning

How To Make Cloud Cost Estimation With Terraform

Last Updated on July 20, 2023 by Editorial Team

Author(s): Guillaume Vincent

Originally published on Towards AI.

Automate cloud cost estimation in your pull requests

Photo by Executium on Unsplash

Cloud computing is great for growing your business quickly. You only pay for what you use. It’s a double-edged sword, you can save a lot or pay a lot. That’s why analyzing your bill every day is an important point. Cloud computing providers offer tools to analyze costs. It is a boring task but necessary.

In this article, we’ll see how to do it in a more fun way. As you know cloud infrastructure is often managed as code. The most commonly used tool is Terraform. It is able to track changes between the code and what exists.

Each change involves costs and we will see how to add this dimension to Terraform with Infracost. In the end, you will know how to measure the cost of a change and integrate it into your code review. The project taken as an example here is presented here

What Is Infracost?

“Infracost shows cloud cost estimates for infrastructure-as-code projects such as Terraform. It helps DevOps, SRE and developers to quickly see a cost breakdown and compare different options upfront.” https://github.com/infracost/infracost

Infracost is a simple executable. So it’s super simple to install and use in a CI/CD pipeline. Here we will use it with Terraform but in the project roadmap, it is planned to use it with other tools like Pulumi or CloudFormation.

Once downloaded, Infracost just needs to be registered, and off you go!

$ infracost register 
Please enter your name and email address to get an API key. See our FAQ (https://www.infracost.io/docs/faq) for more details.
Name: Guillaume Vincent
Email: [email protected]
Thank you Guillaume Vincent! Your API key is: xxxxxxxxxxxxxxxxxxxxxxxxx
Success: Your API key has been saved to /Users/gvincent/.config/infracost/credentials.yml You can now run infracost breakdown --path=... and point to your Terraform directory or JSON/plan file.

Estimating The Cost

The full breakdown of costs

Infracost can be executed to have the full monthly breakdown of costs :

$ infracost breakdown --path terraform_nlb_static_eips
Detected Terraform directory at terraform_nlb_static_eips
U+2714 Running terraform init
U+2714 Running terraform plan
U+2714 Running terraform show
U+2714 Calculating monthly cost estimate
Project: terraform_nlb_static_eips
Name Monthly Qty Unit Monthly Cost
aws_autoscaling_group.webserver
└─ aws_launch_configuration.webserver
├─ Instance usage (Linux/UNIX, on-demand, t2.micro) 0 hours $0.00
├─ EC2 detailed monitoring 0 metrics $0.00
└─ root_block_device
└─ Storage (general purpose SSD, gp2) 0 GB-months $0.00
aws_eip.nlb
└─ IP address (if unused) 730 hours $3.65
aws_lb.this
├─ Network load balancer 730 hours $16.42
└─ Load balancer capacity units Cost depends on usage: $0.006 per LCU-hours
PROJECT TOTAL $20.07

You can play modifying the code and re-run the previous command to have an updated cost estimation of the new version.

The diff monthly costs between the current and planned state

Terraform updates, deletes, or creates resources saves the infrastructure state in a file. When you deploy a new version, Terraform is able to detect what needs to be updated, removed, created following your changes. Infracost relies on this state file to show you the cost impact of the current change from the plan.

To illustrate to you that, I deployed the initial code version using terraform apply. Then I scaled out the current EC2 instance type from t2.micro to m4.large:

$ git --no-pager diff 
diff --git a/launch-configuration.tf b/launch-configuration.tf
index db22265..bcf90c7 100644
--- a/launch-configuration.tf
+++ b/launch-configuration.tf
@@ -1,7 +1,7 @@
resource "aws_launch_configuration" "webserver" {
name_prefix = "${local.name_prefix}_webserver"
image_id = data.aws_ami.ubuntu.image_id
- instance_type = "t2.micro"
+ instance_type = "m4.large"
security_groups = [
aws_security_group.public.id]
user_data = data.template_cloudinit_config.this.rendered

Then I launch the Infracost command with diff argument :

$ infracost diff --path . 
Detected Terraform directory at .
U+2714 Running terraform plan
U+2714 Running terraform show
U+2714 Calculating monthly cost estimate
Project: .
~ aws_autoscaling_group.webserver
+$64.53 ($11.37 -> $75.90)
~ aws_launch_configuration.webserver
- Instance usage (Linux/UNIX, on-demand, t2.micro)
-$8.47
+ Instance usage (Linux/UNIX, on-demand, m4.large)
+$73.00
Monthly cost change for .
Amount: +$64.53 ($27.79 -> $92.32)
Percent: +232%

The diff usage is perfect to be added in complement of a code review to justify and argue the cost of a change.

Refine cost estimation with usage file

You can feed Infracost with a usage file to refine the cost estimation :

terracost-usage-example.yml

This file is added to the command arguments :

$ infracost breakdown --path . --format html > report.html

Report

You can generate reports to share cost estimation in different formats (HTML, JSON, etc..) :

an HTML cost-estimation report

Adding Infracost To GitHub Action

You can integrate Infracost with a lot of CI/CD solutions. Here we’re going to focus on Github Action because it is easy to reuse existing actions from the marketplace. In the workflow, we’re going to use the following actions :

The configuration of the workflow is configured in the terraform git project in.github/workflows/infracost.yml:

.github/workflows/infracost.yml
Credentials needed by the GitHub action workflow stored in a vault

I committed the previous instance type change to a branch named dev and opened a pull request. This triggers the steps of the workflow :

Creation of the GitHub pull request
Execution of the GitHub action workflow steps

When the workflow executed ends, the Infracost output is visible inside the pull request :

The Infracost output in the pull request

Resources

Assign Static Ip to AWS Load Balancers

Configure network load balancer with Terraform

guillaume-vincent.medium.com

Cloud cost estimates for Terraform in pull requests U+007C Infracost

Infracost shows cloud cost estimates for Terraform projects. It integrates into pull requests and allows developers and…

www.infracost.io

infracost/infracost

Infracost shows cloud cost estimates for infrastructure-as-code projects such as Terraform. It helps DevOps, SRE and…

github.com

Features * GitHub Actions

You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…

github.com

infracost/infracost-gh-action

This GitHub Action runs Infracost against pull requests whenever Terraform files change. It automatically adds a pull…

github.com

aws-actions/configure-aws-credentials

Configure AWS credential and region environment variables for use in other GitHub Actions. The environment variables…

github.com

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI

Feedback ↓