Day 29-给我无限多的预算我就能撑起全世界,infracost 教你吃米知米价

本篇介绍如何使用 infracost 工具估计 infrastructure apply 的花费

课程内容与代码会放在 Github 上: https://github.com/chechiachang/terraform-30-days

赛後文章会整理放到个人的部落格上 http://chechia.net/

追踪粉专可以收到文章的主动推播

https://ithelp.ithome.com.tw/upload/images/20210901/20120327NvpHVr2QC0.jpg


infratructure cost management

『给我无限多的预算,我就能撑起全世界』

然而,现实中不存在无限预算的专案,因此成本控管就非常重要。infrastructure 管理中很重要的一部份便是成本控管,以合理成本来架设 infrastructure 是云端管理必备技能。

为了做到成本控管,我们会需要预估一组 infrastructure 的未来花费,而不是等到 apply 上去後等下个月的帐单来(XD)

这是个教你吃米要知道米价的工具

Infracost

既然已经导入 terraform,我们完全可以预期一个 root module 会产生多少 infrastructure

  • 或是 plan 时候,获得新 infra 与旧 infra 的差异
  • 公有云的定价 pricing 讯息都是公开的,然而与其去网页查询,不如直接整合 public cloud pricing API,更准确
  • 整合 infrastructure diff + pricing,透过工具计算整体费用变化就十分容易

Installation

安装

  • 笔者习惯使用 release binary
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

版本要注意一下,建议用新版

  • v0.9.7 之後对於 terragrunt 的支援比较好
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.

Register

使用 infracost 需要注册 api key,执行下面命令注册即可

  • credential 会放在 local 路径
infracost register
cat ${HOME}/.config/infracost/credentials.yml

first run

算看看 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 上不支援计价

  • 事实上,这两个 resource 在 azure 上也是不计价的
  • 计价的是底下 blob storage 的用量

With Terragrunt

新版 infracost 会自动侦测 terragrunt 并使用 terragrunt 的设定,产生 .tf 後才会计算 cost,细节请见infracost官分说明文件

  • 注意记得使用 v0.9.7 以後的版本
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

Breakdown

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】连续上班日做便当3

>>:  DAY14 Kotlin基础 Class 2!兼第二周回顾

Day15 Pseudo Element 眼见不一定为真

什麽是伪元素(Pseudo Element)? 伪元素就如同它的名字一样,不是一个实际存在於网页里...

Day 30 Sniffing & Spoofing 监听与欺骗 (macchanger)

今天要体验的工具是macchanger,从介绍就一目了然其用途是拿来进行MAC地址欺骗,可以暂时修改...

【领域展开 02 式】 拥有个人网站的 3 大理由

平台就是你的资本和机会,在这数位时代,你不定义自己,别人会自动为你贴标签。 这是来自数位形象力这本书...

[Day17] - 在 Vue 中引入现成的 Web Component

当我们拿到一个现有的 Web Component 时 , 如果直接在 Vue 专案中使用会抱错 今天...

为了转生而点技能-JavaScript,day6(初探物件构造及物件取值

物件(Object) 构造:物件就是多种属性的集合,里面有属性及值,其中属性只能是字串,增加属性利用...