terraform planterraform plan は、Terraform のコードと今の状態を比べて、適用(apply)したら何が作られ、何が変わり、何が消えるのかを事前に表示するコマンドです。この時点では何も変更されません。terraform apply の前に必ず打ち、意図しない削除が混ざっていないかを確かめるために使います。
terraform plan の書式
terraform plan [オプション]- 引数はふつう付けません。今いるディレクトリの
.tfファイルが読まれます - AWS などのプロバイダを使うコードでは、先に
terraform initを打っておく必要があります
よく使うオプション
terraform plan -help の説明をもとにまとめました。
| オプション | 意味 |
|---|---|
-out=ファイル名 | 計画をファイルに保存する。terraform apply ファイル名 で、その計画どおりに適用できる |
-var 'name=value' | 変数の値を指定する |
-var-file=ファイル名 | 変数の値をファイルから読む |
-target=リソース | 計画を特定のリソースだけに絞る |
-destroy | すべてを削除する計画を作る |
-detailed-exitcode | 変更が無ければ 0、変更があれば 2 で終わる(スクリプトで使う) |
terraform plan の実行例
macOS 27.2 上の Terraform v1.14.0 で確認しました。外部とつながない組み込みの terraform_data リソースを2つ書いた main.tf です。
resource "terraform_data" "web" {
input = "web-01"
}
resource "terraform_data" "db" {
input = "db-01"
}まだ何も作っていない状態で plan を打つと、2つとも +(作成)になります。
$ terraform plan
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# terraform_data.db will be created
+ resource "terraform_data" "db" {
+ id = (known after apply)
+ input = "db-01"
+ output = (known after apply)
}
# terraform_data.web will be created
+ resource "terraform_data" "web" {
+ id = (known after apply)
+ input = "web-01"
+ output = (known after apply)
}
Plan: 2 to add, 0 to change, 0 to destroy.
─────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.(known after apply) は「作ってみるまで値が決まらない」という意味です。一度 apply したあと、web の値を web-02 に変え、db を消して cache を足すと、3種類の記号がそろいます。
$ terraform plan
terraform_data.web: Refreshing state... [id=fe989055-1ab5-d1c1-4f48-62971a59d142]
terraform_data.db: Refreshing state... [id=120d7231-beb3-ebd6-1bce-38675d436a3c]
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
+ create
~ update in-place
- destroy
Terraform will perform the following actions:
# terraform_data.cache will be created
+ resource "terraform_data" "cache" {
+ id = (known after apply)
+ input = "cache-01"
+ output = (known after apply)
}
# terraform_data.db will be destroyed
# (because terraform_data.db is not in configuration)
- resource "terraform_data" "db" {
- id = "120d7231-beb3-ebd6-1bce-38675d436a3c" -> null
- input = "db-01" -> null
- output = "db-01" -> null
}
# terraform_data.web will be updated in-place
~ resource "terraform_data" "web" {
id = "fe989055-1ab5-d1c1-4f48-62971a59d142"
~ input = "web-01" -> "web-02"
~ output = "web-01" -> (known after apply)
}
Plan: 1 to add, 1 to change, 1 to destroy.
─────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.記号の意味は次のとおりです。
| 記号 | 意味 |
|---|---|
+ | 新しく作る(create) |
~ | 今あるものをその場で変更する(update in-place) |
- | 削除する(destroy) |
-/+ | いったん削除して作り直す(destroy and then create replacement) |
まず最後の Plan: 1 to add, 1 to change, 1 to destroy. の行を見ます。destroy が 0 でないときや、-/+ の行があるときは、今あるものが消えるので、どのリソースかを必ず確かめます。
terraform plan -out=tfplan のように -out を付けると、計画がファイルに保存され、表示の最後の案内が次のように変わります。
Saved the plan to: tfplan
To perform exactly these actions, run the following command to apply:
terraform apply "tfplan"よくあるエラーと対処
.tf ファイルが無いディレクトリで打つと、次のエラーになります。main.tf などがあるディレクトリへ cd してから打ち直します。
$ terraform plan
Error: No configuration files
Plan requires configuration to be present. Planning without a configuration
would mark everything for destruction, which is normally not what is desired.
If you would like to destroy everything, run plan with the -destroy option.
Otherwise, create a Terraform configuration file (.tf file) and try again.保存した計画ファイルを、別の apply のあとにもう一度使おうとすると、古い計画として断られます。もう一度 terraform plan -out=tfplan から作り直します。
$ terraform apply tfplan
Error: Saved plan is stale
The given plan file can no longer be applied because the state was changed by
another operation after the plan was created.terraform apply・validate との違い
| コマンド | すること |
|---|---|
terraform validate | コードの書き方と中身のつじつまだけを確かめる。今の状態とは比べない |
terraform plan | 今の状態と比べて、変更の計画を表示する。何も変えない |
terraform apply | 計画を表示したうえで、yes と答えると実際に変更する |
チームでは、plan の結果をレビューしてから apply する流れがよく使われます。Terraform そのものの考え方は Terraformとは?で、学ぶ順番はインフラエンジニアの学習ロードマップで説明しています。
よくある質問
terraform plan を打つだけで、クラウドの設定が変わることはありますか?
plan は計画を表示するだけで、リソースを作ったり消したりはしません。ただし、今の状態を確かめるためにクラウドへの読み取りは行います。
-out を付けないとどうなりますか?
plan の表示だけで終わります。そのあと apply を打つと、その時点でもう一度計画が作られるため、plan で見た内容と少し違う可能性があります。確実に同じ内容で適用したいときは -out で保存したファイルを apply に渡します。
変更が無いときは何と表示されますか?
コードと今の状態が同じなら、No changes. と表示され、作成・変更・削除のどれも行われません。
