Arukas provider with Terraform

Arukas provider with Terraform

以前Windows環境で実行できないという理由で消えてしまったterraformのArukasプロバイダですが、terraform v0.8.7で無事復活したようです!(コメントで気づいた!)

早速使ってみます。

準備

まずArukasのトークンとシークレットキーを取得します。

ドキュメントを見ると環境変数からトークンとシークレットキーを読み込ませられるので設定しておきます。

export ARUKAS_JSON_API_TOKEN=XXXXXXXX
export ARUKAS_JSON_API_SECRET=YYYYYYYY

使ってみる

以下のようなtfファイルを作成。

resource "arukas_container" "hello_arukas" {
  name      = "hello_arukas"
  image     = "nginx:latest"
  instances = 1
  memory    = 256

  ports = {
    protocol = "tcp"
    number   = "80"
  }
}

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.


The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed. Cyan entries are data sources to be read.

Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.

+ arukas_container.hello_arukas
    app_id:                 "<computed>"
    endpoint:               "<computed>"
    endpoint_full_hostname: "<computed>"
    endpoint_full_url:      "<computed>"
    image:                  "nginx:latest"
    instances:              "1"
    memory:                 "256"
    name:                   "hello_arukas"
    port_mappings.#:        "<computed>"
    ports.#:                "1"
    ports.0.number:         "80"
    ports.0.protocol:       "tcp"


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

apply。

$ terraform apply
arukas_container.hello_arukas: Creating...
  app_id:                 "" => "<computed>"
  endpoint:               "" => "<computed>"
  endpoint_full_hostname: "" => "<computed>"
  endpoint_full_url:      "" => "<computed>"
  image:                  "" => "nginx:latest"
  instances:              "" => "1"
  memory:                 "" => "256"
  name:                   "" => "hello_arukas"
  port_mappings.#:        "" => "<computed>"
  ports.#:                "" => "1"
  ports.0.number:         "" => "80"
  ports.0.protocol:       "" => "tcp"
arukas_container.hello_arukas: Still creating... (10s elapsed)
arukas_container.hello_arukas: Still creating... (20s elapsed)
arukas_container.hello_arukas: Still creating... (30s elapsed)
arukas_container.hello_arukas: Still creating... (40s elapsed)
arukas_container.hello_arukas: Still creating... (50s elapsed)
arukas_container.hello_arukas: Still creating... (1m0s elapsed)
arukas_container.hello_arukas: Still creating... (1m10s elapsed)
arukas_container.hello_arukas: Creation complete

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

The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.

State path: terraform.tfstate

1分ほど待ちましたが、このときArukasの管理画面をみるとコンテナイメージのデプロイ中となっている状態でした。

無事立ち上がった。 ✨

destroyします。さようなら。

$ terraform destroy
Do you really want to destroy?
  Terraform will delete all your managed infrastructure.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

arukas_container.hello_arukas: Refreshing state... (ID: 64820ce8-3f76-48e4-8ff7-5c1313bcad98)
arukas_container.hello_arukas: Destroying...
arukas_container.hello_arukas: Destruction complete

Destroy complete! Resources: 1 destroyed.

Yay!

作成されるterraform.tfstate

こんな感じでした。

{
    "version": 3,
    "terraform_version": "0.8.7",
    "serial": 0,
    "lineage": "d847145f-d876-440b-9948-68bdf354eebf",
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {},
            "resources": {
                "arukas_container.hello_arukas": {
                    "type": "arukas_container",
                    "depends_on": [],
                    "primary": {
                        "id": "64820ce8-3f76-48e4-8ff7-5c1313bcad98",
                        "attributes": {
                            "app_id": "483adcd4-0832-4c86-b007-46f4b15a4489",
                            "cmd": "",
                            "endpoint": "determined-colden-6071",
                            "endpoint_full_hostname": "determined-colden-6071.arukascloud.io",
                            "endpoint_full_url": "https://determined-colden-6071.arukascloud.io",
                            "environments.#": "0",
                            "id": "64820ce8-3f76-48e4-8ff7-5c1313bcad98",
                            "image": "nginx:latest",
                            "instances": "1",
                            "memory": "256",
                            "name": "hello_arukas",
                            "port_mappings.#": "1",
                            "port_mappings.0.container_port": "80",
                            "port_mappings.0.host": "seaof-153-125-237-173.jp-tokyo-18.arukascloud.io",
                            "port_mappings.0.ipaddress": "153.125.237.173",
                            "port_mappings.0.service_port": "31350",
                            "ports.#": "1",
                            "ports.0.number": "80",
                            "ports.0.protocol": "tcp"
                        },
                        "meta": {},
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": ""
                }
            },
            "depends_on": []
        }
    ]
}

既存のArukasをterraform管理しようとしたけど...

※ arukasプロバイダ作成者の@yamamoto-febcさんから補足をいただけたので追記してます。感謝! ✨

~~まだ厳しそうな気がします。~~Arukasはまだβ版で、定期的にコンテナが再起動されます。

そして再起動されると、Portマッピングのステータスが変化します。具体的には以下の部分です。

  • ほかにもコンテナ再起動で変わる値あるかも
$ cat terraform.tfstate.backup | jq '.modules[].resources."arukas_container.hello_arukas".primary.attributes."port_mappings.0.host"'
"seaof-153-125-237-173.jp-tokyo-18.arukascloud.io"

$ cat terraform.tfstate.backup | jq '.modules[].resources."arukas_container.hello_arukas".primary.attributes."port_mappings.0.service_port"'
"31350"

するとterraform.tfstateとの差分が発生してしまうので、terraoformを実行したときに期待しない挙動をしてしまうと思われます。

なのでterraformでちゃんと管理していくのは、Arukasからβが外れてから(定期的なコンテナ再起動がなくなってから)のほうが良さそう。

厳しそうと思っていたが...

コメントで補足を頂いたように、Arukasの再起動で動的に変化してしまう属性は、Arukasプロバイダ側で管理しないように定義されているとのことです。

新規プロバイダを作るためのヘルパー。

terraform.tfstateを実際の値に更新したい場合はterraform refreshコマンドがあるのでこれを使えばOK。

これなら既存のArukasをterraform管理下に置けそう。 ✨

感想

簡単に使えて良い!

まだArukasは機能もそんなに多くないので、terraformのプロバイダはどんな感じで作れるのか見て学ぶにもちょうど良さそう。

参考リンク

Arukasプロバイダのドキュメント。

terraformのチェンジログ。

provider作りの参考。