Terraform を Digital Ocean で触ってみた (初級編)

  • 28
    いいね
  • 0
    コメント
この記事は最終更新日から1年以上が経過しています。

目的

  • Terraform さわってみる
  • 起動 -> 修正 -> 削除をやってみる
  • Digital Ocean の API をさわってみる

前提

手順

Digital Ocean のトークンを取得する

Digital Ocean の管理画面から作成できます

スクリーンショット 2014-08-23 10.23.05 PM.png

do.tf を作成する

Digital Ocean で触るための設定を追加するとともに 2 つ droplet を立ててみましょう

do.tf という名前にしてみました。

provider "digitalocean" {
    token = "ここにトークンいれましょう"
}

resource "digitalocean_droplet" "node-1" {
    image = "ubuntu-14-04-x64"
    name = "node-1"
    region = "sgp1"
    size = "512mb"
}

resource "digitalocean_droplet" "node-2" {
    image = "ubuntu-14-04-x64"
    name = "node-1"
    region = "sgp1"
    size = "512mb"
}

わざと name を両方 node-1 にしています

準備ができたら plan で何が起きるのか見てみます。

$ terraform plan
Refreshing Terraform state prior to plan...


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.

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.

+ digitalocean_droplet.node-1
    backups:              "" => "<computed>"
    image:                "" => "ubuntu-14-04-x64"
    ipv4_address:         "" => "<computed>"
    ipv4_address_private: "" => "<computed>"
    ipv6:                 "" => "<computed>"
    ipv6_address:         "" => "<computed>"
    ipv6_address_private: "" => "<computed>"
    locked:               "" => "<computed>"
    name:                 "" => "node-1"
    private_networking:   "" => "<computed>"
    region:               "" => "sgp1"
    size:                 "" => "512mb"
    status:               "" => "<computed>"

+ digitalocean_droplet.node-2
    backups:              "" => "<computed>"
    image:                "" => "ubuntu-14-04-x64"
    ipv4_address:         "" => "<computed>"
    ipv4_address_private: "" => "<computed>"
    ipv6:                 "" => "<computed>"
    ipv6_address:         "" => "<computed>"
    ipv6_address_private: "" => "<computed>"
    locked:               "" => "<computed>"
    name:                 "" => "node-1"
    private_networking:   "" => "<computed>"
    region:               "" => "sgp1"
    size:                 "" => "512mb"
    status:               "" => "<computed>"

新しい droplet が二つ作られることがわかりました。さっそく実行してみます。

apply で生成できます。

$ terraform apply
digitalocean_droplet.node-2: Creating...
  image:  "" => "ubuntu-14-04-x64"
  name:   "" => "node-1"
  region: "" => "sgp1"
  size:   "" => "512mb"
digitalocean_droplet.node-1: Creating...
  image:  "" => "ubuntu-14-04-x64"
  name:   "" => "node-1"
  region: "" => "sgp1"
  size:   "" => "512mb"
digitalocean_droplet.node-1: Creation complete
digitalocean_droplet.node-2: Creation complete

Apply complete! Resources: 2 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

スクリーンショット 2014-08-23 10.18.04 PM.png

両方の名前が node-1 になっていました。これはまずいですね、node-2 にしましょう。

provider "digitalocean" {
    token = "ここにトークンいれましょう"
}

resource "digitalocean_droplet" "node-1" {
    image = "ubuntu-14-04-x64"
    name = "node-1"
    region = "sgp1"
    size = "512mb"
}

resource "digitalocean_droplet" "node-2" {
    image = "ubuntu-14-04-x64"
    name = "node-2"
    region = "sgp1"
    size = "512mb"
}

name を node-2 に変更しました。

さっそく plan を実行してみます。

$ terraform plan
Refreshing Terraform state prior to plan...

digitalocean_droplet.node-2: Refreshing state... (ID: 2433223)
digitalocean_droplet.node-1: Refreshing state... (ID: 2433224)

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.

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.

~ digitalocean_droplet.node-2
    name: "node-1" => "node-2"

node-2 の名前を node-1 から node-2 に変えることわかりました。

期待通りなので apply で実行します。

$ terraform apply
digitalocean_droplet.node-2: Refreshing state... (ID: 2433223)
digitalocean_droplet.node-1: Refreshing state... (ID: 2433224)
digitalocean_droplet.node-2: Modifying...
  name: "node-1" => "node-2"
digitalocean_droplet.node-2: Modifications complete

Apply complete! Resources: 0 added, 1 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

スクリーンショット 2014-08-23 10.19.59 PM.png

無事ノード名が node-2 に変わっていることが確認できました。

さて、最後は droplet を削除します。

削除するにはコメントアウトしてしまえば良いです。

provider "digitalocean" {
    token = "ここにトークンいれましょう"
}

/*
resource "digitalocean_droplet" "node-1" {
    image = "ubuntu-14-04-x64"
    name = "node-1"
    region = "sgp1"
    size = "512mb"
}

resource "digitalocean_droplet" "node-2" {
    image = "ubuntu-14-04-x64"
    name = "node-2"
    region = "sgp1"
    size = "512mb"
}
*/

さて、plan を実行してみましょう。

$ terraform plan
Refreshing Terraform state prior to plan...

digitalocean_droplet.node-1: Refreshing state... (ID: 2433224)
digitalocean_droplet.node-2: Refreshing state... (ID: 2433223)

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.

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.

- digitalocean_droplet.node-1

- digitalocean_droplet.node-2

二つの droplet が削除されていることがわかりました。

実際に apply を実行します。

$ terraform apply
digitalocean_droplet.node-1: Refreshing state... (ID: 2433224)
digitalocean_droplet.node-2: Refreshing state... (ID: 2433223)
digitalocean_droplet.node-1: Destroying...
digitalocean_droplet.node-2: Destroying...
digitalocean_droplet.node-2: Destruction complete
digitalocean_droplet.node-1: Destruction complete

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

無事二つの droplet が削除されました。

感想

かなり簡単に操作できました。まだ基本的なことしかやっていませんが、もっといろいろできるみたいです今後は以下のようなことをやってみるつもりです。

  • Provision に Ansible を使用する
  • クラスタ機能をもつ何かを入れ、2~3 台でクラスタを組む
  • AWS でもやってみる