8
2

More than 3 years have passed since last update.

Terraform 0.12.x 特定のモジュール(リソース)を apply / plan する(公式ドキュメントとコマンドが違うやないかい)

Last updated at Posted at 2020-03-03

Terraformでファイルを変更して、ほかのリソースのアップデートまではしたくない場合に、[-target] オプションを使うと便利です。

Terraformのバージョンは下記のとおりです。
今回は、awsのサービスを利用したいため、プロバイダーはawsを使用しています。

Terraform> terraform -v
Terraform v0.12.18
+ provider.aws v2.51.0

ファイル構成は以下のようになっています。

Terraform  
 ├─main.tf
 ├─output.tf
 ├─provider.tf
 ├─terraform.tf
 ├─variables.tf
 └─modules/
     └─CodeBuild/
                ├─main.tf
                └─variables.tf 

テラフォームのエントリーポイントとなっている Terraform/main.tf に必要なモジュールをまとめて定義していて、 CodeBuildについての設定内容を読み込むための処理をしています。

main.tf
# CodeBuildのモジュール定義
module "CodeBuild" {
  source = "./modules/CodeBuild"
  account_id_number = data.aws_caller_identity.current.account_id 
}

今回は、CodeBuildに関しての設定を変更したので、このモジュールだけ更新したいと思います。

#target-resource(公式ドキュメント)を見ると

terraform plan -targert=resource

で特定のモジュールを指定すればいいと明記してあります。

リソースまでのパスの指定方法は
module.A.module.B.module.C... のように指定すればいいとあるので、

[module path] = module.CodeBuild

で大丈夫です。 と思って、コマンドを実行してみると、、、

$ terraform plan -target=module.CodeBuild
Usage: terraform plan [options] [DIR]

  Generates an execution plan for Terraform.

  This execution plan can be reviewed prior to running apply to get a
  sense for what Terraform will do. Optionally, the plan can be saved to
  a Terraform plan file, and apply can take this plan file to execute
  this plan exactly.

Options:
 ****
  -target=resource    Resource to target. Operation will be limited to this
                      resource and its dependencies. This flag can be used
                      multiple times.
 ****

オプションが認識されない!!!!!!
色々試してみた結果、下記コマンドで実行できました。

結論、公式ドキュメントとはコマンドが変わっています。

$ terraform plan -target module.CodeBuild

(イコール記号 '=' を省いてオプションとリソースパスの間にスペースを入れる)

公式ドキュメントにも、コマンドヘルプにも違った案内のままなので早く修正してほしいですね。

8
2
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
8
2