ISUCON7予選と同じ環境を立ち上げたいなーと思ってsakura cloudのterraformを触ってみたのでそのメモです。
第一歩目として単にサーバを立ち上げて見ました。
さくらのクラウドユーザコミュニティによる公認ツールがあるようなのでそれを使いました。
やることは
- API Tokenの取得
- terraformの設定を書く
- 実行する
という感じです。
1. API Tokenの取得
すごくまとまっていたので、ここを参考にすれば一発です。
インストールガイド - Terraform for さくらのクラウド
2. terraformの設定を書く
resource "sakuracloud_disk" "disk01"{ name = "disk01" source_archive_id = "${data.sakuracloud_archive.ubuntu.id}" password = "password" # ssh用のパスワードなのでいい感じに変更する必要あり } # サーバー resource "sakuracloud_server" "server01" { name = "server01" disks = ["${sakuracloud_disk.disk01.id}"] tags = ["@virtio-net-pci"] } data sakuracloud_archive "ubuntu" { os_type = "ubuntu" }
ちょっとはまったところ
最低限の設定を書きたかったので下記記事を参考にしたのですが、
resource "sakuracloud_disk" "disk01"{ # ディスク名 name = "disk01" # コピー元アーカイブ(CentOS 7.3 64bitを利用) source_archive_id = "112900062806" # パスワード password = "YOUR_PASSWORD_HERE" }
の部分でsource_archive_idが今現在異なっているらしく * sakuracloud_disk.disk01: Failed to create SakuraCloud Disk resource: Error in response: &sacloud.ResultErrorValue{IsFatal:true, Serial:"...", Status:"400 Bad Request", ErrorCode:"param_res_not_found", ErrorMessage:"不適切な要求です。パラメータで指定されたリソースが存在しません。IDをご確認ください。"}
と怒られてしまいました。
sakura cloudのpublic archiveと言うもののIDみたいですが、
それがちょくちょく更新されて変わっているみたいですね。
なので先程書いた例のように
data sakuracloud_archive "ubuntu" { os_type = "ubuntu" }
と書いてあげて、 source_archive_id = "${data.sakuracloud_archive.ubuntu.id}"
を利用すればいい感じに出来るようです。
os_typeに記載できるのはこのあたりにありました。
3. 実行する
あとはterraformを実行するだけです。
さくっと行けそうだったのでdockerでやりました。
makefileに書いたのでこれを使うと make apply token=... secret=...
みたいな感じで実行できます。
token= secret= SAKURA=init apply plan destroy $(SAKURA): docker run -it --rm \ -e SAKURACLOUD_ACCESS_TOKEN=$(token) \ -e SAKURACLOUD_ACCESS_TOKEN_SECRET=$(secret) \ -v $(PWD):/workdir \ sacloud/terraform $@
https://hub.docker.com/r/sacloud/terraform/
ちょっとはまったところ
インストールガイドを見ながら実行したんですが、
微妙にコマンドが違っていて動かなかったですw
latestのimageで実行するとmountするディレクトリが現在異なっているようでした。
今のところ下記のように書いてありますが、 /work
のところを /workdir
に変える必要があります。
Dockerhubの方のREADMEでは /workdir
になっていたのでわかって良かったです。
# × $ docker run -it --rm \ -e SAKURACLOUD_ACCESS_TOKEN=[さくらのクラウド APIトークン] \ -e SAKURACLOUD_ACCESS_TOKEN_SECRET=[さくらのクラウド APIシークレット] \ -v $PWD:/work \ sacloud/terraform apply # ◯ $ docker run -it --rm \ -e SAKURACLOUD_ACCESS_TOKEN=[さくらのクラウド APIトークン] \ -e SAKURACLOUD_ACCESS_TOKEN_SECRET=[さくらのクラウド APIシークレット] \ -v $PWD:/workdir \ sacloud/terraform apply
追記
直しました。ご指摘ありがとうございました!https://t.co/1KRBnZcSNB
— やまもと@反省から学びへ (@yamamoto_febc) 2017年10月27日
書いた直後に中の方が直してくださいました!
すごい!はやい!
今ドキュメントの方は動く形になっているかと思います
追記ここまで
下記がapplyしてdestroyまで実行したときの流れです。
はじめにinitする必要があるようでした。
### terraform plan $make token=xxx secret=yyy plan docker run -it --rm \ -e SAKURACLOUD_ACCESS_TOKEN=xxx \ -e SAKURACLOUD_ACCESS_TOKEN_SECRET=yyy \ -v /Users/a-tanaka/sakura:/workdir \ sacloud/terraform plan Plugin reinitialization required. Please run "terraform init". Reason: Could not satisfy plugin requirements. Plugins are external binaries that Terraform uses to access and manipulate resources. The configuration provided requires plugins which can't be located, don't satisfy the version constraints, or are otherwise incompatible. 1 error(s) occurred: * provider.sakuracloud: new or changed plugin executable Terraform automatically discovers provider requirements from your configuration, including providers used in child modules. To see the requirements and constraints from each module, run "terraform providers". error satisfying plugin requirements make: *** [plan] Error 1 # terraform init $make token=xxx secret=yyy init docker run -it --rm \ -e SAKURACLOUD_ACCESS_TOKEN=xxx \ -e SAKURACLOUD_ACCESS_TOKEN_SECRET=yyy \ -v /Users/a-tanaka/sakura:/workdir \ sacloud/terraform init Initializing provider plugins... 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. # terraform plan $make token=xxx secret=yyy plan docker run -it --rm \ -e SAKURACLOUD_ACCESS_TOKEN=xxx \ -e SAKURACLOUD_ACCESS_TOKEN_SECRET=yyy \ -v /Users/a-tanaka/sakura:/workdir \ sacloud/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. data.sakuracloud_archive.ubuntu: Refreshing state... ------------------------------------------------------------------------ An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: + sakuracloud_disk.disk01 id: <computed> connector: "virtio" graceful_shutdown_timeout: "60" name: "disk01" password: <sensitive> plan: "ssd" server_id: <computed> size: "20" source_archive_id: "112901206585" zone: <computed> + sakuracloud_server.server01 id: <computed> cdrom_id: <computed> core: "1" disks.#: <computed> dns_servers.#: <computed> gateway: <computed> graceful_shutdown_timeout: "60" interface_driver: "virtio" ipaddress: <computed> macaddresses.#: <computed> memory: "1" name: "server01" nic: "shared" nw_address: <computed> nw_mask_len: <computed> packet_filter_ids.#: <computed> tags.#: "1" tags.0: "@virtio-net-pci" zone: <computed> Plan: 2 to add, 0 to change, 0 to destroy. ------------------------------------------------------------------------ Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run. # terraform apply $make token=xxx secret=yyy apply docker run -it --rm \ -e SAKURACLOUD_ACCESS_TOKEN=xxx \ -e SAKURACLOUD_ACCESS_TOKEN_SECRET=yyy \ -v /Users/a-tanaka/sakura:/workdir \ sacloud/terraform apply data.sakuracloud_archive.ubuntu: Refreshing state... sakuracloud_disk.disk01: Creating... connector: "" => "virtio" graceful_shutdown_timeout: "" => "60" name: "" => "disk01" password: "<sensitive>" => "<sensitive>" plan: "" => "ssd" server_id: "" => "<computed>" size: "" => "20" source_archive_id: "" => "112901206585" zone: "" => "<computed>" sakuracloud_disk.disk01: Still creating... (10s elapsed) sakuracloud_disk.disk01: Still creating... (20s elapsed) sakuracloud_disk.disk01: Still creating... (30s elapsed) sakuracloud_disk.disk01: Still creating... (40s elapsed) sakuracloud_disk.disk01: Still creating... (50s elapsed) sakuracloud_disk.disk01: Still creating... (1m0s elapsed) sakuracloud_disk.disk01: Still creating... (1m10s elapsed) sakuracloud_disk.disk01: Still creating... (1m20s elapsed) sakuracloud_disk.disk01: Creation complete after 1m28s (ID: 112901361233) sakuracloud_server.server01: Creating... cdrom_id: "" => "<computed>" core: "" => "1" disks.#: "" => "1" disks.0: "" => "112901361233" dns_servers.#: "" => "<computed>" gateway: "" => "<computed>" graceful_shutdown_timeout: "" => "60" interface_driver: "" => "virtio" ipaddress: "" => "<computed>" macaddresses.#: "" => "<computed>" memory: "" => "1" name: "" => "server01" nic: "" => "shared" nw_address: "" => "<computed>" nw_mask_len: "" => "<computed>" packet_filter_ids.#: "" => "<computed>" tags.#: "" => "1" tags.0: "" => "@virtio-net-pci" zone: "" => "<computed>" sakuracloud_server.server01: Still creating... (10s elapsed) sakuracloud_server.server01: Still creating... (20s elapsed) sakuracloud_server.server01: Creation complete after 29s (ID: 112901361336) Apply complete! Resources: 2 added, 0 changed, 0 destroyed. # terraform destroy $make token=xxx secret=yyy destroy docker run -it --rm \ -e SAKURACLOUD_ACCESS_TOKEN=xxx \ -e SAKURACLOUD_ACCESS_TOKEN_SECRET=yyy \ -v /Users/a-tanaka/sakura:/workdir \ sacloud/terraform destroy data.sakuracloud_archive.ubuntu: Refreshing state... sakuracloud_disk.disk01: Refreshing state... (ID: 112901361233) sakuracloud_server.server01: Refreshing state... (ID: 112901361336) An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: - sakuracloud_disk.disk01 - sakuracloud_server.server01 Plan: 0 to add, 0 to change, 2 to destroy. Do you really want to destroy? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes sakuracloud_server.server01: Destroying... (ID: 112901361336) sakuracloud_server.server01: Still destroying... (ID: 112901361336, 10s elapsed) sakuracloud_server.server01: Destruction complete after 15s sakuracloud_disk.disk01: Destroying... (ID: 112901361233) sakuracloud_disk.disk01: Destruction complete after 2s Destroy complete! Resources: 2 destroyed.
destroy前コンソールを見たら作成されていました。
さくっと作れるので、これで設定書いてISUCON7予選の環境を再現していきたいです。
2ファイルしかないのでコピペで十分そうですが、
使ったMakefileなどはここに置きました。