Cost estimation from Terraform with Infracost
Barbara Gaspar

Barbara Gaspar @barbara_gaspar

About: Principal FinOps Engineer

Location:
México
Joined:
Jan 8, 2025

Cost estimation from Terraform with Infracost

Publish Date: Apr 23
0 0

Using Infracost we can estimate infrastructure costs prior to deployment from Terraform.
The steps to execute it are as follows:

Install Infracost

brew install infracost

infracost --version # Should show 0.10.40
Enter fullscreen mode Exit fullscreen mode

code

If we already have it installed, we can update to the latest version

brew update
Enter fullscreen mode Exit fullscreen mode

Get API key

To get a free API that uses CLI to retrieve prices from prices in the cloud, it is necessary to make a registration, which directs us to a web page to register our mail.

infracost auth login
Enter fullscreen mode Exit fullscreen mode

Once you are registered, you will see this message

Infracost

code

Create the working directory and file

#Directory creation 
mkdir my-terraform-project
cd my-terraform-project

#main file creation
touch main.tf 
nano main.tf #edit file
Enter fullscreen mode Exit fullscreen mode

Edit the file with 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"
  }
}
Enter fullscreen mode Exit fullscreen mode

We apply the configuration

code

The result of this estimation we have $294 of consumption for the infrastructure we defined in the main.tf file. If we edit some configurations, we can compare the costs of the infrastructure to be deployed. Undoubtedly very useful for estimating costs.

References
Infracost (2025). Get Started, https://www.infracost.io

Comments 0 total

    Add comment