Infracost is important for DevOps, SRE, and developers looking to estimate cloud costs for infrastructure-as-code projects such as Terraform. With Infracost, you can quickly see a cost breakdown while comparing different options, which helps lead to better insights before a big decision.
How Does Infracost Work?
Infracost can check over 3 million prices by scanning your Terraform code, and it produces a simple and easy-to-read cost estimate before you launch resources.
You can also use Infracost to know which lines of code have the biggest cost impact since it maps cost to resources in your pull requests. By integrating Infracost into your CI/CD, you can collaborate with your team and enable discussions over the impact of changes in your existing workflow.
Infracost works through its Cloud Pricing API backend service, and with the CLI tool, you can parse a Terraform plan in JSON format. The API cross-references the individual cost of each cloud resource in use, as well as their related cost parameters based on the cloud provider.
The program does not require any credentials to operate, and it doesn’t make any alterations to Terraform or to the analyzed cloud resources. By using the count of Terraform resource types, the API ensures that new resources are accounted for.
There are two main ways Infracost shows the breakdown of costs:
A) Full Breakdown of Costs
B) Difference of Monthly Costs Between Current and Planned State
Getting Started With Infracost (Step-by-Step)
Follow this step-by-step guide to install and start using Infracost:
1. Install Infracost
The first step to getting started is to install Infracost, but you must first already have Terraform installed.
Here is a look at the different versions of Infracost:
macOS Homebrew:
brew install infracost
infracost –version # Should show v0.9.7
macOS manual:
# 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
Linux:
# 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
Docker:
docker pull infracost/infracost
docker run –rm \
-e INFRACOST_API_KEY=see_following_step_on_how_to_get_this \
-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \
-v $PWD/:/code/ infracost/infracost breakdown –path /code/
# Add other required flags/envs for Infracost or Terraform
# For example, these might be required if you are using AWS assume-role:
# -e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN \
# -e AWS_REGION=$AWS_REGION \
Windows:
Download and unzip the latest release. Run it from the Command Prompt or Powershell using .\infracost.exe --no-color
alongside other required commands/flags (color output has a bug we need to fix on Windows). You should also move the exe file to a folder that is in your PATH
environment variable, e.g. C:\Windows
.
2. Get an API Key
The next step is to register for a free API key, which the CLI uses to query the Cloud Pricing API for tasks like getting prices for instance types.
All Operating Systems:
infracost register
The key is saved in ~/.config/infracost/credentials.yml
.
3. Run Infracost
Now you are ready to run Infracost, which does not make any changes to your Terraform state or cloud resources. You can run it with the provided Terraform project to test it out.
All Operating Systems:
git clone https://github.com/infracost/example-terraform.git
cd example-terraform/sample1
# Play with main.tf and re-run to compare costs
infracost breakdown –path .
# Show diff of monthly costs, edit the yml file and re-run to compare costs
infracost diff –path . –sync-usage-file –usage-file infracost-usage.yml
4. Add to CI/CD
You can now use Infracost’s CI/CD integrations to automatically add pull request comments, which show you cost estimate diffs. This will also provide you with a safetynet, enabling teams to discuss the cost impact of changes as part of their workflow.
Usage
The infracost
CLI has the following main commands:
breakdown
: show full breakdown of costsdiff
: show diff of monthly costs between current and planned state
Use an Infracost config file if your repo has multiple Terraform projects or workspaces. Their results will be combined into the same breakdown or diff output.
Advanced Usage
Infracost also has various advanced usage methods that can be used in addition to the regular usage methods. These advanced methods can be used via an Infracost config file as well.
Terraform Plan File
Infracost can be run against a Terraform plan file, which implies you have already run Terraform init.
Infracost just runs Terraformshow
in this case, meaning it doesn’t require cloud credentials or --terraform-plan-flags
to be set.
cd path/to/code
terraform init
terraform plan -out tfplan.binary
infracost breakdown –path tfplan.binary
infracost diff –path tfplan.binary
Terraform State
If you want to see the cost breakdown of the current Terraform state, you should know the infracost breakdown
command has a --terraform-use-state
flag, which would prove useful in this case. You must already run Terraform apply
, so Infracost just runs Terraform show
, which doesn’t require cloud credentials or --terraform-plan-flags
to be set.
infracost breakdown –path examples/terraform –terraform-use-state
Terraform State JSON File
You can run the infracost breakdown
against a Terraform state JSON file. This would be the [JSON output format] of the state instead of the internal JSON representation. It must be generated by running terraform show -json
inside a Terraform project, and it is especially useful if you want to see the cost breakdown of the current Terraform state. In this case, it implies you already run Terraform apply
, meaning no cloud credentials or --terraform-plan-flags
are required.
Which Clouds and Resources are Supported?
Infracost supports more than 200 Terraform resources across AWS, Google, and Azure, while other IaC tools like Pulumi and Cloudformation are included in the roadmap. Support for new resources is constantly being added, so it’s important to check the repo for releases.
Conclusion
Infracost is a helpful tool for developers, DevOps, SRE, and others looking to quickly see the cost breakdown and compare different options upfront. It is also a good choice if you are looking to integrate it with CI and/or need support for AWS and Google Cloud. If you want to create better collaboration between your team, Infracost can also help in that regard. All of these features make it a tool that you should strongly consider implementing for cloud cost estimates for Terraform.

Leave a Reply