It's the beginning of a month, and you receive an email in your inbox detailing your AWS bill for the previous month. You see how much you're charged, and you're shocked. You begin to ask several questions: How did I spend this much? What services did I use that cost this much? Did I forget to run terraform destroy
? Which you’re pretty sure you did… but the bill says otherwise. You sigh, still puzzled over how you ended up with such a huge charge.
If this sounds familiar, you’re not alone. Whether you’re a beginner, a seasoned cloud engineer, or somewhere in between, almost everyone has faced this kind of surprise at least once. You can handle this surprise by using various cost monitoring tools, either provided by the cloud vendor or third parties, but in this article, we will focus on cost monitoring using Infracost.
What is Infracost
Infracost is a cost monitoring solution that helps teams and companies shift cost left. In simple terms, Infracost gives you visibility into the cost of your infrastructure before you deploy to the cloud, instead of waiting until the end of the month to see what your infrastructure costs you.
This means you manage your cloud costs proactively rather than reactively. This allows you to balance your infrastructure needs with cost right from the onset, rather than deploying and waiting to find out your cost before attempting to optimize. And more importantly, this approach means you can make informed decisions, and this helps you to be more holistic in your approach, balancing both technical and business needs.
Why Use Infracost
The major challenge with cloud costs is visibility. When working with cloud infrastructure, you often don't know how much something will cost until it's deployed; by then, it's already too late, as the resources are running, and the bill is counting.
On reading this, you might say, you'll just make estimates using AWS or Azure calculator. And while you're right on this, it's a manual process and consumes valuable time that can be spent on activities much more profitable. Also, when using these tools, chances are you might leave out a resource or not account for a "hidden" charge. By hidden charge, I mean a charge that can go unnoticed.
A good example of this would be in a previous project where I built an end-to-end DevOps pipeline. In this project, I needed to use a NAT gateway to allow the resources in the private subnets to connect to the internet. I did my research and saw what fees I would incur for including it in my architecture. Imagine my surprise when I saw my bill and discovered I would also have to pay for IPv4-related charges, which I did not initially account for.
These reasons, among many others, are the reasons why Infracost matters. With Infracost, you see the cost of infrastructure ahead of time, so no surprises. You weigh tradeoffs between instance types, storage classes, or scaling strategies, allowing you to make smarter decisions. Lastly, costs show up in pull requests so everyone sees the financial impact of infrastructure changes.
Getting Started With Infracost
Installing Infracost
Installing Infracost depends on your operating system, but if you use a Linux operating system (like me), you can install Infracost by running the command below:
# 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
For other operating systems, you can find your respective commands here.
Creating an Account on Infracost
After installing Infracost, you need to create an account with Infracost. You can sign up here, either with GitHub, Google, or email and password.
On signing up, select your "Source control integrations", depending on your Version Control System (VCS), and click on "Connect".
You will be directed to install Infracost on your repository, either all or some. For security purposes, it is advisable to select only the repositories that you want Infracost to have access to. Once done, you confirm access by inputting your MFA code or password.
N.B.: If you do not have a Terraform project to practice with, you can fork an already existing Terraform project in my repo here
Back to Infracost, the next step is to decide how you want to "Add your code repos". You could choose to add all current and future Terraform repos automatically, which is recommended. You could choose to add it based on a wildcard, or finally add it manually. I'll go with the recommended version, but if you're hard on security, add your code repos manually.
In the next sections, you'll see how to work with Infracost in pull requests and when working with Terraform.
How Infracost Works in Pull Requests
For this section, create a new branch in your repository; a test repo will work fine for this, or clone the repository linked above. Ensure this repository is accessible by Infracost.
Next, create a file called infracost_test.tf
and paste in the following code:
provider "aws" {
region = "us-east-1"
skip_credentials_validation = true
skip_requesting_account_id = true
access_key = "mock_access_key"
secret_key = "mock_secret_key"
}
resource "aws_instance" "my_web_app" {
ami = "ami-005e54dee72cc1d00"
instance_type = "m3.xlarge" # <<<<<<<<<< Try changing this to m5.xlarge to compare the costs
tags = {
Environment = "production"
Service = "web-app"
}
root_block_device {
volume_size = 1000 # <<<<<<<<<< Try adding volume_type="gp3" to compare costs
}
}
resource "aws_lambda_function" "my_hello_world" {
runtime = "nodejs12.x"
handler = "exports.test"
image_uri = "test"
function_name = "test"
role = "arn:aws:ec2:us-east-1:123123123123:instance/i-1231231231"
memory_size = 512
tags = {
Environment = "Prod"
}
}
N.B.: This code is from Infracost's official "Get Started"
Once created, commit your changes, and head to the branch's main page.
Next, open a pull request by clicking on "Compare and Pull Request" or "Contribute" on the right-hand side of the page.
Put in your desired description and create your pull request
On creating a pull request, Infracost scans your Terraform files and points out inconsistencies such as tagging policies and guardrails, as seen below.
How Infracost Works with Terraform
For this section, create or clone a repository with Terraform files available, and ensure you have Terraform installed on your local machine.
To start, change into your project's root directory:
cd end-to-end-devops-pipeline-using-aws-docker-and-terraform
Next, get your Infracost API key. If you don't have it, you can generate it by clicking "Settings", then clicking on "Org Settings".
Then, to get your API, copy the already generated token for CLI and CI/CD operations.
After getting your API key, the next step is to configure your CLI. You can do this by running:
infracost configure set api_key {API_KEY}
Replace {API_KEY}
with your actual API key.
Once completed, you can get your Terraform infrastructure costs by running:
infracost breakdown --path .
And here's the result:
Project: main
Name Monthly Qty Unit Monthly Cost
aws_instance.my_web_app
├─ Instance usage (Linux/UNIX, on-demand, m3.xlarge) 730 hours $194.18
└─ root_block_device
└─ Storage (general purpose SSD, gp2) 1,000 GB $100.00
aws_lambda_function.my_hello_world
├─ Requests Monthly cost depends on usage: $0.20 per 1M requests
├─ Ephemeral storage Monthly cost depends on usage: $0.0000000309 per GB-seconds
└─ Duration (first 6B) Monthly cost depends on usage: $0.0000166667 per GB-seconds
Project total $294.18
──────────────────────────────────
Project: terraform
Module path: terraform
Name Monthly Qty Unit Monthly Cost
aws_instance.roi_calculator_bastion_host_ec2_public_subnet_one
├─ Instance usage (Linux/UNIX, on-demand, t3.xlarge) 730 hours $121.47
└─ root_block_device
└─ Storage (general purpose SSD, gp2) 8 GB $0.80
aws_instance.roi_calculator_bastion_host_ec2_public_subnet_two
├─ Instance usage (Linux/UNIX, on-demand, t3.xlarge) 730 hours $121.47
└─ root_block_device
└─ Storage (general purpose SSD, gp2) 8 GB $0.80
aws_instance.roi_calculator_production_host_ec2_private_subnet_one
├─ Instance usage (Linux/UNIX, on-demand, t3.xlarge) 730 hours $121.47
└─ root_block_device
└─ Storage (general purpose SSD, gp2) 8 GB $0.80
aws_instance.roi_calculator_production_host_ec2_private_subnet_two
├─ Instance usage (Linux/UNIX, on-demand, t3.xlarge) 730 hours $121.47
└─ root_block_device
└─ Storage (general purpose SSD, gp2) 8 GB $0.80
aws_nat_gateway.roi_calculator_ngw_private_subnet_one
├─ NAT gateway 730 hours $32.85
└─ Data processed Monthly cost depends on usage: $0.045 per GB
aws_nat_gateway.roi_calculator_ngw_private_subnet_two
├─ NAT gateway 730 hours $32.85
└─ Data processed Monthly cost depends on usage: $0.045 per GB
aws_lb.roi_calculator_aws_lb
├─ Application load balancer 730 hours $16.43
└─ Load balancer capacity units Monthly cost depends on usage: $5.84 per LCU
aws_db_instance.roi_calculator
├─ Database instance (on-demand, Single-AZ, db.t3.micro) 730 hours $13.14
└─ Storage (general purpose SSD, gp2) 5 GB $0.58
Project total $584.93
OVERALL TOTAL $879.11
*Usage costs can be estimated by updating Infracost Cloud settings; see docs for other options.
──────────────────────────────────
49 cloud resources were detected:
∙ 10 were estimated
∙ 39 were free
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Project ┃ Baseline cost ┃ Usage cost* ┃ Total cost ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━╋━━━━━━━━━━━━┫
┃ main ┃ $294 ┃ - ┃ $294 ┃
┃ terraform ┃ $585 ┃ - ┃ $585 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━━━━┛
From the example above, you see that Infracost breakdown estimates $879/month, mostly from EC2 instances ($680) and NAT gateways ($65), with smaller costs from storage, RDS, and load balancers. This clearly shows how compute and networking dominate cloud bills in this setup, and why Infracost is useful for spotting optimizations like right-sizing instances or reducing NAT gateways before deploying.
Conclusion
Getting hit with an unexpected cloud bill is one of those experiences that stays with you, and not in a good way. Throughout this article, we've explored how Infracost can transform the way you handle cloud costs by giving you the information you need upfront, rather than leaving you to discover it in your monthly bill. The beauty of Infracost lies in its simplicity; you get real-time cost estimates integrated right into your development workflow, whether through pull requests or command-line breakdowns.
The shift from reactive to proactive cost management isn't just about saving money; it's about making better decisions with complete information. Tools like Infracost help ensure that cost visibility becomes a natural part of your workflow, balancing both technical requirements and business realities from day one. At the end of the day, the best way to avoid bill shock is to eliminate the shock entirely.
Top comments (0)