Infracost for Terraform code
Many companies use Terraform as a IaC standard. Though, every other company finds themselves in situation when their infrastructure grows really quick and they end up with huge bills. In this situation, there are many different approaches you can take One of them, is proactively understand and track the cloud spent
One of the best tools for this is an Infracost. It is becoming more of a standard for this kind of job. There are lots of integrations, super easy to get started, and provides good visualisation capabilities (for some extra fees)
Highlights & Features of Infracost
- infracost CLI that allows to give you cost estimate in single command
- Define your FinOps tagging policies here to communicate and enforce required tag keys/values in pull requests. This enables you to shift-left on your tagging practices and provide engineers with fast feedback loops vs asking them to clean up tagging issues post-deployment.
- Post cost estimates in pull requests
- infracost diff shows diff of monthly costs between current and planned state
- Infracost Cloud with dashboard & integration with CI/CDs
- Infracost supports over 1,000 Terraform resources
- works with Terraform, Terragrunt , Cloudformation
- Guardrails help you control costs by monitoring pull requests and triggering actions when thresholds are exceeded. You can define cost or percentage based thresholds, and setup email approvals or Slack notifications.
- Integration with GIT (Github & Enterprise Cloud, Gitlab, Azure Repos, Bitbucket)
- Infracost VS Code integration
Infracost Licence
Apache-2.0 license, SOC2 Type II certified , 9.7k stars on Gitbhub
Infracost Pricing
Infracost is open source , but also has a Cloud SaaS offering
"Infra Cost CI/CD" has free 1,000 runs per month, upgrade packs can be purchased (purchase now) Price per upgrade pack of 10K per month runs in Oct 2023 is $150.00 (excluding tax.)
"Infracost Cloud" costs $50 per seat . Seats are equal to authors of PRs. Atthe end of the month Infracost Cloud counts how many unique PR authors you have, and add any seats needed for Infracost Cloud users. If you have a monorepo with code different that TF, it only takes into account PRs related to Terraform code only
"Infracost Enterprise" Price not listed, but we would put a ballmark of 1-8% based on your Cloud bill and other similar Cloud pricing tools for Enterprise.
Enterprise discounts:
- AWS Enterprise Discount Program (EDP)
- Google Commitment Agreement
- Azure Prepayment / Azure Monetary Commitment which are part of Azure Enterprise Agreements
Infracost Pricing documentation
Install the Infracost
Install Infracost on macOS with brew
brew install infracost
infracost --version
Install Infracost on Linux/ Bash script via Curl
curl -fsSL https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh | sh
Install Infracost on Windows
choco install infracost
infracost --version
Run Infracost in Docker (For CI or AMD images)
docker pull infracost/infracost:ci-latest
docker run --rm \
-e INFRACOST_API_KEY=see_following_step_on_how_to_get_this \
-v $PWD/:/code/ infracost/infracost:ci-latest breakdown --path /code/
Dockerised CLI is targetting CI systems, as well there is no ARM Docker Image yet On Mac M1 you will get following errors "no matching manifest for linux/arm64/v8". Check Infracost Gihub Issue: Build arm64 Docker image #2492
Using infracost CLI with Terraform code
When using free Cloud Pricing API, you need to get an API key via "infracost auth login" command
The API key after succesful auth is saved under <Home_DIR_OS_USER>.config/infracost/credentials.yml
infracost auth login
Provide a breakdown of cloud costs based on Terraform code
infracost breakdown --path=.
Example output :
Evaluating Terraform directory at .
✔ Downloading Terraform modules
✔ Evaluating Terraform directory
✔ Retrieving cloud prices to calculate costs
Project: cloudtipss/Private-EKS-With-Bastion-Host
Name Monthly Qty Unit Monthly Cost
module.ec2-instance.aws_instance.this[0]
├─ Instance usage (Linux/UNIX, on-demand, t3.small) 730 hours $15.18
└─ root_block_device
└─ Storage (general purpose SSD, gp2) 8 GB $0.80
module.eks.aws_cloudwatch_log_group.this[0]
├─ Data ingested Monthly cost depends on usage: $0.50 per GB
├─ Archival Storage Monthly cost depends on usage: $0.03 per GB
└─ Insights queries data scanned Monthly cost depends on usage: $0.005 per GB
module.eks.aws_eks_cluster.this[0]
└─ EKS cluster 730 hours $73.00
module.eks.module.eks_managed_node_group["on_demand_1"].aws_eks_node_group.this[0]
└─ module.eks.module.eks_managed_node_group["on_demand_1"].aws_launch_template.this[0]
├─ Instance usage (Linux/UNIX, on-demand, t3.small) 730 hours $15.18
└─ EC2 detailed monitoring 7 metrics $2.10
module.eks.module.kms.aws_kms_key.this[0]
├─ Customer master key 1 months $1.00
├─ Requests Monthly cost depends on usage: $0.03 per 10k requests
├─ ECC GenerateDataKeyPair requests Monthly cost depends on usage: $0.10 per 10k requests
└─ RSA GenerateDataKeyPair requests Monthly cost depends on usage: $0.10 per 10k requests
module.vpc.aws_nat_gateway.this[0]
├─ NAT gateway 730 hours $32.85
└─ Data processed Monthly cost depends on usage: $0.045 per GB
OVERALL TOTAL $140.12
──────────────────────────────────
61 cloud resources were detected:
∙ 6 were estimated, 4 of which include usage-based costs, see https://infracost.io/usage-file
∙ 55 were free, rerun with --show-skipped to see details
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓
┃ Project ┃ Monthly cost ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━┫
┃ cloudtipss/Private-EKS-With-Bastion-Host ┃ $140 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━┛