Creating DigitalOcean Droplet(CentOS 9 Stream) by Terraform

TerraformでDigitalOceanのDropletをつくる。

Get list of Droplet Images

Dropletを作成出来るディストリビューションイメージを確認。

doctl compute image list-distribution
ID           Name                  Type        Distribution    Slug                   Public    Min Disk
77558491     12.2 ufs x64          snapshot    FreeBSD         freebsd-12-x64-ufs     true      20
77558552     12.2 zfs x64          snapshot    FreeBSD         freebsd-12-x64-zfs     true      15
78547182     1.5.8 x64             snapshot    RancherOS       rancheros              true      15
84780898     34 x64                snapshot    Fedora          fedora-34-x64          true      15
89246461     8 Stream x64          snapshot    CentOS          centos-stream-8-x64    true      15
95344509     35 x64                snapshot    Fedora          fedora-35-x64          true      15
96967120     RockyLinux 8.5 x64    snapshot    Rocky Linux     rockylinux-8-x64       true      15
96967133     RockyLinux 8.4 x64    snapshot    Rocky Linux     rockylinux-8-4-x64     true      15
100167549    7 x64                 snapshot    CentOS          centos-7-x64           true      15
101111514    20.04 (LTS) x64       snapshot    Ubuntu          ubuntu-20-04-x64       true      15
101120353    9 x64                 snapshot    Debian          debian-9-x64           true      15
101121241    18.04 (LTS) x64       snapshot    Ubuntu          ubuntu-18-04-x64       true      15
101121352    21.10 x64             snapshot    Ubuntu          ubuntu-21-10-x64       true      15
101134485    10 x64                snapshot    Debian          debian-10-x64          true      15
101537412    11 x64                snapshot    Debian          debian-11-x64          true      15
104246242    9 Stream x64          snapshot    CentOS          centos-stream-9-x64    true      15

下記イメージでDropletを作成する。

104246242    9 Stream x64          snapshot    CentOS          centos-stream-9-x64    true      15

Configuring Terraform for DigitalOcean

環境変数”DO_PAT”にDigitalOcean Personal Access Tokenを設定する。

export DO_PAT="my_personal_access_token"

DigitalOcean用のprovider.tfを作成

terraform {
  required_providers {
    digitalocean = {
      source = "digitalocean/digitalocean"
      version = "~> 2.0"
    }
  }
}

variable "do_token" {}
variable "pvt_key" {}

provider "digitalocean" {
  token = var.do_token
}

data "digitalocean_ssh_key" "hogehoge" {
  name = "hogehoge"
}

digitalocean_ssh_keyには登録している”SSH Keys”のキー名を指定する。

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

terraform init

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

Initializing the backend...

Initializing provider plugins...
- Finding digitalocean/digitalocean versions matching "~> 2.0"...
- Installing digitalocean/digitalocean v2.18.0...
- Installed digitalocean/digitalocean v2.18.0 (signed by a HashiCorp partner, key ID F82037E524B9C0E8)

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 Droplet by Terraform

droplet.tfを作成

resource "digitalocean_droplet" "dosgcs91" {
  image = "centos-stream-9-x64"
  name = "dosgcs91.dok8s.net"
  region = "sgp1"
  ipv6 = true
  size = "s-1vcpu-1gb"
  ssh_keys = [
    data.digitalocean_ssh_key.hogehoge.id
  ]

  connection {
    host = self.ipv4_address
    user = "root"
    type = "ssh"
    private_key = file(var.pvt_key)
    timeout = "2m"
  }

}

terraformの実行計画を確認

terraform plan -var "do_token=${DO_PAT}" -var "pvt_key=$HOME/.ssh/id_rsa"

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

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:

  # digitalocean_droplet.dosgcs91 will be created
  + resource "digitalocean_droplet" "dosgcs91" {
      + backups              = false
      + created_at           = (known after apply)
      + disk                 = (known after apply)
      + graceful_shutdown    = false
      + id                   = (known after apply)
      + image                = "centos-stream-9-x64"
      + ipv4_address         = (known after apply)
      + ipv4_address_private = (known after apply)
      + ipv6                 = true
      + ipv6_address         = (known after apply)
      + locked               = (known after apply)
      + memory               = (known after apply)
      + monitoring           = false
      + name                 = "dosgcs91.dok8s.net"
      + price_hourly         = (known after apply)
      + price_monthly        = (known after apply)
      + private_networking   = (known after apply)
      + region               = "sgp1"
      + resize_disk          = true
      + size                 = "s-1vcpu-1gb"
      + ssh_keys             = [
          + "29140726",
        ]
      + status               = (known after apply)
      + urn                  = (known after apply)
      + vcpus                = (known after apply)
      + volume_ids           = (known after apply)
      + vpc_uuid             = (known after apply)
    }

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

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

確認したプランを適用

terraform apply -var "do_token=${DO_PAT}" -var "pvt_key=$HOME/.ssh/id_rsa"

確認が促されるので、’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

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

digitalocean_droplet.dosgcs91: Creating...
digitalocean_droplet.dosgcs91: Still creating... [10s elapsed]
digitalocean_droplet.dosgcs91: Still creating... [20s elapsed]
digitalocean_droplet.dosgcs91: Still creating... [30s elapsed]
digitalocean_droplet.dosgcs91: Still creating... [40s elapsed]
digitalocean_droplet.dosgcs91: Still creating... [50s elapsed]
digitalocean_droplet.dosgcs91: Creation complete after 56s [id=292861753]

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

ステータスを確認

terraform show
# digitalocean_droplet.dosgcs91:
resource "digitalocean_droplet" "dosgcs91" {
    backups              = false
    created_at           = "2022-03-29T08:18:05Z"
    disk                 = 25
    graceful_shutdown    = false
    id                   = "292861753"
    image                = "centos-stream-9-x64"
    ipv4_address         = "167.99.66.33"
    ipv4_address_private = "10.104.0.3"
    ipv6                 = true
    ipv6_address         = "2400:6180:0:d0::ef2:b001"
    locked               = false
    memory               = 1024
    monitoring           = false
    name                 = "dosgcs91.dok8s.net"
    price_hourly         = 0.00744
    price_monthly        = 5
    private_networking   = true
    region               = "sgp1"
    resize_disk          = true
    size                 = "s-1vcpu-1gb"
    ssh_keys             = [
        "29140726",
    ]
    status               = "active"
    urn                  = "do:droplet:292861753"
    vcpus                = 1
    volume_ids           = []
    vpc_uuid             = "6797d105-a4a4-4cdc-ac1a-5e9494b1d3ee"
}

コメントを残す

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