Blog >

Infracost.io Configuration

Introduction

Infracost is an open-source fin-ops tool that helps DevOps and developers continuously reduce their cloud costs.

Infracost integrates with terraform (Infrastructure-as-a-code tool) to provide the breakdown of cost and difference in cost for any pull request that DevOps/Developers raised with shift-left approach.

Infracost empowers to discuss costs before the actual resources are created, not after the resources are created.

Getting Started

Installation

1. Install Infracost

Assuming Terraform is already installed, get the latest Infracost release on mac:

brew install infracost

2. Get API key

Register for a free API key:

infracost register

The key is saved in  ~/.config/infracost/credentials.yml .

3. Run it

Run Infracost using our example Terraform project to see how it works:

git clone git@github.com:org/terraform.git

cd kong-terraform

# Get breakdown of cost for one of the env

infracost breakdown –path /dev/

# Show diff of monthly costs, edit the yaml file and re-run to compare costs infracost diff –path /dev/ –usage-file /dev/infracost-usage.yml

Advanced usage

The following advanced usage methods can be used in addition to the usage methods mentioned above.

Using Terraform plan file

Infracost can be run against a Terraform plan file.

cd /dev/

terraform init

terraform plan -out tfplan.binary

infracost breakdown –path tfplan.binary

infracost diff –path tfplan.binary

Using Terraform state

The  infracost breakdown  command has a  –terraform-use-state  flag that is useful if you want to see the cost breakdown of the current Terraform state. This implies that you have already run Terraform  apply .

infracost breakdown –path /dev/ –terraform-use-state

Using Terraform state JSON file

The  infracost breakdown  command can be run against a Terraform state JSON file. This implies that you have already run Terraform  apply , thus no cloud creds or  –terraform-plan-flags  is needed.

cd /dev/

terraform show -json > tfstate.json

infracost breakdown –path tfstate.json

Generate reports

The  infracost breakdown  command has a  –format json|table|html  flag that can be used to change the output format. The JSON option can be used to generate files from individual projects that can then be consumed by the  infracost output  command to generate a combined report. The output command has a  –format json|diff|table|html  flag that sets the report format.

Usage

Run  infracost output –help  to see the available options. Example usage:

infracost breakdown –path /dev/ –format json > project1.json infracost breakdown –path /stage/ –format json > project2.json infracost output –path project*.json –format html > report.html

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/94b48619-c82d-4a45-a9f4-350c0f59f1a0/report.html


Config file

An Infracost config file can be created in each of your Terraform project repos to specify how Infracost should be run.

1. Create an  infracost.yml  file in each of your Terraform project repos. Each project can have the parameters mentioned in the table below:

version: 0.1

projects:

– path: /dev/tfplan.json

usage_file:/dev/infracost-usage.yml

– path: /test/tfplan.json

usage_file: /test/infracost-usage.yml

2. Pass the file to the  infracost breakdown  or  infracost diff  using the  -config-file  option.

infracost breakdown –config-file infracost.yml

infracost diff –config-file infracost.yml

Usage-based resources

Infracost distinguishes the price of a resource from its cost. For example, an EC2 instance might be priced at $0.02 per hour, and if run for 100 hours (its usage), it’ll cost $2. Supported resources in Infracost will always show prices, but if a resource has a usage-based cost component, we can’t show its cost as we don’t know how much it’ll be used. For example, an AWS Lambda resource shows no monthly costs for requests and duration:

Name Quantity Unit Monthly Cost aws_lambda_function.hi

├─ Requests Cost depends on usage: $0.20 per 1M requests

└─ Duration Cost depends on usage: $0.0000166667 per GB-seconds

PROJECT TOTAL $0.00


Infracost usage file

Infracost solves the above problem by enabling you to describe usage estimates in a file called  infracost-usage.yml , which can be passed to Infracost using the  –usage-file  option so it can calculate costs. An  infracost-usage.yml  file with Lambda function usage estimates:

version: 0.1

resource_usage:

aws_lambda_function.hi:

monthly_requests: 100000000 # Monthly requests to the Lambda function.

request_duration_ms: 250 # Average duration of each request in milliseconds.

Running  infracost breakdown –config-file infracost.yml –usage-file=infracost-usage.yml  now shows monthly cost estimates:

Name Quantity Unit Monthly Cost

aws_lambda_function.hi

├─ Requests 100 1M requests $20.00

└─ Duration 12,500,000 GB-seconds $208.33

PROJECT TOTAL $228.33

CI/CD Integration

Use our to automatically add pull request comments showing cost estimate diffs.

1. Developer/DevOps proposes a change to the infrastructure

A Developer/DevOps makes changes to the infrastructure as code file, and creates a pull/merge request.

Infracost.io Configuration 5

2. Infracost calculates the cost of the change

Infracost looks at the changes made to the Terraform file and shows the pre and post-change costs as a comment in the pull request: “This change will increase your bill by 27%”

3. Team discusses the cost implication

This enables engineering teams to see how much each pull request costs, and to peer review costs alongside the code.

Real example of how we have integrated infracost in our terraform repo:


Talk to our experts.

Heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

Quick Demo

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Popular Posts

Recent posts

Start your cloud transformation now.

TALK TO OUR EXPERTS

GET READY TO KNOW THE LASTEST UPDATE!

Sign up for our newsletter and stay up-to-date on the latest event trends, industry news, and exclusive Eventify promotions.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.