LoginSignup
80
30

More than 5 years have passed since last update.

あなたのterraform planを手軽に高速化する(かもしれない)魔法の言葉

Last updated at Posted at 2018-10-26

terraformで管理しているリソースが増えてきて、tfstateが肥大化してくると、
terraform planがどんどん遅くなり、待ってる間にtwitterを見てしまい、、、あれ?結局何をしていたのか忘れる。あるあるです。

そんな場合の根本的な対策は、適度な粒度にtfstateを分割していくんですけど、tfstateをいじるのはできなくはないけど、ちょっとめんどくさいです。

が、もっと手軽に高速化できる(かもしれない)小手先のテクニックがあります。

その魔法の言葉とは --parallelism です。デフォルトは10並列で動いてますが、変更可能です。
呼び出すAPIやネットワーク構成などに依存するので、最適な値はケースバイケースですが、
一般的に扱うリソースが多い場合は、並列度を上げると高速化が期待できそうです。

まずは比較のために、素の状態の実行時間を計測しておきます。

$ time terraform plan

terraform plan  14.48s user 4.79s system 27% cpu 1:09.17 total

1分ぐらいかかってますね。並列度を30にあげてみると、

$ time terraform plan --parallelism=30

terraform plan --parallelism=30  13.82s user 4.10s system 57% cpu 30.962 total

なんと、30秒ぐらいで終わりました。たったこれだけで2倍高速化できました。わーい。

毎回指定するのはめんどくさいので、環境変数 TF_CLI_ARGS_plan に設定できます。

$ export TF_CLI_ARGS_plan="--parallelism=30"
$ time terraform plan

terraform plan  13.88s user 3.98s system 56% cpu 31.868 total

環境変数でも効いてますね。
applyの場合は、環境変数 TF_CLI_ARGS_apply です。

どれぐらいの並列度が最適なのかは場合によりけりなのだけど、めっちゃ簡単なので試してみて損はないですね。

たまたまTerraform のissueを眺めてたら見つけたんだけど、
別に魔法の言葉でもなんでもなくて、普通に公式ドキュメントに書いてありましたね。
普通にコマンドのヘルプにも出てるし、なんで今までこれ気づかなかったのか。

参考:
- https://github.com/hashicorp/terraform/issues/18974
- https://www.terraform.io/docs/commands/plan.html
- https://www.terraform.io/docs/configuration/environment-variables.html#tf_cli_args-and-tf_cli_args_name

80
30
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
80
30