Creating Linodes by Terraform

TerraformでLinodeのLinodes Instanceを作ってみる。

Get List of Linodes imeges

作成できるディストリビューションイメージをlinode-cliで確認。

linode-cli images ls --vendor Debian

下記のように表示される。

┌──────────────────────────────┬────────────────────────────────┬────────┬─────────────┬───────────┬──────┬───────────┐
│ id                           │ label                          │ vendor │ description │ is_public │ size │ status    │
├──────────────────────────────┼────────────────────────────────┼────────┼─────────────┼───────────┼──────┼───────────┤
│ linode/debian10              │ Debian 10                      │ Debian │             │ True      │ 1500 │ available │
│ linode/debian11              │ Debian 11                      │ Debian │             │ True      │ 1300 │ available │
│ linode/debian11-kube-v1.24.8 │ Kubernetes 1.24.8 on Debian 11 │ Debian │             │ True      │ 3500 │ available │
│ linode/debian11-kube-v1.25.4 │ Kubernetes 1.25.4 on Debian 11 │ Debian │             │ True      │ 3500 │ available │
│ linode/debian11-kube-v1.26.1 │ Kubernetes 1.26.1 on Debian 11 │ Debian │             │ True      │ 3500 │ available │
│ linode/debian11-kube-v1.26.3 │ Kubernetes 1.26.3 on Debian 11 │ Debian │             │ True      │ 3500 │ available │
│ linode/debian9               │ Debian 9                       │ Debian │             │ True      │ 1600 │ available │
└──────────────────────────────┴────────────────────────────────┴────────┴─────────────┴───────────┴──────┴───────────┘

Get List of Linode regions

Linodeのリージョンリストをlinode-cliで確認。

linode-cli regions ls

下記のように表示される。

┌──────────────┬───────────────┬─────────┬─────────────────────────────────────────────────────────────────────┬────────┐
│ id           │ label         │ country │ capabilities                                                        │ status │
├──────────────┼───────────────┼─────────┼─────────────────────────────────────────────────────────────────────┼────────┤
│ ap-west      │ Mumbai, IN    │ in      │ Linodes, NodeBalancers, Block Storage, GPU Linodes, Kubernetes, ... │ ok     │
│ ca-central   │ Toronto, CA   │ ca      │ Linodes, NodeBalancers, Block Storage, Kubernetes, Cloud Firewal... │ ok     │
│ ap-southeast │ Sydney, AU    │ au      │ Linodes, NodeBalancers, Block Storage, Kubernetes, Cloud Firewal... │ ok     │
│ us-central   │ Dallas, TX    │ us      │ Linodes, NodeBalancers, Block Storage, Kubernetes, Cloud Firewal... │ ok     │
│ us-west      │ Fremont, CA   │ us      │ Linodes, NodeBalancers, Block Storage, Kubernetes, Cloud Firewal... │ ok     │
│ us-southeast │ Atlanta, GA   │ us      │ Linodes, NodeBalancers, Block Storage, Object Storage, GPU Linod... │ ok     │
│ us-east      │ Newark, NJ    │ us      │ Linodes, NodeBalancers, Block Storage, Object Storage, GPU Linod... │ ok     │
│ eu-west      │ London, UK    │ uk      │ Linodes, NodeBalancers, Block Storage, Kubernetes, Cloud Firewal... │ ok     │
│ ap-south     │ Singapore, SG │ sg      │ Linodes, NodeBalancers, Block Storage, Object Storage, GPU Linod... │ ok     │
│ eu-central   │ Frankfurt, DE │ de      │ Linodes, NodeBalancers, Block Storage, Object Storage, GPU Linod... │ ok     │
│ ap-northeast │ Tokyo, JP     │ jp      │ Linodes, NodeBalancers, Block Storage, Kubernetes, Cloud Firewal... │ ok     │
└──────────────┴───────────────┴─────────┴─────────────────────────────────────────────────────────────────────┴────────┘

Get List of Linodes Types

LinodesのTypeリストを確認。

curl https://api.linode.com/v4/linode/types | dsq -s json "select id, label from {'data'}" | jq

下記のように表示される。

[
  {
    "id": "g6-nanode-1",
    "label": "Nanode 1GB"
  },
  {
    "id": "g6-standard-1",
    "label": "Linode 2GB"
  }
]

Configuring Terraform for Linode

Linode用のprovider.tfを作成

terraform {
  required_providers {
    linode = {
      source = "linode/linode"
      version = "~> 2.0.0"
    }
  }
}

provider "linode" {
  token = "LINODE_API_TOKEN"
}

API_TOKENはLinode management consoleで作成する。

プロジェクトフォルダ内でterraformを初期化する。

terraform init

初期化成功すると下記のようなメッセージが表示される。

Initializing the backend...

