私はインフラコストの算出がとても苦手です。AWSならAWS Pricing Calculator、Google CloudならGoogle Cloud Calculatorを使用すればいいんでしょうが、入力項目は多いですし、構成を変更するたびに同じ項目を入力しなくてはいけなくて嫌になってきてしまいます。Terraform CloudにはTerraformのコードからコストを算出するCost Estimation機能があるんですが有料オプションです。そこで無料で使えて、かつ、CLI上から確認できるinfracostというツールを紹介します。
Infracost ???
InfracostはTerraformのコードから料金を計算してくれるサービスです。InfracostにはInfracost CI/CD、Infracost Cloud、Infracost Enterpriseと3つのプランがあり、CloudとEnterpriseは料金がかかってしまうのですが、CI/CDは無料で使用できます。
それぞれのプランで何ができるかはこちらを参考にしてください。
セットアップ
CI/CDプランを使用するにはInfracostのサービスにアカウントを用意する必要があります。こちらからSign Upを済ませておいてください。
私はGitHubアカウントでログインしました。
続いてinfracostコマンドをインストールします。それぞれのOS毎にインストールドキュメントが用意されているので、ご自身の環境に合わせてセットアップしてください。
コマンドのインストールが終わったら以下のコマンドを実行してAPIキーを取得します。以下のコマンドを実行するとブラウザが開いてログイン画面が出てくるのでログインします。
infracost auth login
ログインが完了するとコマンドが完了し~/.config/infracost/credentials.ymlというファイル名でクレデンシャルファイルが生成されます。
このクレデンシャルはCloud Pricing APIから料金を取得するために使ってるみたいです。
Register for a free API key, which is used by the CLI to retrieve prices from our Cloud Pricing API, e.g. get prices for instance types.
https://www.infracost.io/docs/cloud_pricing_api/overview/
ここまでエラーが出ていなければセットアップは完了です。
使い方
今回はこちらのサンプルコードに対してコスト算出を行なってみます。
Terraformのコードがあるディレクトリに移動してinfracost breakdownコマンドを実行してみましょう。
$ infracost breakdown --path .
Evaluating Terraform directory at .
✔ Downloading Terraform modules
✔ Evaluating Terraform directory
✔ Retrieving cloud prices to calculate costs
Project: .
Name Monthly Qty Unit Monthly Cost
aws_instance.private
├─ Instance usage (Linux/UNIX, on-demand, t3.micro) 730 hours $7.59
└─ root_block_device
└─ Storage (general purpose SSD, gp2) 8 GB $0.80
aws_instance.public
├─ Instance usage (Linux/UNIX, on-demand, t3.micro) 730 hours $7.59
└─ root_block_device
└─ Storage (general purpose SSD, gp2) 8 GB $0.80
aws_nat_gateway.private
├─ NAT gateway 730 hours $32.85
└─ Data processed Monthly cost depends on usage: $0.045 per GB
OVERALL TOTAL $49.63
──────────────────────────────────
140 cloud resources were detected:
∙ 3 were estimated, all of which include usage-based costs, see https://infracost.io/usage-file
∙ 137 were free, rerun with --show-skipped to see details
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓
┃ Project ┃ Monthly cost ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━┫
┃ . ┃ $50 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━┛
サンプルの構成で月$50かかることがわかりました。
では、パブリックサブネットに紐づくインスタンスのタイプをt3.microからt3.mediumに変更してみます。
まずは今の状態をinfracost-base.jsonというファイルに保存します。
$ infracost breakdown --path . --format json --out-file infracost-base.json Evaluating Terraform directory at . ✔ Downloading Terraform modules ✔ Evaluating Terraform directory ✔ Retrieving cloud prices to calculate costs Output saved to infracost-base.json
infracost-base.jsonというファイル名はわかりやすいものであればなんでもOKです。
続いてmain.tfのaws_instance.publicのインスタンスタイプをt3.microからt3.mediumに変更してinfracost breakdownを実行してみます。
$ infracost breakdown --path .
Evaluating Terraform directory at .
✔ Downloading Terraform modules
✔ Evaluating Terraform directory
✔ Retrieving cloud prices to calculate costs
Project: .
Name Monthly Qty Unit Monthly Cost
aws_instance.private
├─ Instance usage (Linux/UNIX, on-demand, t3.micro) 730 hours $7.59
└─ root_block_device
└─ Storage (general purpose SSD, gp2) 8 GB $0.80
aws_instance.public
├─ Instance usage (Linux/UNIX, on-demand, t3.medium) 730 hours $30.37
└─ root_block_device
└─ Storage (general purpose SSD, gp2) 8 GB $0.80
aws_nat_gateway.private
├─ NAT gateway 730 hours $32.85
└─ Data processed Monthly cost depends on usage: $0.045 per GB
OVERALL TOTAL $72.41
──────────────────────────────────
140 cloud resources were detected:
∙ 3 were estimated, all of which include usage-based costs, see https://infracost.io/usage-file
∙ 137 were free, rerun with --show-skipped to see details
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓
┃ Project ┃ Monthly cost ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━┫
┃ . ┃ $72 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━┛
合計の料金が変わったのが確認できました。
infracostにはdiffコマンドが用意されているので変更前と後でどれくらいコストアップしたのか確認してみます。
$ infracost diff --path . --compare-to infracost-base.json
Evaluating Terraform directory at .
✔ Downloading Terraform modules
✔ Evaluating Terraform directory
✔ Retrieving cloud prices to calculate costs
──────────────────────────────────
Project: .
~ aws_instance.public
+$23 ($8 → $31)
~ Instance usage (Linux/UNIX, on-demand, t3.micro → t3.medium)
+$23 ($8 → $30)
Monthly cost change for .
Amount: +$23 ($50 → $72)
Percent: +46%
──────────────────────────────────
Key: ~ changed, + added, - removed
140 cloud resources were detected:
∙ 3 were estimated, all of which include usage-based costs, see https://infracost.io/usage-file
∙ 137 were free, rerun with --show-skipped to see details
Infracost estimate: Monthly cost will increase by $23 ↑
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┓
┃ Project ┃ Cost change ┃ New monthly cost ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━┫
┃ . ┃ +$23 (+46%) ┃ $72 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━┛
$23コストが上がるようです。
まとめ
構成からインフラのコストを簡単に算出できました。CI/CDというプランを使っていることからわかるように、CI/CDと連携して使うのがよさそうですね。主要なCI/CDサービスはサポートされているようです。
VS Codeのエクステンションなども用意されているようなので、コマンドが苦手な方でも簡単に試せるのではないでしょうか。
