本篇介紹如何使用 infracost 工具估計 infrastructure apply 的花費
課程內容與代碼會放在 Github 上: https://github.com/chechiachang/terraform-30-days
賽後文章會整理放到個人的部落格上 http://chechia.net/
『給我無限多的預算,我就能撐起全世界』
然而,現實中不存在無限預算的專案,因此成本控管就非常重要。infrastructure 管理中很重要的一部份便是成本控管,以合理成本來架設 infrastructure 是雲端管理必備技能。
為了做到成本控管,我們會需要預估一組 infrastructure 的未來花費,而不是等到 apply 上去後等下個月的帳單來(XD)
這是個教你吃米要知道米價的工具
既然已經導入 terraform,我們完全可以預期一個 root module 會產生多少 infrastructure
安裝
wget https://github.com/infracost/infracost/releases/download/v0.9.8/infracost-darwin-amd64.tar.gz
tar -zxf infracost-darwin-amd64.tar.gz
sudo mv infracost-darwin-amd64 /usr/local/bin/infracost
版本要注意一下,建議用新版
infracost --version
Infracost v0.9.8
Infracost - cloud cost estimates for Terraform
DOCS
https://infracost.io/docs
USAGE
infracost [flags]
infracost [command]
EXAMPLES
Generate a cost diff from Terraform directory with any required Terraform flags:
infracost diff --path /path/to/code --terraform-plan-flags "-var-file=my.tfvars"
Generate a full cost breakdown from Terraform directory with any required Terraform flags:
infracost breakdown --path /path/to/code --terraform-plan-flags "-var-file=my.tfvars"
AVAILABLE COMMANDS
breakdown Show full breakdown of costs
completion Generate completion script
configure Display or change global configuration
diff Show diff of monthly costs between current and planned state
help Help about any command
output Combine and output Infracost JSON files in different formats
register Register for a free Infracost API key
FLAGS
-h, --help help for infracost
--log-level string Log level (trace, debug, info, warn, error, fatal)
--no-color Turn off colored output
-v, --version version for infracost
Use "infracost [command] --help" for more information about a command.
使用 infracost 需要註冊 api key,執行下面命令註冊即可
infracost register
cat ${HOME}/.config/infracost/credentials.yml
算看看 azure/_poc/foundation
要花多少錢
infracost diff --path azure/_poc/foundation/
Detected Terraform directory at azure/_poc/foundation/
✔ Running terraform plan
✔ Running terraform show
✔ Calculating monthly cost estimate
Project: chechiachang/terraform-30-days/azure/_poc/foundation
Monthly cost change for chechiachang/terraform-30-days/azure/_poc/foundation
Amount: $0.00 ($0.00 -> $0.00)
----------------------------------
Key: ~ changed, + added, - removed
No changes detected. Run infracost breakdown to see the full breakdown.
2 resource types weren't estimated as they're not supported yet, rerun with --show-skipped to see.
Please watch/star https://github.com/infracost/infracost as new resources are added regularly.
算出來不用錢?!太佛拉!!
--show-skipped
options 來看到更完整的訊息infracost diff --path azure/_poc/foundation/ --show-skipped
Detected Terraform directory at azure/_poc/foundation/
✔ Running terraform plan
✔ Running terraform show
✔ Calculating monthly cost estimate
Project: chechiachang/terraform-30-days/azure/_poc/foundation
Monthly cost change for chechiachang/terraform-30-days/azure/_poc/foundation
Amount: $0.00 ($0.00 -> $0.00)
----------------------------------
Key: ~ changed, + added, - removed
No changes detected. Run infracost breakdown to see the full breakdown.
2 resource types weren't estimated as they're not supported yet.
Please watch/star https://github.com/infracost/infracost as new resources are added regularly.
1 x azurerm_storage_account
1 x azurerm_storage_container
兩個 resource 上不支援計價
新版 infracost 會自動偵測 terragrunt 並使用 terragrunt 的設定,產生 .tf 後才會計算 cost,細節請見infracost官分說明文件
infracost diff --path azure/dev/southeastasia/chechia_net/kubernetes
Detected Terragrunt directory at azure/dev/southeastasia/chechia_net/kubernetes
✔ Running terragrunt run-all terragrunt-info
✔ Running terragrunt run-all plan
✔ Running terragrunt show
✔ Calculating monthly cost estimate
Project: chechiachang/terraform-30-days/azure/dev/southeastasia/chechia_net/kubernetes
+ azurerm_kubernetes_cluster.main
+$116
+ default_node_pool
+ Instance usage (pay as you go, Standard_D2_v2)
+$115
+ os_disk
+ Storage (P1)
+$0.78
+ Load Balancer
+ Data processed
Monthly cost depends on usage
+$0.005 per GB
+ azurerm_kubernetes_cluster_node_pool.spot["spot"]
+$116
+ Instance usage (pay as you go, Standard_D2_v2)
+$115
+ os_disk
+ Storage (P1)
+$0.78
Monthly cost change for chechiachang/terraform-30-days/azure/dev/southeastasia/chechia_net/kubernetes
Amount: +$232 ($0.00 -> $232)
----------------------------------
Key: ~ changed, + added, - removed
To estimate usage-based resources use --usage-file, see https://infracost.io/usage-file
infracost breakdown --path azure/dev/southeastasia/chechia_net/kubernetes
Detected Terragrunt directory at azure/dev/southeastasia/chechia_net/kubernetes
✔ Running terragrunt run-all terragrunt-info
✔ Running terragrunt run-all plan
✔ Running terragrunt show
✔ Calculating monthly cost estimate
Project: chechiachang/terraform-30-days/azure/dev/southeastasia/chechia_net/kubernetes
Name Monthly Qty Unit Monthly Cost
azurerm_kubernetes_cluster.main
├─ default_node_pool
│ ├─ Instance usage (pay as you go, Standard_D2_v2) 730 hours $115.34
│ └─ os_disk
│ └─ Storage (P1) 1 months $0.78
└─ Load Balancer
└─ Data processed Monthly cost depends on usage: $0.005 per GB
azurerm_kubernetes_cluster_node_pool.spot["spot"]
├─ Instance usage (pay as you go, Standard_D2_v2) 730 hours $115.34
└─ os_disk
└─ Storage (P1) 1 months $0.78
OVERALL TOTAL $232.24
----------------------------------
To estimate usage-based resources use --usage-file, see https://infracost.io/usage-file
這章就這麼單純,但蠻好用的,可以整合到 git-ops workflow 中