Skip to main content

Terraform Cloud Cost Estimation

Introduction

Cost management is a critical aspect of infrastructure as code. As organizations deploy increasingly complex cloud infrastructure, understanding and controlling costs becomes essential. Terraform Cloud's cost estimation feature provides a way to predict infrastructure costs before resources are provisioned, helping teams make informed decisions and avoid unexpected expenses.

In this guide, we'll explore how Terraform Cloud's cost estimation works, how to set it up, and how to leverage it effectively in your infrastructure planning process.

What is Cost Estimation in Terraform Cloud?

Cost estimation in Terraform Cloud is a feature that analyzes your Terraform configuration files and provides an estimate of how much your infrastructure will cost to run. This happens during the planning phase, allowing you to review potential costs before applying any changes.

The feature helps answer important questions like:

  • How much will this new infrastructure cost to run?
  • What will be the cost impact of these changes to existing infrastructure?
  • Which resources contribute most to our cloud spending?

Prerequisites

Before getting started with cost estimation in Terraform Cloud, you'll need:

  1. A Terraform Cloud account (Team & Governance tier or higher)
  2. Infrastructure defined as code using Terraform
  3. Cloud provider credentials properly configured
  4. Basic understanding of Terraform workflows

Setting Up Cost Estimation

Enabling Cost Estimation

Cost estimation is available in Terraform Cloud's Team & Governance tier and above. To enable it:

  1. Navigate to your organization settings in Terraform Cloud
  2. Select "Cost Estimation" from the settings menu
  3. Toggle the feature on for the appropriate workspaces
hcl
# Example of a simple AWS EC2 instance configuration
resource "aws_instance" "web_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"

tags = {
Name = "WebServer"
Environment = "Production"
}
}

Configuring Cloud Provider Integration

For accurate cost estimation, Terraform Cloud needs to understand your cloud provider's pricing. This requires proper integration setup:

AWS Integration

hcl
# In your Terraform Cloud workspace, configure AWS credentials
# These credentials should have pricing API access

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}

provider "aws" {
region = "us-west-2"
}

Azure Integration

hcl
# Azure provider configuration for cost estimation

terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}

provider "azurerm" {
features {}
}

Google Cloud Integration

hcl
# GCP provider configuration for cost estimation

terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 4.0"
}
}
}

provider "google" {
project = "my-project-id"
region = "us-central1"
}

Understanding Cost Estimates

When you run a Terraform plan in a workspace with cost estimation enabled, Terraform Cloud analyzes your configuration and provides cost information alongside the plan results.

Sample Cost Estimate Output

Terraform will perform the following actions:

# aws_instance.web_server will be created
+ resource "aws_instance" "web_server" {
+ ami = "ami-0c55b159cbfafe1f0"
+ instance_type = "t2.micro"
+ tags = {
+ "Environment" = "Production"
+ "Name" = "WebServer"
}
# (additional attributes omitted)
}

Plan: 1 to add, 0 to change, 0 to destroy.

Terraform Cloud Cost Estimation:

Resources: 1 of 1 estimated
Monthly cost estimate: $8.47

* aws_instance.web_server: $8.47

Understanding the Cost Breakdown

The cost estimate shows:

  • Total monthly cost for all resources
  • Per-resource cost breakdown
  • Any resources that couldn't be estimated

It's important to note that these are estimates based on:

  • Published cloud provider pricing
  • Default usage assumptions
  • Standard pricing models (not accounting for custom discounts)

Working with Cost Policies

Terraform Cloud allows you to create cost policies that establish guardrails for your infrastructure spending.

Creating a Cost Policy

Cost policies are created using the Sentinel policy language. Here's a simple example:

hcl
# policy.sentinel - Limit monthly cost increase to $100
import "tfrun"

monthly_cost_delta = tfrun.cost_estimate.delta_monthly_cost

main = rule {
monthly_cost_delta <= 100
}

Cost Policy Workflow

When a cost policy is in place:

  1. The team member creates a Terraform plan
  2. Terraform Cloud calculates cost estimates
  3. The policy is evaluated against the cost estimate
  4. If the policy fails, the plan cannot be applied

This creates a workflow where cost controls are enforced automatically.

Practical Examples

Let's look at some real-world examples of using cost estimation in Terraform Cloud.

Example 1: Estimating a Web Application Infrastructure

hcl
# Web application infrastructure with cost estimates
resource "aws_instance" "web" {
count = 2
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.small"
}

resource "aws_db_instance" "database" {
allocated_storage = 20
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
username = "admin"
password = var.db_password
parameter_group_name = "default.mysql5.7"
skip_final_snapshot = true
}

resource "aws_s3_bucket" "static_assets" {
bucket = "my-app-static-assets"
}

When you run a plan with this configuration, you'll receive a cost breakdown for each component, allowing you to understand which elements contribute most to your infrastructure costs.

Example 2: Comparing Different Instance Types

You can use cost estimation to compare different infrastructure options:

hcl
# Option 1: Single larger instance
resource "aws_instance" "option1" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "m5.large"
}

# Option 2: Multiple smaller instances
resource "aws_instance" "option2" {
count = 3
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.small"
}

By running plans with different configurations and reviewing the cost estimates, teams can make data-driven decisions about infrastructure design.

