2. インストール

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

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
    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:
    0.12upgrade        Rewrites pre-0.12 module source code for v0.12
    debug              Debug output management (experimental)
    force-unlock       Manually unlock the terraform state
    push               Obsolete command for Terraform Enterprise legacy (v1)
    state              Advanced state management

2.2. 動作確認

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

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

ここでは例として、 FIC-Port をTerrraform経由で実際に作成/削除してみます。

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

% mkdir work
% cd work

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

terraform {
  required_providers {
    fic = {
      source = "nttcom/fic"
      version = "0.5.3"
    }
  }
}

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

resource "fic_eri_port_v1" "port_1" {
    name = "terraform_port_1"
    switch_name = "[Switch名]"
    port_type = "10G"
    number_of_vlans = 16
    is_activated = true
}

terraform init を実行します。

% terraform init

Initializing the backend...

Initializing provider plugins...
- Finding nttcom/fic versions matching "0.5.3"...
- Installing nttcom/fic v0.5.3...
- Installed nttcom/fic v0.5.3 (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/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 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:

  # fic_eri_port_v1.port_1 will be created
  + resource "fic_eri_port_v1" "port_1" {
      + area            = (known after apply)
      + id              = (known after apply)
      + is_activated    = true
      + location        = (known after apply)
      + name            = "terraform_port_1"
      + number_of_vlans = 16
      + port_type       = "10G"
      + switch_name     = "[Switch名]"
      + tenant_id       = (known after apply)
      + vlans           = (known after apply)

      + vlan_ranges {
          + end   = (known after apply)
          + start = (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

fic_eri_port_v1.port_1: Creating...
fic_eri_port_v1.port_1: Still creating... [10s elapsed]
fic_eri_port_v1.port_1: Still creating... [20s elapsed]
fic_eri_port_v1.port_1: Still creating... [30s elapsed]
fic_eri_port_v1.port_1: Still creating... [40s elapsed]
fic_eri_port_v1.port_1: Still creating... [50s elapsed]
fic_eri_port_v1.port_1: Still creating... [1m0s elapsed]
fic_eri_port_v1.port_1: Still creating... [1m10s elapsed]
fic_eri_port_v1.port_1: Still creating... [1m20s elapsed]
fic_eri_port_v1.port_1: Creation complete after 1m26s [id=F012000000312]

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

Flexible InterConnect のFICコンソールなどから実際にFIC-Portが作成されていることを確認します。

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

% terraform destroy
fic_eri_port_v1.port_1: Refreshing state... [id=F012000000316]

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:

  # fic_eri_port_v1.port_1 will be destroyed
  - resource "fic_eri_port_v1" "port_1" {
      - area            = "JPEAST" -> null
      - id              = "F012000000316" -> null
      - is_activated    = true -> null
      - location        = "NTTComOsaka(Dojima#1)" -> null
      - name            = "terraform_port_1" -> null
      - number_of_vlans = 16 -> null
      - port_type       = "10G" -> null
      - switch_name     = "[Switch名]" -> null
      - tenant_id       = "75ece6039bfd4d2f88c8b8d790482886" -> null
      - vlans           = [
          - {
              - status = "unused"
              - vid    = 257
            },
          - {
              - status = "unused"
              - vid    = 258
            },
          - {
              - status = "unused"
              - vid    = 259
            },
          - {
              - status = "unused"
              - vid    = 260
            },
          - {
              - status = "unused"
              - vid    = 261
            },
          - {
              - status = "unused"
              - vid    = 262
            },
          - {
              - status = "unused"
              - vid    = 263
            },
          - {
              - status = "unused"
              - vid    = 264
            },
          - {
              - status = "unused"
              - vid    = 265
            },
          - {
              - status = "unused"
              - vid    = 266
            },
          - {
              - status = "unused"
              - vid    = 267
            },
          - {
              - status = "unused"
              - vid    = 268
            },
          - {
              - status = "unused"
              - vid    = 269
            },
          - {
              - status = "unused"
              - vid    = 270
            },
          - {
              - status = "unused"
              - vid    = 271
            },
          - {
              - status = "unused"
              - vid    = 272
            },
        ] -> 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

fic_eri_port_v1.port_1: Destroying... [id=F012000000312]
fic_eri_port_v1.port_1: Still destroying... [id=F012000000312, 10s elapsed]
fic_eri_port_v1.port_1: Still destroying... [id=F012000000312, 20s elapsed]
fic_eri_port_v1.port_1: Still destroying... [id=F012000000312, 30s elapsed]
fic_eri_port_v1.port_1: Still destroying... [id=F012000000312, 40s elapsed]
fic_eri_port_v1.port_1: Destruction complete after 43s

Destroy complete! Resources: 1 destroyed.

Flexible InterConnect のFICコンソールなどから実際にFIC-Portが削除されていることを確認します。FIC-Portが削除されていれば動作確認は完了です。