Local PC から Terraform で IBM Cloud の Power Systems Virtual Server にAIXサーバー をデプロイする

Local PC(Mac OS) から IBM Cloud の Power Systems Vitrual Server サービスに AIX サーバー をデプロイした記録です。

* 一部の固有値はマスクしています。


IBM Power Systems Virtual Server on IBM Cloud については [IBM Cloud資料](https://cloud.ibm.com/docs/infrastructure/power-iaas?topic=power-iaas-about-power-virtual-server&locale=ja) を参照ください。 


>IBM® Power Systems™ Virtual Server on IBM Cloud は、既存の IBM Cloud プラットフォームを使用して、PowerVM ハイパーバイザーを備えた IBM Power Systems ハードウェア上で稼働する IBM® Power Systems™ Virtual Server on IBM Cloud (論理区画 (LPAR) とも呼ばれる) を作成します。




#### 前提環境

- IBM Power Systems Vitrual Server on IBM Cloud サービス・デプロイ済
- IBM Cloud で Direct Link のオーダー済
  (→ Private Networkが必要な場合に設定します。AIX が Public IPのみを使用する場合は不要)
- (private ip を使用する場合) プライベート・サブネット設定済


- Local PC： Mac OS, Movaje 
　導入/使用モジュール
  - [Terraform](https://www.terraform.io/downloads.html) : v0.12.23
  - Terraform Provider [terraform-provider-ibm v1.2.0](https://github.com/IBM-Cloud/terraform-provider-ibm/releases) (** 注意事項 参照)
  - terraform テンプレート (****.tf ファイル, 使用するterraform テンプレート参照)
  - ssh key  (Local PC から IBM Cloud に接続するssh key。Local PC上でssh-keygenを実行)
   



#### 使用するterraform tfファイル(カスタマイズ作成)
* provider.tf
* powervs.tf


・provider.tf 中身  (2023/2/17更新版)

```
terraform {
  required_providers {
    ibm = {
      source = "IBM-Cloud/ibm"
      version = ">= 1.12.0"
    }
  }
}

provider "ibm" {
  ibmcloud_api_key   = "xxxxx"
  region             = "tok"
  zone               = "tok04"
}
```


##### * IBM Cloud API キーについて
IBM Cloud コンソール => 管理 => アクセス(IAM) => IBM Cloud API キーから作成して使用します。作成時に ID が確認できます。

![スクリーンショット 2020-03-16 9.41.24.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/517421/bba79219-09fc-2a43-7726-4181d090bbbe.png)

#####参考：IBM Cloud 資料 / ID およびアクセス権限の管理 : 
[サービス ID の API キーの管理](https://cloud.ibm.com/docs/iam?topic=iam-serviceidapikeys&locale=ja)


・powervs.tf 中身 (2023/2/17更新版)

```
$ cat powervs.tf
resource "ibm_pi_instance" "test-instance" {
    pi_memory             = "2"                   #<= メモリー(GB)
    pi_processors         = "0.25"                #<= CPU 
    pi_instance_name      = "testaix01"           #<= インスタンス名
    pi_proc_type          = "shared"              #<= リソース共有モード
    pi_migratable         = "true"                #<=インスタンス移動可否
    pi_image_id           = "d16dd949-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #<=使用するOSイメージ
    pi_volume_ids         = []   #<= 外部ボリュームを接続する場合指定
    pi_key_pair_name      = "id_rsa_xxxx" #<= IBM Cloud上の登録済 ssh-key
    pi_sys_type           = "s922"              #<= 対象筐体タイプ
    pi_replication_policy = "none"              #<=複製ポリシー (default none)
    pi_replicants         = "1"                 #<= デプロイするインスタンス数
    pi_storage_type       = "tier3"             #<= ストレージタイプ tier1 or tier3
    pi_cloud_instance_id  = "ef505604-xxxx-xxxx-xxxx-xxxxxxxxxxxxx"  #<= IBM Cloudで使用しているアカウントでのインスタンスID
    pi_network {
      network_id = "xxxxxa7c-4fe1-xxxx-d292xxxxx0"    #<=使用するネットワーク
    }

}
```
#####参考： IBM Cloud 資料/ Terraform 

・[Power Systems resources](https://cloud.ibm.com/docs/terraform?topic=terraform-power-vsi)
・[Power Systems data sources](
https://cloud.ibm.com/docs/terraform?topic=terraform-power-data-sources)
・IBM Cloud Provider for terraform : [pi_instance](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/pi_instance)

#####* pi_image_id、pi_network_ids について

ibmcloud コマンドを使用すると、id の取得が容易です。


IBM Cloud資料/ IBM Power Systems Virtual Servers CLI plug-in: 
[IBM Power Systems Virtual Servers CLI のリファレンス]
(https://cloud.ibm.com/docs/power-iaas-cli-plugin?topic=power-iaas-cli-plugin-power-iaas-cli-reference)



・イメージ ID 確認

```
$ ibmcloud pi images
Listing images under account <アカウント名> as user xxxxxxxx@xxxxx.xxx...
ID                                     Name                   Address
0dd8346b-xxxx-xxxx-xxxx-xxxxxxxxxxx   7100-05-04             /pcloud/v1/cloud-instances/xxxxxxx2fb6b4238a000a2672xxxxxxx/images/xxxxxx6x-xxxx-xxxx-xxxx-xxxxxxxxxxx
f2a1c801-xxxx-xxxx-xxxx-xxxxxxxxxxx   7100-05-05             /pcloud/v1/cloud-instances/xxxxxxx6b4238a000a267xxxxxxxx/images/xxxxc801-xxxx-xxxx-xxxx-xxxxxxxxxx
b97b94e6-xxxx-xxxx-xxxx-xxxxxxxxxxx   7200-03-03             /pcloud/v1/cloud-instances/xxxxxxxx2fb6b4238a000a267xxxxxxx/images/xxxxxe6-d71f-xxxx-xxxx-xxxxxxxxxx
d16dd949-xxxx-xxxx-xxxx-xxxxxxxxxxx   7200-04-01             /pcloud/v1/cloud-instances/xxxxxxxfb6b4238a000a26xxxxxxxx/images/xxxxx49-c95c-xxxx-xxxx-xxxxxxxxxx

```

・ネットワーク ID 確認

```
$ ibmcloud pi networks
Listing networks under account <アカウント名> as user xxxx@xxx.xxx.xxx  ...
ID                                     Name          Address
592886ba-xxxx-xxxx-xxxx-xxxxxxxxxxx   public-net   /pcloud/v1/cloud-instances/xxxxxxxxxb6b4238a000a26xxxxxxxxx/networks/xxxxxxxxx86ba-xxxx-xxxx-xxxx-0f34afxxxxxx
5976b283-xxxx-xxxx-xxxx-xxxxxxxxxxx   test-nw-02    /pcloud/v1/cloud-instances/xxxxxxxxxb6b4238a000a2672xxxxxxxxx/networks/xxxx6b283-xxxx-xxxx-xxxxx-44a5xxxxxxxxx
647dd1ce-xxxx-xxxx-xxxx-xxxxxxxxxxx   test-nw-01    /pcloud/v1/cloud-instances/xxxxxxxxx238a000a2672bxxxxxxxxx/networks/xxxxxxxxx1ce-99c3-xxxx-xxxx-d813xxxxxxxxx

```



##### * pi_cloud_instance_id について

pi_cloud_instance_id はアカウント毎に異なります。
IBM Cloud Console では、Power Systems Virtual Server サービス詳細の GUID が pi_cloud_instance_id に相当します。

![pvs.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/517421/598f76f4-46bc-2462-eccc-917a3068f56a.png)

ibmcloud cli でも確認可能です。

```
$ ibmcloud pi service-list
Listing services under account IBM - <アカウント名＞ as user xxxxxx@xxx.xxx.xxx. in region us-south...
ID                                                                                                                    Name
crn:v1:bluemix:public:power-iaas:us-south:a/528aa1cb0f3a4109acxxxxxxxxx:ef505604-xxxx-xxxx-xxxx-xxxxxx::   psvs-i
PPP
```
-> ID列 : 区切り末尾が pi_cloud_instance_id に相当します。


##### * ssh-keyについて

この検証では、Local PC からデプロイしたサーバーへのアクセスは ssh-key 実施しています。
IBM Cloud へのssh公開鍵の事前登録が必要です。
登録方法はIBM Cloud 資料/ [SSH鍵の追加] (https://cloud.ibm.com/docs/infrastructure/ssh-keys?topic=ssh-keys-adding-an-ssh-key&locale=ja) を参照ください。






### 実行

1) terraform init を実行し、プラグインをインストールします。
   配置した[terraform-provider-ibm v1.2.0](https://github.com/IBM-Cloud/terraform-provider-ibm/releases)) を確認しています。そのほか使用するプラグインがあればこの時導入されます。


実行ディレクトリ配置確認

```
$ ls -l
total 311744

-rw-------   1 xxxxxxx  staff      1679 Feb 10 16:16 id_rsa_2020_xxxx
-rw-------   1 xxxxxxx  staff       381 Feb 10 16:16 id_rsa_2020_xxxx.pub
-rw-r--r--@  1 xxxxxxx  staff       612 Feb 10 17:03 powervs.tf #<= tfファイル
-rw-r--r--@  1 xxxxxxx  staff       124 Feb 10 14:37 provider.tf #<= tfファイル
-rwxr-xr-x@  1 xxxxxxx  staff  57587984 Feb 10 06:03 terraform  #<=terraformコマンド本体
-rwxr-xr-x@  1 xxxxxxx  staff  55090968 Feb 10 16:05 terraform-provider-ibm_v1.2.0   # <= 配置したプロバイダー
```

init の実行

```
$ ./terraform init

Initializing the backend...

Initializing provider plugins...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.ibm: version = "~> 1.2"

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.

```

2) terraform plan を実行して、適用内容を確認します。

```
$ ./terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

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:

  # ibm_pi_instance.test-instance will be created
  + resource "ibm_pi_instance" "test-instance" {
      + addresses             = (known after apply)
      + id                    = (known after apply)
      + instance_volumes      = (known after apply)
      + pi_cloud_instance_id  = "ef505604-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
      + pi_creation_date      = (known after apply)
      + pi_disk_size          = (known after apply)
      + pi_health_status      = (known after apply)
      + pi_image_id           = "d16dd949-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
      + pi_instance_id        = (known after apply)
      + pi_instance_name      = "test-xxxx"
      + pi_instance_status    = (known after apply)
      + pi_key_pair_name      = "id_rsa_2020_xxxx"
      + pi_memory             = 2
      + pi_migratable         = true
      + pi_minproc            = (known after apply)
      + pi_network_ids        = [
          + "592886ba-xxxx-xxxx-xxxx-xxxxxxxxxxx",
        ]
      + pi_proc_type          = "shared"
      + pi_processors         = 0.25
      + pi_progress           = (known after apply)
      + pi_public_network     = false
      + pi_replicants         = 1
      + pi_replication_policy = "none"
      + pi_replication_scheme = "suffix"
      + pi_sys_type           = "e880"
    }

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

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
```

3) terraform apply を実行し、インスタンスをデプロイします。 

```
$ ./terraform apply --auto-approve
ibm_pi_instance.test-instance: Creating...
ibm_pi_instance.test-instance: Still creating... [10s elapsed]
ibm_pi_instance.test-instance: Still creating... [20s elapsed]
ibm_pi_instance.test-instance: Still creating... [30s elapsed]
ibm_pi_instance.test-instance: Still creating... [40s elapsed]
ibm_pi_instance.test-instance: Still creating... [50s elapsed]
ibm_pi_instance.test-instance: Still creating... [1m0s elapsed]
ibm_pi_instance.test-instance: Still creating... [1m10s elapsed]
ibm_pi_instance.test-instance: Still creating... [1m20s elapsed]
ibm_pi_instance.test-instance: Still creating... [1m30s elapsed]
ibm_pi_instance.test-instance: Still creating... [1m40s elapsed]
ibm_pi_instance.test-instance: Still creating... [1m50s elapsed]
ibm_pi_instance.test-instance: Still creating... [2m0s elapsed]
ibm_pi_instance.test-instance: Still creating... [2m10s elapsed]
ibm_pi_instance.test-instance: Still creating... [2m20s elapsed]
ibm_pi_instance.test-instance: Still creating... [2m30s elapsed]
ibm_pi_instance.test-instance: Still creating... [2m40s elapsed]
ibm_pi_instance.test-instance: Still creating... [2m50s elapsed]
ibm_pi_instance.test-instance: Still creating... [3m0s elapsed]
ibm_pi_instance.test-instance: Still creating... [3m10s elapsed]
ibm_pi_instance.test-instance: Still creating... [3m20s elapsed]
ibm_pi_instance.test-instance: Still creating... [3m30s elapsed]
ibm_pi_instance.test-instance: Still creating... [3m40s elapsed]
ibm_pi_instance.test-instance: Still creating... [3m50s elapsed]
ibm_pi_instance.test-instance: Still creating... [4m0s elapsed]
ibm_pi_instance.test-instance: Still creating... [4m10s elapsed]
ibm_pi_instance.test-instance: Still creating... [4m20s elapsed]
ibm_pi_instance.test-instance: Still creating... [4m30s elapsed]
ibm_pi_instance.test-instance: Still creating... [4m40s elapsed]
ibm_pi_instance.test-instance: Still creating... [4m50s elapsed]
ibm_pi_instance.test-instance: Still creating... [5m0s elapsed]
ibm_pi_instance.test-instance: Still creating... [5m10s elapsed]
ibm_pi_instance.test-instance: Still creating... [5m20s elapsed]
ibm_pi_instance.test-instance: Still creating... [5m30s elapsed]
ibm_pi_instance.test-instance: Still creating... [5m40s elapsed]
ibm_pi_instance.test-instance: Still creating... [5m50s elapsed]
ibm_pi_instance.test-instance: Still creating... [6m0s elapsed]
ibm_pi_instance.test-instance: Still creating... [6m10s elapsed]
ibm_pi_instance.test-instance: Still creating... [6m20s elapsed]
ibm_pi_instance.test-instance: Still creating... [6m30s elapsed]
ibm_pi_instance.test-instance: Still creating... [6m40s elapsed]
ibm_pi_instance.test-instance: Still creating... [6m50s elapsed]
ibm_pi_instance.test-instance: Still creating... [7m0s elapsed]
ibm_pi_instance.test-instance: Still creating... [7m10s elapsed]
ibm_pi_instance.test-instance: Still creating... [7m20s elapsed]
ibm_pi_instance.test-instance: Still creating... [7m30s elapsed]
ibm_pi_instance.test-instance: Still creating... [7m40s elapsed]
ibm_pi_instance.test-instance: Still creating... [7m50s elapsed]
ibm_pi_instance.test-instance: Still creating... [8m0s elapsed]
ibm_pi_instance.test-instance: Still creating... [8m10s elapsed]
ibm_pi_instance.test-instance: Still creating... [8m20s elapsed]
ibm_pi_instance.test-instance: Still creating... [8m30s elapsed]
ibm_pi_instance.test-instance: Still creating... [8m40s elapsed]
ibm_pi_instance.test-instance: Still creating... [8m50s elapsed]
ibm_pi_instance.test-instance: Still creating... [9m0s elapsed]
ibm_pi_instance.test-instance: Still creating... [9m10s elapsed]
ibm_pi_instance.test-instance: Still creating... [9m20s elapsed]
ibm_pi_instance.test-instance: Still creating... [9m30s elapsed]
ibm_pi_instance.test-instance: Still creating... [9m40s elapsed]
ibm_pi_instance.test-instance: Still creating... [9m50s elapsed]
ibm_pi_instance.test-instance: Still creating... [10m0s elapsed]
ibm_pi_instance.test-instance: Still creating... [10m10s elapsed]
ibm_pi_instance.test-instance: Still creating... [10m20s elapsed]
ibm_pi_instance.test-instance: Still creating... [10m30s elapsed]
ibm_pi_instance.test-instance: Still creating... [10m40s elapsed]
ibm_pi_instance.test-instance: Still creating... [10m50s elapsed]
ibm_pi_instance.test-instance: Creation complete after 10m58s [id=a614268b-xxxx-xxxx-xxxx-xxxxxxxxxxxxxxx]

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

-> 10 分程度でデプロイできました。

4) 確認

terraform show コマンドで詳細を確認します。

```
$ ./terraform show
# ibm_pi_instance.test-instance:
resource "ibm_pi_instance" "test-instance" {
    addresses             = [
        {
            externalip  = "52.xxx.xx.xxx"      #<-public ip
            ip          = "192.168.xxx.xx"     #<- private ip
            macaddress  = "fa:xx:xx:xx:xx:xxx"
            networkid   = "592886ba-xxxx-xxxx-xxxx-xxxxxxxxx"
            networkname = "public-net"
            type        = "fixed"
        },
    ]
    id                    = "a614268b-xxxx-xxxx-xxxx-xxxxxxxxxxx"
    pi_cloud_instance_id  = "ef505604-xxxx-xxxx-xxxx-xxxxxxxxxxx"
    pi_health_status      = "OK"
    pi_image_id           = "d16dd949-xxxx-xxxx-xxxx-xxxxxxxxxxx"
    pi_instance_name      = "test-xxxx"
    pi_instance_status    = "ACTIVE"
    pi_key_pair_name      = "id_rsa_2020_xxxx"
    pi_memory             = 2
    pi_migratable         = true
    pi_minproc            = 0.25
    pi_network_ids        = [
        "592886ba-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    ]
    pi_proc_type          = "shared"
    pi_processors         = 0.25
    pi_progress           = 0
    pi_public_network     = false
    pi_replicants         = 1
    pi_replication_policy = "none"
    pi_replication_scheme = "suffix"
    pi_sys_type           = "e880"
}
```

5) ログイン確認

デプロイ時に指定したssh key の private key を指定してログインします。

```
$ ssh -i id_rsa_2020_xxxx root@52.xxx.xx.xxx

The authenticity of host '52.xxx.xx.xxx (52.xxx.xx.xxx)' can't be established.
RSA key fingerprint is SHA256:vxxxxiDurWLbyMf1GlyCfxxxxxPRmNs0lo3cA24s.
Are you sure you want to continue connecting (yes/no)? yes  <= yes を入力

Last unsuccessful login: Mon Mar 16 02:10:12 CDT 2020 on ssh from 111.xxx.xxx.xxx
*******************************************************************************
*                                                                             *
*                                                                             *
*  Welcome to AIX Version 7.2!                                                *
*                                                                             *
*                                                                             *
*  Please see the README file in /usr/lpp/bos for information pertinent to    *
*  this release of the AIX Operating System.                                  *
*                                                                             *
*                                                                             *
*******************************************************************************
#
# oslevel -s
7200-04-01-1939
# exit
Connection to 52.xxx.xx.xxx closed.
```


6) 作成したインスタンスを削除します。


```
$ ./terraform destroy
ibm_pi_instance.test-instance: Refreshing state... [id=a614268b-xxxx-xxxx-xxxx-xxxxxxxxxx]

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:

  # ibm_pi_instance.test-instance will be destroyed
  - resource "ibm_pi_instance" "test-instance" {
      - addresses             = [
          - {
              - externalip  = "52.xxx.xx.xxx"
              - ip          = "192.168.xxx.xx"
              - macaddress  = "fa:xx:xx:xx:8f:xx"
              - networkid   = "592886ba-xxxx-xxxx-xxxx-xxxxxxxx"
              - networkname = "public-net"
              - type        = "fixed"
            },
        ] -> null
      - id                    = "a614268b-xxxx-xxxx-xxxx-xxxxxxxxxx" -> null
      - pi_cloud_instance_id  = "ef505604-xxxx-xxxx-xxxx-xxxxxxxxxx" -> null
      - pi_health_status      = "OK" -> null
      - pi_image_id           = "d16dd949-c95c-xxxx-xxxx-xxxxxxxxxx" -> null
      - pi_instance_name      = "test-xxxx" -> null
      - pi_instance_status    = "ACTIVE" -> null
      - pi_key_pair_name      = "id_rsa_2020_xxxx" -> null
      - pi_memory             = 2 -> null
      - pi_migratable         = true -> null
      - pi_minproc            = 0.25 -> null
      - pi_network_ids        = [
          - "592886ba-xxxx-xxxx-xxxx-xxxxxxxxxxx",
        ] -> null
      - pi_proc_type          = "shared" -> null
      - pi_processors         = 0.25 -> null
      - pi_progress           = 0 -> null
      - pi_public_network     = false -> null
      - pi_replicants         = 1 -> null
      - pi_replication_policy = "none" -> null
      - pi_replication_scheme = "suffix" -> null
      - pi_sys_type           = "e880" -> 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    #<= yes を入力

ibm_pi_instance.test-instance: Destroying... [id=a614268b-xxxx-xxx-xxxx-xxxxxxxxx]
ibm_pi_instance.test-instance: Still destroying... [id=a614268b-xxxx-xxxx-xxxx-xxxxxxxxxx, 10s elapsed]
ibm_pi_instance.test-instance: Destruction complete after 16s

Destroy complete! Resources: 1 destroyed.

```

destroyは1,2 分内程度で コマンド結果が返ってきました。




###### その他参考：
・[IBM-Cloud/terraform-provider-ibm](https://github.com/IBM-Cloud/terraform-provider-ibm/tree/master/examples/ibm-power)
・[Setting up the CLI and preparing your environment]
(https://cloud.ibm.com/docs/terraform?topic=terraform-setup_cli)



##### **注意事項

最新の [IBM Cloud Terraform Provider] (https://github.com/IBM-Cloud/terraform-provider-ibm/releases) は「**.tfファイル」の記載変更が必要である可能性があります。


以上です。