Logo

dev-resources.site

for different kinds of informations.

How to Estimate Cloud Costs with Terraform using Infracost

Published at
12/20/2024
Categories
devops
finops
terraform
aws
Author
cloudcuddler
Categories
4 categories in total
devops
open
finops
open
terraform
open
aws
open
Author
12 person written this
cloudcuddler
open
How to Estimate Cloud Costs with Terraform using Infracost

Managing, optimizing and estimating cloud costs has become critical to running efficient and cost-effective operations in today’s cloud-centric world. As organizations increasingly rely on Infrastructure as Code (IaC) tools like Terraform to manage their cloud infrastructure, the need for integrated cost estimation and financial operations (FinOps) practices is more important than ever. This article explores leveraging Infracost for accurate cloud cost estimates and adopting FinOps best practices to streamline your Terraform-managed infrastructure.

The Importance of Cloud Cost Estimates

Understanding the cost implications of your infrastructure decisions is fundamental to effective cloud management. Accurate cloud cost estimates enable teams to:

  1. Budget Accurately: Ensure that cloud expenses align with financial forecasts and budgets.
  2. Optimize Resources: Identify cost-saving opportunities by analyzing spending patterns and optimizing resource utilization.
  3. Promote Cost-Aware Culture: Embed a cost-conscious culture within development teams, ensuring cost considerations are part of the decision-making process.

How to use Infracost with Terraform?

Terraform is a widely used IaC tool that allows developers to define and provision cloud resources using declarative configuration files. By integrating Infracost, a tool designed for real-time cost estimation, into your Terraform workflows, you can gain valuable insights into the cost implications of your infrastructure changes before deployment.

Prerequisites

  • A machine (for this guide, I am using Amazon Linux )
  • Terraform CLI
  • An AWS account

STEP 1 – Install Infracost
Get the latest Infracost release. The simplest method is to use the installation script.

# 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
Enter fullscreen mode Exit fullscreen mode

infracost --version # Should show 0.10.37

Image description

STEP 2 – Get the API key
Please register for a free API key, which the CLI uses to fetch prices from our Cloud Pricing API, such as retrieving prices for different instance types.
infracost auth login

You will be redirected to the Infracost login page, where you must complete the login process. Once done, the key will be stored on your server. You can retrieve the key by running the following command:

infracost configure get api_key 
# output should be ico-X8i4bg0xD6SBV9GT1QqtseF51S4V8Z0P
Enter fullscreen mode Exit fullscreen mode

STEP 3 – Create a Terraform Configuration
Created a simple Terraform configuration file, which you can download from git [https://github.com/cloudcuddlers/infracost-terraform]

STEP 4 – Initialize Terraform and generate a plan
Run the command init and plan to generate a terraform plan for your infrastructure as code.

terraform init
terraform plan -out=tfplan.binary
Enter fullscreen mode Exit fullscreen mode

STEP 5 – Generate Infracost Cost Estimates
Run Infracost to generate a cost estimate for your Terraform configuration:

infracost breakdown --path=tfplan.binary --project-name=cloudcuddler
Enter fullscreen mode Exit fullscreen mode

Image description

Now if you want to compare the cost with the previous version and the current version then you need to save the output in JSON format with the mentioned command

infracost breakdown --path=tfplan.binary --project-name=cloudcuddler --out-file previous_version.json --format json
Enter fullscreen mode Exit fullscreen mode

Now you have added one more EC2 instance in your Terraform configuration file and again run the command

infracost diff --path=tfplan.binary --compare-to previous_version.json --project-name=cloudcuddler
Enter fullscreen mode Exit fullscreen mode

Image description

As you can see, by adding another EC2 instance, Infracost captures this change from Terraform, reflecting the resulting increase in monthly costs.

Conclusion

By integrating Infracost into your Terraform workflows, you can gain valuable insights into the cost implications of your infrastructure changes before deployment. This ensures that cost efficiency is considered at every development lifecycle stage. Implementing FinOps best practices with Infracost helps promote a culture of cost awareness and drives financial and operational excellence in your cloud journey.

finops Article's
30 articles in total
Favicon
Nueva región AWS en México: su implicación de los costos y FinOps
Favicon
Optimizaciones de costo claves de EC2 en AWS
Favicon
Create spot instances on GCP & AWS
Favicon
Sécuriser l'association entre un projet et son compte de facturation sur Google Cloud Platform
Favicon
Clean up S3 with Batch Operations, Tags and Lifecycle policies, they said. It will be cheaper, they said.
Favicon
Reducciones de costo, ahorro y optimización de costos ¿Diferentes o iguales?
Favicon
AWS Compute Optimizer released a new feature
Favicon
Síndrome del bolsillo profundo: síntomas y remedios
Favicon
FinOps Consulting Services for Cloud Cost Optimization
Favicon
Building Bedrock Agents for AWS Account Metadata and Cost Analysis
Favicon
Budget Forecasting for Legacy Cloud deployments using Cost Explorer and AWS Pricing Calculator
Favicon
Set budget alerts on GCP & AWS
Favicon
How to Estimate Cloud Costs with Terraform using Infracost
Favicon
How Can You Initiate FinOps Practices in an Organization?
Favicon
Cloud Financial Management: Best Practices for Optimizing Cloud Spend
Favicon
The Hidden Cost of Paying Twice for Inter-AZ Network Traffic in AWS
Favicon
Amazon EC2 now supports to Optimize CPUs post instance launch
Favicon
Optimize AWS Cloud Costs
Favicon
When Your AWS Bill Goes Viral and Your Finance Department Becomes a Memelord 🚀💸
Favicon
AWS Compute Optimizer now supports 80 additional Amazon EC2 instance types
Favicon
Cloud FinOps Services for Effective Financial Forecasting in Cloud Operations
Favicon
How I spent a few hours using advanced technology to save $2
Favicon
Optimization and Automation of AWS Resources to reduce costs without impacting operations
Favicon
AWS Cost Management now provides purchase recommendations for Amazon #DynamoDB reserved capacity
Favicon
k8s-pvc-tagger: The Swiss Army Knife of AWS EBS Tagging
Favicon
The Ultimate Guide to Kubernetes Monitoring: Best Practices and Hands-On Instructions
Favicon
Cuando tu factura de AWS se convierte en un meme y tu departamento financiero en un meme-lord 🚀💸
Favicon
2 simple tips to reduce your Google Cloud Run costs
Favicon
Turbo360 FinOps and Cost Management Is Now Available in the Microsoft Azure Marketplace
Favicon
Insights do IBM FinOps Day: Transformando a Gestão Financeira na Era da Nuvem

Featured ones: