1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

terraform planの実行時間を短くするTIPS

1
Last updated at Posted at 2024-01-13

どーも、ogimanです。
最近、terraformを触る機会が多いです。
terraformを使い始めて思うこと・・・それは、terraform planの実行時間が長〜い!?

えっ!?一つのリソース変えただけなのに、まだ終わってないの?
えっ!?全然仕事が進まないのですけどー
えっ!?待ち時間に他のことやってしまいマルチタスクになって生産性落ちるんですけどー

terraformのディレクトリの構成が悪いのだとは思いますが、これにより開発の効率が非常に悪くなってます。

ということで、この問題を解決する方法をメモっときます。

目的

terraformの開発効率を上げる

誰向け

  • terrraformを普段使っている方
  • terraformの実行時間を短くしたい方

現状

terraformのリソースやtfファイルを修正と実装を繰り返しながら開発している。その度にterraform planやterraform applyを実行するが、1回の実行時間が1分以上掛かる

問題

1つ、2つのリソース変更を適用するだけなのにterraform全体に対してterraform planやterraform applyを実行している

対策

  • 1つもしくは複数のリソース単位でterraform plan、terraform applyを実行する
  • 1つのtfファイル単位でterraform plan、terraform applyを実行する

注意

  • コマンド例に記載している リソースタイプ にはterraformの各providerのリソースタイプを指定します。こちらから参照ください。
  • コマンド例に記載している リソース名 には任意のリソース名を指定してください。
  • 後述しているコマンド例は terraform plan を実行していますが、terraform apply を実行する場合はterraform planterraform apply に置き換えてください。

実際のコマンド

1つもしくは複数のリソース単位でterraform plan、terraform applyを実行する方法

# コマンド構文
## terraform plan -target={リソースタイプ.リソース名}
# 実行例
terraform plan -target={aws_iam_role.ogiman_ec2}

複数のリソースを指定してterraform plan、terraform applyを実行する方法

# コマンド構文
## terraform plan \
##   -target=リソースタイプ.リソース名 \
##   -target=リソースタイプ.リソース名
# 実行例
terraform plan \
  -target=aws_iam_role.ogiman_ec2 \
  -target=aws_iam_role.ogiman_alb

もしくは

# コマンド構文
## terraform plan -target={リソースタイプ(1).リソース名(1),リソースタイプ(2).リソース名(2)}
# 実行例
terraform plan -target={aws_iam_role.ogiman_ec2,aws_iam_role.ogiman_alb}

1つのtfファイル単位でterraform plan、terraform applyを実行する方法

# tfファイル名を変数へ設定
## tf="tfファイル名" 
tf="elb.tf"

# 以下のコマンドを実行して適用
terraform plan $(cat $tf | grep -E '^resource ' | tr -d '"' | awk '{printf("-target=%s.%s ",$2,$3);}')

以上です。
他にも効率的に開発刷る方法があればドシドシ情報お待ちしております。

ではではー

参考

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?