Infracost can estimate your cloud costs before you run terraform apply, saving you from surprise bills.

Here’s a quick look at what it does. Imagine you have this Terraform code:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "HelloWorld"
  }
}

Running infracost break-down in the same directory would show you something like this:

$ infracost break-down

Your AWS EC2 instance (t2.micro) will cost approximately $21.12 / month.
(This is based on On-Demand pricing in us-east-1, and assumes 730 hours of usage per month)

      Resource         Terraform Action      Monthly Cost
---------------------------------------------------------------------
  aws_instance.example       create               $21.12
---------------------------------------------------------------------
Total Monthly Cost             create              $21.12

This is useful, but the real magic happens when you integrate it into your workflow. Infracost works by parsing your Terraform plan or state files. It then queries cloud provider APIs for pricing information and correlates that with the resources defined in your Terraform code.

Let’s dive deeper. The core problem Infracost solves is the lack of cost visibility during infrastructure development. Traditionally, you’d write Terraform, apply it, and then look at your cloud bill. This disconnect means developers often provision resources without a clear understanding of their financial impact, leading to cost overruns.

Infracost bridges this gap. It analyzes your terraform plan output. This output describes the resources Terraform intends to create, modify, or destroy. Infracost takes this information and translates it into estimated costs.

Here’s how it works internally:

  1. Plan Parsing: infracost break-down -- terraform plan (or infracost breakdown --path terraform.tfstate) reads the plan file. It identifies each resource and its configuration.
  2. Resource Mapping: For each resource (e.g., aws_instance, aws_db_instance, aws_s3_bucket), Infracost knows which cloud service it belongs to and what attributes are relevant to cost (e.g., instance_type, allocated_storage, region, storage_class).
  3. Pricing API Calls: It queries cloud provider pricing APIs (AWS, Azure, GCP) for the specific region and configuration. It accounts for different pricing models: On-Demand, Reserved Instances, Savings Plans, Spot Instances, etc.
  4. Cost Calculation: It sums up the costs for all identified resources, providing a detailed breakdown.

The key levers you control are the resources you define in your Terraform code. By changing an instance_type from t3.medium to t3.small, or by selecting a different region, or by adding tags that enable cost allocation, you directly influence the output of Infracost.

Consider a more complex example with multiple resources:

resource "aws_instance" "web_server" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.medium"
  region        = "us-west-2"

  tags = {
    Environment = "Production"
    Project     = "WebApp"
  }
}

resource "aws_rds_instance" "db" {
  identifier           = "mydb"
  engine               = "mysql"
  engine_version       = "8.0"
  instance_class       = "db.t3.small"
  allocated_storage    = 100
  storage_type         = "gp2"
  multi_az             = false
  publicly_accessible  = false
  db_subnet_group_name = aws_db_subnet_group.main.name
  vpc_security_group_ids = [aws_security_group.db.id]

  tags = {
    Environment = "Production"
    Project     = "WebApp"
  }
}

resource "aws_db_subnet_group" "main" {
  name       = "main"
  subnet_ids = ["subnet-xxxxxxxxxxxxxxxxx", "subnet-yyyyyyyyyyyyyyyyy"]
}

resource "aws_security_group" "db" {
  name        = "db-sg"
  description = "Allow DB access"
  vpc_id      = "vpc-zzzzzzzzzzzzzzzzz"
}

Running infracost break-down on this would yield a more detailed report, showing costs for both the EC2 instance and the RDS instance, factoring in their respective configurations and regions.

What often surprises people is how Infracost handles different pricing tiers and options. It doesn’t just grab the default On-Demand price. If your Terraform code specifies a region where Reserved Instances are frequently used, or if you’ve configured a resource for Spot instances, Infracost will attempt to reflect that in its estimates. For example, if you’re using AWS Spot instances, Infracost can estimate the potential savings compared to On-Demand pricing. You can also provide your own custom pricing files for private pricing or specific negotiated rates.

The most common way to use Infracost is via its CLI. However, it also offers integrations with CI/CD pipelines, GitHub Pull Requests, and other platforms, allowing for automated cost checks before code merges. This proactive approach helps catch potential cost issues early in the development cycle.

The next step after getting comfortable with cost estimation is understanding how to optimize those costs based on the insights Infracost provides, which often involves looking at different instance families or storage types.

Want structured learning?

Take the full Terraform course →