Initializing provider plugins...
- Finding linode/linode versions matching "~> 2.0.0"...
- Installing linode/linode v2.0.0...
- Installed linode/linode v2.0.0 (signed by a HashiCorp partner, key ID F4E6BBD0EA4FE463)

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.

Creating Linodes by Terraform

linodes.tfを作成

resource "linode_instance" "lndtkyd1" {
  image = "linode/debian11"
  label = "lndtkyd1.linando.net"
  region = "ap-northeast"
  type = "g6-nanode-1"
  authorized_keys = ["PUBLIC_SSH_KEY"]
  root_pass = "ROOT_PASSWORD"
}

terraformの実行計画を確認

terraform plan

下記のような実行計画が表示される。

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:

  # linode_instance.lndtkyd1 will be created
  + resource "linode_instance" "lndtkyd1" {
      + authorized_keys    = [
          + "PUBLIC_SSH_KEY"
        ]
      + backups            = (known after apply)
      + backups_enabled    = (known after apply)
      + boot_config_label  = (known after apply)
      + booted             = (known after apply)
      + host_uuid          = (known after apply)
      + id                 = (known after apply)
      + image              = "linode/debian11"
      + ip_address         = (known after apply)
      + ipv4               = (known after apply)
      + ipv6               = (known after apply)
      + label              = "lndtkyd1.linando.net"
      + private_ip_address = (known after apply)
      + region             = "ap-northeast"
      + resize_disk        = false
      + root_pass          = (sensitive value)
      + shared_ipv4        = (known after apply)
      + specs              = (known after apply)
      + status             = (known after apply)
      + swap_size          = (known after apply)
      + type               = "g6-nanode-1"
      + watchdog_enabled   = true
    }

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

確認したプランを適用

terraform apply

確認が促されるので、’yes’を入力

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

下記のように表示されてLinodes作成完了

linode_instance.lndtkyd1: Creating...
linode_instance.lndtkyd1: Still creating... [10s elapsed]
linode_instance.lndtkyd1: Still creating... [20s elapsed]
linode_instance.lndtkyd1: Still creating... [30s elapsed]
linode_instance.lndtkyd1: Still creating... [40s elapsed]
linode_instance.lndtkyd1: Creation complete after 42s [id=45570636]

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

ステータス確認

terraform show
# linode_instance.lndtkyd1:
resource "linode_instance" "lndtkyd1" {
    authorized_keys   = [
        "PUBLIC_SSH_KEY",
    ]
    backups           = [
        {
            available = false
            enabled   = false
            schedule  = [
                {
                    day    = ""
                    window = ""
                },
            ]
        },
    ]
    backups_enabled   = false
    boot_config_label = "My Debian 11 Disk Profile"
    booted            = true
    host_uuid         = "965726b444d9939a896c4537163434c6c855e644"
    id                = "45570636"
    image             = "linode/debian11"
    ip_address        = "172.105.229.235"
    ipv4              = [
        "172.105.229.235",
    ]
    ipv6              = "2400:8902::f03c:93ff:fe71:937f/128"
    label             = "lndtkyd1.linando.net"
    private_ip        = false
    region            = "ap-northeast"
    resize_disk       = false
    root_pass         = (sensitive value)
    shared_ipv4       = []
    specs             = [
        {
            disk     = 25600
            memory   = 1024
            transfer = 1000
            vcpus    = 1
        },
    ]
    status            = "running"
    swap_size         = 512
    type              = "g6-nanode-1"
    watchdog_enabled  = true

    alerts {
        cpu            = 90
        io             = 10000
        network_in     = 10
        network_out    = 10
        transfer_quota = 80
    }

    config {
        kernel       = "linode/grub2"
        label        = "My Debian 11 Disk Profile"
        memory_limit = 0
        root_device  = "/dev/sda"
        run_level    = "default"
        virt_mode    = "paravirt"

        devices {
            sda {
                disk_id    = 90915772
                disk_label = "Debian 11 Disk"
                volume_id  = 0
            }
            sdb {
                disk_id    = 90915773
                disk_label = "512 MB Swap Image"
                volume_id  = 0
            }
        }

        helpers {
            devtmpfs_automount = true
            distro             = true
            modules_dep        = true
            network            = true
            updatedb_disabled  = true
        }
    }

    disk {
        authorized_keys  = []
        authorized_users = []
        filesystem       = "ext4"
        id               = 90915772
        label            = "Debian 11 Disk"
        read_only        = false
        size             = 25088
        stackscript_data = (sensitive value)
        stackscript_id   = 0
    }
    disk {
        authorized_keys  = []
        authorized_users = []
        filesystem       = "swap"
        id               = 90915773
        label            = "512 MB Swap Image"
        read_only        = false
        size             = 512
        stackscript_data = (sensitive value)
        stackscript_id   = 0
    }
}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です