How to Estimate Cloud Costs with Terraform using Infracost

Listen to this Post

Featured Image
Managing cloud costs is a critical challenge for organizations transitioning to the cloud. While cloud services offer scalability and flexibility, costs can spiral without proper monitoring. Infracost is a powerful tool that integrates with Terraform to provide real-time cost estimates, helping teams optimize spending before deploying infrastructure.

You Should Know:

1. Install Infracost

First, install Infracost on your system:

 Linux/macOS 
curl -fsSL https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh | sh 

Verify installation:

infracost --version 

2. Set Up Infracost with Terraform

Run Infracost against your Terraform plan:

terraform init 
terraform plan -out tfplan.binary 
terraform show -json tfplan.binary > tfplan.json 
infracost breakdown --path tfplan.json 

3. Integrate with CI/CD Pipelines

Add Infracost to GitHub Actions:

- name: Setup Infracost 
uses: infracost/actions/setup@v2 
with: 
api-key: ${{ secrets.INFRACOST_API_KEY }}

<ul>
<li>name: Generate Infracost report 
run: | 
terraform plan -out tfplan.binary 
terraform show -json tfplan.binary > tfplan.json 
infracost breakdown --path tfplan.json --format json --out-file infracost.json 

4. Monitor Costs Over Time

Use Infracost Cloud for historical cost tracking:

infracost upload --path infracost.json 

5. Optimize Costs

Identify expensive resources and adjust configurations:

 Example: Reduce AWS EC2 instance size 
resource "aws_instance" "example" { 
ami = "ami-0c55b159cbfafe1f0" 
instance_type = "t3.medium"  Downgrade from t3.large 
} 

What Undercode Say:

Cloud cost management is essential for financial control. Tools like Infracost provide visibility into infrastructure expenses, preventing budget overruns. By integrating cost checks into CI/CD pipelines, teams can enforce cost-aware deployments.

Expected Output:

Project: my-terraform-project

Name Monthly Cost 
──────────────────────────────── 
aws_instance.web $24.50 
aws_lb.frontend $18.20 
──────────────────────────────── 
Total $42.70 

Prediction:

As cloud adoption grows, FinOps (Cloud Financial Management) will become a standard practice, with tools like Infracost playing a key role in cost optimization.

Reference:

How to Estimate Cloud Costs with Terraform using Infracost

IT/Security Reporter URL:

Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram