本篇介绍如何使用 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 中
>>: DAY14 Kotlin基础 Class 2!兼第二周回顾
什麽是伪元素(Pseudo Element)? 伪元素就如同它的名字一样,不是一个实际存在於网页里...
今天要体验的工具是macchanger,从介绍就一目了然其用途是拿来进行MAC地址欺骗,可以暂时修改...
平台就是你的资本和机会,在这数位时代,你不定义自己,别人会自动为你贴标签。 这是来自数位形象力这本书...
当我们拿到一个现有的 Web Component 时 , 如果直接在 Vue 专案中使用会抱错 今天...
物件(Object) 构造:物件就是多种属性的集合,里面有属性及值,其中属性只能是字串,增加属性利用...