Terraform でサクッと Cloud Data Fusion インスタンスを作成してみた

2022.07.07

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは!エノカワです。

ここ最近、Cloud Data Fusion に触る機会が増えた者です。

検証時にインスタンスを作成して終わったら削除する、といった使い方をしているのですが、
毎回コンソール上で行っていたので、もっと楽にできる方法は無いものかと思っていました。

ふと「Terraform でサクッと Cloud Data Fusion インスタンスを作成できないかな?」と思い立ち、 やってみたら驚くほど簡単にできちゃったので、今回はその方法をご紹介します。

準備

コンソールメニューからData Fusionを選択します。
Cloud Data Fusion APIが有効になっていれば、インスタンスページが表示されます。

Cloud Data Fusion APIが有効になっていない場合は、有効にするボタンをクリックしてAPIを有効にします。

インスタンスページが表示されました。
まだ何も作成されていない状態です。

はい、準備はこれだけです!
あとは Terraform の出番です。

Cloud Shell 起動

Terraform コマンドの実行は Cloud Shell 上で行うのですが、
Google Cloud Platform Provider のドキュメント から Cloud Shell を起動します。

ここが今回のサクッとポイントになります!

google_data_fusion_instance のドキュメント を覗いてみると、
Cloud Data Fusion インスタンス作成の使用例が載っています。

Data Fusion Instance Fullの使用例では、
各種プロパティを指定したインスタンス作成ができるようです。

今回はこの使用例のままインスタンスを作成してみましょう。

使用例の右上の「OPEN IN GOOGLE CLOUD SHELL」をクリックします。

すると、Cloud Shell 起動ダイアログが表示されるので、「確認」をクリックします。

Cloud Shell のプロビジョニングが始まります。

Cloud Shell が起動しました!

起動ダイアログにあった通り、ドキュメントサンプルのリポジトリがクローンされた状態でエフェメラルモードの Cloud Shell が起動しています。

先ほど見たData Fusion Instance Fullの Terraform 構成ファイルが開かれた状態になっています。

ここから Terraform コマンドの実行に入っていきます。

terraform init

Terraform を実行するためには、はじめにterraform initでワークスペースを初期化することが必須となっています。
ですが、Terraformは環境変数からプロジェクトIDを取得するため、下記コマンドを実行しておきます。

$ export GOOGLE_CLOUD_PROJECT={プロジェクトID}

つづけて、terraform initコマンドを実行します。

$ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/google...
- Finding latest version of hashicorp/random...
- Installing hashicorp/google v4.27.0...
- Installed hashicorp/google v4.27.0 (self-signed, key ID 34365D9472D7468F)
- Installing hashicorp/random v3.3.2...
- Installed hashicorp/random v3.3.2 (self-signed, key ID 34365D9472D7468F)

Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

ワークスペースに必要なプラグインがダウンロードされ、Terraform の準備が整いました!

terraform apply

それでは、インスタンス作成を実行しましょう。

下記コマンドを実行します。

$ terraform apply

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:

  # google_data_fusion_instance.extended_instance will be created
  + resource "google_data_fusion_instance" "extended_instance" {
      + create_time                   = (known after apply)
      + dataproc_service_account      = "{プロジェクトID}@appspot.gserviceaccount.com"
      + description                   = "My Data Fusion instance"
      + enable_stackdriver_logging    = true
      + enable_stackdriver_monitoring = true
      + gcs_bucket                    = (known after apply)
      + id                            = (known after apply)
      + labels                        = {
          + "example_key" = "example_value"
        }
      + name                          = (known after apply)
      + private_instance              = true
      + project                       = (known after apply)
      + region                        = "us-central1"
      + service_endpoint              = (known after apply)
      + state                         = (known after apply)
      + state_message                 = (known after apply)
      + tenant_project_id             = (known after apply)
      + type                          = "BASIC"
      + update_time                   = (known after apply)
      + version                       = "6.3.0"

      + network_config {
          + ip_allocation = "10.89.48.0/22"
          + network       = "default"
        }
    }

  # random_pet.suffix will be created
  + resource "random_pet" "suffix" {
      + id        = (known after apply)
      + length    = 2
      + separator = "-"
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value:

Terraform の実行予定が表示されます。
※一部 {プロジェクトID} で値をマスクしています。

実行を受け入れる場合は、yesと入力します。

入力すると、インスタンス作成が始まります。

インスタンスページを覗いてみると、作成中になっているのが確認できます!


15分後。
作成が終わったようです。

インスタンスページを覗いてみると、インスタンス作成できています!

インスタンス名をクリックして詳細を見てみましょう。

Data Fusion Instance Fullの使用例の通りに作成されています。

terraform destroy

最後に Terraform が作成したインスタンスを削除します。

下記コマンドを実行します。

$ terraform destroy
random_pet.suffix: Refreshing state... [id=known-crow]
google_data_fusion_instance.extended_instance: Refreshing state... [id=projects/{プロジェクトID}/locations/us-central1/instances/my-instance-known-crow]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # google_data_fusion_instance.extended_instance will be destroyed
  - resource "google_data_fusion_instance" "extended_instance" {
      - create_time                   = "2022-07-07T01:55:41.958559300Z" -> null
      - dataproc_service_account      = "{プロジェクトID}@appspot.gserviceaccount.com" -> null
      - description                   = "My Data Fusion instance" -> null
      - enable_stackdriver_logging    = true -> null
      - enable_stackdriver_monitoring = true -> null
      - gcs_bucket                    = "gs://df-8723234802641863852-beytngx5tei6zaxxaizbbqaaaa" -> null
      - id                            = "projects/{プロジェクトID}/locations/us-central1/instances/my-instance-known-crow" -> null
      - labels                        = {
          - "example_key" = "example_value"
        } -> null
      - name                          = "my-instance-known-crow" -> null
      - options                       = {} -> null
      - private_instance              = true -> null
      - project                       = "{プロジェクトID}" -> null
      - region                        = "us-central1" -> null
      - service_endpoint              = "https://my-instance-known-crow-{プロジェクトID}-dot-usc1.datafusion.googleusercontent.com" -> null
      - state                         = "ACTIVE" -> null
      - tenant_project_id             = "f7ae3e8bb446d28b8-tp" -> null
      - type                          = "BASIC" -> null
      - update_time                   = "2022-07-07T02:10:34.917566173Z" -> null
      - version                       = "6.3.0" -> null

      - network_config {
          - ip_allocation = "10.89.48.0/22" -> null
          - network       = "default" -> null
        }
    }

  # random_pet.suffix will be destroyed
  - resource "random_pet" "suffix" {
      - id        = "known-crow" -> null
      - length    = 2 -> null
      - separator = "-" -> null
    }

Plan: 0 to add, 0 to change, 2 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value:

Terraform の実行予定が表示されます。
※一部 {プロジェクトID} で値をマスクしています。

実行を受け入れる場合は、yesと入力します。

入力すると、インスタンス削除が始まります。

インスタンスページを覗いてみると、削除中になっているのが確認できます!


しばらくすると削除が完了し、インスタンスが無くなりました。  

まとめ

以上、Terraform でサクッと Cloud Data Fusion インスタンスを作成する方法のご紹介でした。

当初、Terraform 構成ファイルを組み立てる必要があるのかと思っていましたが、
Google Cloud Platform Provider のドキュメントから Cloud Shell を利用することで簡単にインスタンスを作成することができました。

Cloud Data Fusion 以外にもサンプルが用意されているので、
Terraform で Google Cloud のリソースを 作成する際の取っ掛かりとして活用できるのではないでしょうか。

参考