Having worked in the “cloud” for several years, one thing that I’m super conscious about is our cloud bill. There are tons of subtleties associated with billing, such as AZ-to-AZ traffic costs or how VPC endpoints can reduce egress charges. If you utilize Terraform for infrastructure provisioning, you may want to look at infracost. Infracost can help you understand cloud spend for a green field deployment, or what it will cost to expand your existing infrastructure. While it can’t model costs exactly, what it can do is help you approximate what a given infrastructure change will do your wallet.
Getting started with infracost is super easy. You will first need to run $(infracost register) to get an API token. Once you have this, you will need to set the INFRACOST_API_KEY environment variable with the token you received. This token is used to talk to the infracost cloud pricing APIs. To view a full cost estimate for a service, you can run infracost with the breakdown command, the “–path” option, and the directory that contains the Terraform HCL you want to analyze:
$ infracost breakdown --path .
Detected Terraform directory at .
✔ Checking for cached plan... expired
✔ Running terraform plan
✔ Running terraform show
✔ Extracting only cost-related params from terraform
✔ Retrieving cloud prices to calculate costs
Project: .
Name Monthly Qty Unit Monthly Cost
module.eks_control_plane.aws_cloudwatch_log_group.eks_logging
├─ 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_control_plane.aws_eks_cluster.eks_cluster
└─ EKS cluster 730 hours $73.00
module.eks_node_group_api.aws_eks_node_group.eks_node_group
├─ Instance usage (Linux/UNIX, on-demand, m5.large) 1,460 hours $140.16
└─ Storage (general purpose SSD, gp2) 40 GB $4.00
module.eks_node_group_ingress.aws_eks_node_group.eks_node_group
├─ Instance usage (Linux/UNIX, on-demand, m5.large) 730 hours $70.08
└─ Storage (general purpose SSD, gp2) 20 GB $2.00
OVERALL TOTAL $289.24
──────────────────────────────────
24 cloud resources were detected:
∙ 4 were estimated, 3 of which include usage-based costs, see https://infracost.io/usage-file
∙ 20 were free, rerun with --show-skipped to see details
If you are making a change to a root module and want to see how that will impact your bill, you can run infracost with the diff command and the “–path” option:
$ infracost diff --path .
Detected Terraform directory at .
✔ Checking for cached plan... change detected
✔ Running terraform plan
✔ Running terraform show
✔ Extracting only cost-related params from terraform
✔ Retrieving cloud prices to calculate costs
Project: .
~ module.eks_node_group_api.aws_eks_node_group.eks_node_group
+$577 ($144 → $721)
~ Instance usage (Linux/UNIX, on-demand, m5.large)
+$561 ($140 → $701)
~ Storage (general purpose SSD, gp2)
+$16.00 ($4.00 → $20.00)
Monthly cost change for .
Amount: +$577 ($289 → $866)
Percent: +199%
──────────────────────────────────
Key: ~ changed, + added, - removed
24 cloud resources were detected:
∙ 4 were estimated, 3 of which include usage-based costs, see https://infracost.io/usage-file
∙ 20 were free, rerun with --show-skipped to see details
The estimates that infracost provides can be included along with your plan in a pull request, and are invaluable for finding issues (e.g., using a G3 instance instead of an M3) before you apply your plan to a given environment. Infracost gets even more useful when you integrate it with your favorite CI tool. Then when you submit your plan through a pull request (or through a CI job), infracost will run behind the scenes and attach a cost estimate to your PR. The layer of protection infracost provides is worth its weight in gold!
If you decide to use infracost I would highly suggest signing up for an Enterprise account. It costs the team at infracost time and money to make their APIs available. Spending a few bucks each month will help them improve their product, and give you access to advanced features and support. And for the record, I have no affiliations with infracost. I just love the features it provides!