TerraformをDigital Oceanで試してみる1
Digital Oceanの情報取得
Terraform を Digital Ocean で触ってみた (初級編) を参考にDigital Ocean のtokenを作成します。
Degital OceanのAPI V2を利用して、curlからSSH鍵の情報を取得します。
参考:https://developers.digitalocean.com/documentation/v2/#ssh-keys
# curl -X GET -H 'Content-Type: application/json' -H 'Authorization: Bearer 先ほど作成したtokenを記載' "https://api.digitalocean.com/v2/account/keys" { "ssh_keys": [ { "id": 512189, "fingerprint": "3b:16:bf:e4:8b:00:8b:b8:59:8c:a9:d3:f0:19:45:fa", "public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQDDHr/jh2Jy4yALcK4JyWbVkPRaWmhck3IgCoeOO3z1e2dBowLh64QAM+Qb72pxekALga2oi4GvT+TlWNhzPH4V example", "name": "My SSH Public Key" } ], "links": { }, "meta": { "total": 1 } }
ここで返ってきたidとDegital Oceanのtokenを次で作成する.tfファイルで利用します。
.tfファイルの作成
terraformでは、.tfファイルを作成しそこから環境を作成出来ます。
ここで、今まで行った作業を元にDegital Ocean用のdo.tfファイルを作成してみます。
provider "digitalocean" { token = "作成したtokenを記載" } resource "digitalocean_droplet" "node-1" { image = "ubuntu-14-04-x64" name = "node-1" region = "sgp1" size = "512mb" ssh_keys = [512189] }
dropletの作成
do.tfを利用して、dropletを作成します。 planのみでは実際にdropletは作成されず、applyまで実行することでdropletが作成されます。
$ 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 image: "" => "ubuntu-14-04-x64" ipv4_address: "" => "<computed>" ipv4_address_private: "" => "<computed>" ipv6_address: "" => "<computed>" ipv6_address_private: "" => "<computed>" locked: "" => "<computed>" name: "" => "node-1" region: "" => "sgp1" size: "" => "512mb" ssh_keys.#: "" => "1" ssh_keys.0: "" => "512189" status: "" => "<computed>" $ terraform apply digitalocean_droplet.node-1: Creating... image: "" => "ubuntu-14-04-x64" ipv4_address: "" => "<computed>" ipv4_address_private: "" => "<computed>" ipv6_address: "" => "<computed>" ipv6_address_private: "" => "<computed>" locked: "" => "<computed>" name: "" => "node-1" region: "" => "sgp1" size: "" => "512mb" ssh_keys.#: "" => "1" ssh_keys.0: "" => "512189" status: "" => "<computed>" digitalocean_droplet.node-1: Creation complete
status: "" => "
dropletにログイン
作成したdropletの情報を確認してログインします。
$ terraform show digitalocean_droplet.node-1: id = 5556133 image = ubuntu-14-04-x64 ipv4_address = 128.199.64.45 locked = false name = node-1 region = sgp1 size = 512mb ssh_keys.# = 1 ssh_keys.0 = 512189 status = active
IPアドレスがわかったので、実際にログインしてみます。
秘密鍵は~/.ssh/id_rsaにあるものとしています。
ssh -i ~/.ssh/id_rsa root@128.199.64.45
dropletの破棄
作成したdropletを破棄します。
$ 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 digitalocean_droplet.node-1: Refreshing state... (ID: 5556133) digitalocean_droplet.node-1: Destroying... digitalocean_droplet.node-1: Destruction complete Apply complete! Resources: 0 added, 0 changed, 1 destroyed.