Example 3: Cost Difference When Scaling

hcl
# Before scaling
resource "aws_eks_cluster" "cluster" {
name = "my-cluster"
role_arn = aws_iam_role.cluster.arn
version = "1.21"

vpc_config {
subnet_ids = var.subnet_ids
}
}

resource "aws_eks_node_group" "nodes" {
cluster_name = aws_eks_cluster.cluster.name
node_group_name = "standard-workers"
node_role_arn = aws_iam_role.node.arn
subnet_ids = var.subnet_ids

scaling_config {
desired_size = 2
max_size = 3
min_size = 1
}

instance_types = ["t3.medium"]
}

# After scaling: Update to
# scaling_config {
# desired_size = 5
# max_size = 10
# min_size = 3
# }

Running a plan after updating the scaling configuration would show the cost impact of the change, helping teams budget appropriately for growth.

Cost Estimation for Multi-Cloud Environments

Terraform Cloud's cost estimation works across multiple cloud providers, allowing you to estimate costs for hybrid or multi-cloud deployments.

hcl
# Multi-cloud example with AWS and Azure

# AWS resources
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}

# Azure resources
resource "azurerm_linux_virtual_machine" "app" {
name = "app-machine"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
size = "Standard_B1s"
admin_username = "adminuser"

network_interface_ids = [
azurerm_network_interface.example.id,
]

os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
}

Visualizing Cost Data

Terraform Cloud provides visualization tools to help understand cost data. The Cost Estimation UI includes:

  1. Monthly cost breakdown
  2. Cost trends over time
  3. Resource-specific cost details

These visualizations make it easier to:

  • Identify cost drivers
  • Track spending trends
  • Communicate cost information to stakeholders

Integrating Cost Estimation into CI/CD

Cost estimation becomes particularly powerful when integrated into CI/CD pipelines. This can be achieved through:

GitHub Actions Integration

yaml
name: Terraform Plan with Cost Estimation

on:
pull_request:
branches: [ main ]

jobs:
terraform:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Terraform Plan
uses: hashicorp/setup-terraform@v1
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
terraform_wrapper: false

- run: terraform init

- name: Terraform Plan with Cost Estimation
run: terraform plan -out=tfplan
env:
TF_CLOUD_ORGANIZATION: "my-org"
TF_WORKSPACE: "my-workspace"

This setup ensures that cost estimates are generated for every PR, making cost implications visible during code review.

Advanced Cost Management Techniques

Beyond basic cost estimation, Terraform Cloud enables several advanced cost management techniques:

1. Resource Tagging Strategies

hcl
# Implement consistent tagging for cost allocation
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"

tags = {
Name = "web-server"
Environment = "production"
Department = "engineering"
Project = "website-redesign"
CostCenter = "12345"
}
}

2. Using Variables for Cost Control

hcl
# Define variables that affect cost
variable "instance_type" {
type = string
default = "t2.micro"
description = "EC2 instance type to use"
}

variable "instance_count" {
type = number
default = 2
description = "Number of instances to provision"
}

# Use the variables in resource definitions
resource "aws_instance" "app" {
count = var.instance_count
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_type
}

Best Practices for Cost Estimation

To get the most out of Terraform Cloud's cost estimation:

  1. Enable cost estimation for all workspaces to maintain visibility across your infrastructure
  2. Create cost policies to enforce spending limits
  3. Review cost estimates during planning cycles to avoid surprises
  4. Tag resources consistently to improve cost allocation reporting
  5. Document cost considerations in module READMEs and documentation
  6. Compare cost estimates between environments (dev, staging, production)
  7. Track cost trends over time to identify optimization opportunities

Troubleshooting Cost Estimation

Common Issues and Solutions

  1. Missing cost data for certain resources

    • Ensure the resource is supported by the cost estimation feature
    • Check that provider credentials have access to pricing APIs
  2. Inaccurate estimates

    • Verify your region settings match your actual deployment
    • Check for custom pricing agreements not reflected in standard rates
    • Update to the latest provider versions
  3. Cost estimation timeouts

    • Break large configurations into smaller workspaces
    • Simplify complex module structures

Summary

Terraform Cloud's cost estimation feature provides powerful capabilities for predicting and managing infrastructure costs. By integrating cost awareness into your infrastructure as code workflow, you can:

  • Make informed decisions based on financial impact
  • Avoid unexpected cloud expenses
  • Establish guardrails for infrastructure spending
  • Optimize resource allocation across projects

By following the practices outlined in this guide, you can leverage cost estimation to build not just technically sound infrastructure, but financially optimized infrastructure as well.

Additional Resources

Here are some exercises to help you practice using Terraform Cloud cost estimation:

  1. Exercise: Set up a simple workspace with cost estimation enabled and compare the costs of different instance types for a basic application stack.

  2. Exercise: Create a Sentinel policy that limits the maximum monthly cost increase for any workspace to $50.

  3. Exercise: Refactor an existing Terraform configuration to reduce estimated costs by at least 10% without sacrificing necessary functionality.

  4. Challenge: Create a multi-cloud deployment and use cost estimation to determine which cloud provider offers the most cost-effective solution for your specific requirements.



If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)