1
0

More than 1 year has passed since last update.

Terraformで特定のResourceのみを取り扱う

Last updated at Posted at 2021-11-15

はじめに

これまでの検証はこのように対象別にフォルダを分けて行っていました。
そのフォルダごとにinit、plan、applyを実行していました。
さらに面倒なのがterraform.tfvarsとvariables.tfは同じもの。
各フォルダにコピーしていました。

   └terraform
     ├ec2
     │  ├terraform.tfvars
     │  ├variables.tf
     │  └ec2.tf
     ├ec2ssm
     │  ├terraform.tfvars
     │  ├variables.tf
     │  ├ec2.tf
     │  └role.tf
     └s3
        ├terraform.tfvars
        ├variables.tf
        └s3.tf

でも、こういう構成のほうが管理はしやすいですし、作業フォルダも一箇所で済みます。

   └terraform
        ├terraform.tfvars
        ├variables.tf
        ├ec2.tf
        ├ec2.tf
        ├role.tf
        └s3.tf

terraformのオプションでなにかないのか、探してみたところ「-terget」オプションが使えそうです。

上記のフォルダ構成、init済みを前提に早速検証してみます。

参考

初めてのTerraform
SSMでEC2にアクセス
TerraformでS3バケットを登録
Command: plan
Terraformでtarget指定の削除

手順

# terraform plan -target=s3.tf

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration and found no differences, so no changes
are needed.
╷
│ Warning: Resource targeting is in effect
│
│ You are creating a plan with the -target option, which means that the result of this plan may not represent all of
│ the changes requested by the current configuration.
│
│ The -target option is not for routine use, and is provided only for exceptional situations such as recovering from
│ errors or mistakes, or when Terraform specifically suggests to use it as part of an error message.

エラーになってしまいました...
調べたところ、Targetにはファイルを指定するのではなく、Resourceを指定するようです。
であれば今度はこのコマンドで検証してみます。

まずはS3。
s3.tfに記載のある「resource」を指定してみます。

resource "aws_s3_bucket" "bucket1" {
    bucket = "xxxxx-tf-bucket"
    acl = "private"
}
# terraform plan -target=aws_s3_bucket.bucket1

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with
the following symbols:
  + create
  :
  :
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.

無事にplanが実行できました。
念の為、EC2も試してみます。
同じように、ec2.tfに記載のある「Resource」を指定してみます。

resource "aws_instance" "xxxxx_tf-ec2" {
  count         = 1
  ami           = "ami-03d5c68bab01f3496" # Ubuntu 20.04 LTS official ami
  instance_type = "t2.micro"
  iam_instance_profile = aws_iam_instance_profile.systems_manager.name
  tags = {
    Name = "${format("xxxxx_tf-ec2-%02d", count.index + 1)}"
  }
}

こちらも無事にplanが実行できました。

# terraform plan -target=aws_instance.xxxxx_tf-ec2

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with
the following symbols:
  + create
  :
  :
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.

plan同様にapplyもdestroyも実行可能です。

おわりに

この検証がどういうときに役に立つか。
例えばですが、デプロイ対象のResourceがたくあんある場合です。
特定のResourceのみ実行できれば実行時間の短縮に繋がります。
planについても既に確認済みのResourceは実行不要、特定のResourceだけ確認できればいい、そういう場合に役立ちそうです。

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