2. インストール

Terraform 本体、および Terraform Custom Provider for Smart Data Platform のインストール方法についてご説明します。

2.1. Terraform 本体のインストール

公式サイトの、 Installing Terraform に従いインストールをお願いします。

手順の概要としては以下の通りです。

  1. こちら からTerraformのバイナリ(zip圧縮)をダウンロードします
  2. zipファイルを解凍し、 terraform ファイル(Windowsの場合は terraform.exe )を任意のディレクトリにコピーします
  3. 上記2のディレクトリにパスを通します。パスの設定については、上記 Installing Terraform をあわせてご参照ください
  4. terraformコマンドを実行していただき、以下のような表示がされればインストール成功です。
% terraform
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
    destroy            Destroy Terraform-managed infrastructure
    env                Workspace management
    fmt                Rewrites config files to canonical format
    get                Download and install modules for the configuration
    graph              Create a visual graph of Terraform resources
    import             Import existing infrastructure into Terraform
    init               Initialize a Terraform working directory
    output             Read an output from a state file
    plan               Generate and show an execution plan
    providers          Prints a tree of the providers used in the configuration
    push               Upload this Terraform module to Atlas to run
    refresh            Update local state file against real resources
    show               Inspect Terraform state or plan
    taint              Manually mark a resource for recreation
    untaint            Manually unmark a resource as tainted
    validate           Validates the Terraform files
    version            Prints the Terraform version
    workspace          Workspace management

All other commands:
    debug              Debug output management (experimental)
    force-unlock       Manually unlock the terraform state
    state              Advanced state management

2.2. 動作確認

※以下手順は、Terraform 0.13.0 以降を対象にしています

Terraform Custom Provider for Smart Data Platform により、正しく Smart Data Platform 上にリソースの作成が可能かどうかを確認します。

ここでは例として、 サーバーインスタンスのキーペア をTerraform経由で実際に作成/削除してみます。

最初に作業用ディレクトリを作成します。以降の作業はすべてこのディレクトリにて実施します。

% mkdir work
% cd work

作成した作業ディレクトリに以下の内容を、拡張子が .tf (例: sample.tf)となるように保存します。

terraform {
  required_providers {
    ecl = {
      source = "nttcom/ecl"
      version = "1.11.2"
    }
  }
}

provider "ecl" {
    auth_url          = "https://keystone-[リージョン]-ecl.api.ntt.com/v3/"
    user_name         = "[API鍵]"
    password          = "[API秘密鍵]"
    tenant_id         = "[テナントID]"
    user_domain_id    = "default"
    project_domain_id = "default"
}

resource "ecl_compute_keypair_v2" "kp_1" {
    name = "keypair-1"
}

terraform init を実行します。

% terraform init

Initializing provider plugins...
- Reusing previous version of nttcom/ecl from the dependency lock file
- Installing nttcom/ecl v1.11.2...
- Installed nttcom/ecl v1.11.2 (signed by a HashiCorp partner, key ID ****************)

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/plugins/signing.html

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 apply を実行します。

% terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # ecl_compute_keypair_v2.kp_1 will be created
  + resource "ecl_compute_keypair_v2" "kp_1" {
      + fingerprint = (known after apply)
      + id          = (known after apply)
      + name:       = "keypair-1"
      + private_key = (known after apply)
      + public_key  = (known after apply)
      + region      = (known after apply)
    }

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

yes と入力しリソースの作成を開始します。

  Enter a value: yes

ecl_compute_keypair_v2.kp_1: Creating...
ecl_compute_keypair_v2.kp_1: Creation complete after *s [id=keypair-1]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Smart Data Platform のポータルなどから実際にキーペアが作成されていることを確認します。

問題なく作成されているようでしたら、次に terraform destroy により作成されたリソースを削除します。

% terraform destroy

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # ecl_compute_keypair_v2.kp_1 will be destroyed
  - resource "ecl_compute_keypair_v2" "kp_1" {
      - fingerprint = "*********" -> null
      - id          = "keypair-1" -> null
      - name        = "keypair-1" -> null
      - private_key = <<-EOT
            -----BEGIN RSA PRIVATE KEY-----
            *********************************************************************
            *********************************************************************
            *********************************************************************
            -----END RSA PRIVATE KEY-----
        EOT -> null
      - public_key  = "**************************" -> null
    }

Plan: 0 to add, 0 to change, 1 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:

yes と入力しリソースの削除を開始します。

  Enter a value: yes

ecl_compute_keypair_v2.kp_1: Destroying... [id=keypair-1]
ecl_compute_keypair_v2.kp_1: Destruction complete after *s

Destroy complete! Resources: 1 destroyed.

Smart Data Platform のポータルなどから実際にキーペアが削除されていることを確認します。キーペアが削除されていれば動作確認は完了です。