Getting started

Infracost shows cloud cost estimates for Terraform projects. It helps developers, devops and others to quickly see the cost breakdown and compare different options upfront.

Installation#

1. Install Infracost#

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

brew install infracost

Subsequent updates can be installed in the usual way: brew upgrade infracost

2. Get API key#

Use our free Cloud Pricing API by registering for an API key:

infracost register

The key is saved in ~/.config/infracost/config.yml. If you prefer, you can run your own Cloud Pricing API.

3. Run it#

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

git clone https://github.com/infracost/example-terraform.git
cd example-terraform
# You can play with `aws/main.tf` and `aws/infracost-usage.yml`, and re-run infracost to compare costs
infracost --tfdir aws --usage-file aws/infracost-usage.yml

Our CI/CD integrations can be used to automatically add a comment showing the cost estimate diff between a pull request and the master branch.

Usage methods#

Infracost can be run with different options depending on the use-case. As mentioned in the FAQ, you can run Infracost in your Terraform directories without worrying about security or privacy issues as no cloud credentials, secrets, tags or Terraform resource identifiers are sent to the open-source Cloud Pricing API. Infracost does not make any changes to your Terraform state or cloud resources.

1. Terraform directory#

This is the default method. Point to the Terraform directory using --tfdir and pass any required Terraform flags using --tfflags. Internally Infracost runs Terraform init, plan and show; init requires cloud credentials to be set, e.g. via the usual AWS_ACCESS_KEY_ID environment variables. This method works with remote state too.

infracost --tfdir /path/to/code --tfflags "-var-file=myvars.tfvars"

2. Terraform state file#

Point to the Terraform directory using --tfdir and instruct Infracost to use the Terraform state file using --use-tfstate. This implies that you have already run Terraform init, thus Infracost just runs Terraform show, which does not require cloud creds to be set. This method takes less time to run compared with method #1 and also works with remote state.

terraform init
infracost --tfdir /path/to/code --use-tfstate

3. Terraform plan JSON#

Point to an existing Terraform plan JSON file using --tfjson. This implies that the user has already run Terraform init, thus Infracost just runs Terraform show, which does not require cloud creds to be set.

cd path/to/code
terraform init
terraform plan -out plan.save .
terraform show -json plan.save > plan.json
infracost --tfjson plan.json

4. Terraform plan file#

Point to the Terraform directory and use the Terraform plan. This implies that the user has already run Terraform init, thus Infracost just runs Terraform show, which does not require cloud creds to be set. This method works with remote state too.

cd path/to/code
terraform init
terraform plan -out plan.save .
infracost --tfdir /path/to/code --tfplan plan.save

Useful options#

Run infracost --help to see the available options, which include:

--usage-file Path to Infracost usage file that specifies values for usage-based resources
--output value Output format: json, table, html (default: "table")
--show-skipped Show unsupported resources, some of which might be free. Only for table and HTML output (default: false)
--no-color Turn off colored output (default: false)
--log-level value Use "debug" to troubleshoot, can be set to "info" or "warn" in CI/CD systems to reduce noise