Vapor Trail

明るく楽しく元気よく

さくらのクラウドをTerraformから作る

さくらのクラウドをTerraformから作る

わざわざTerraform使わずにコンパネからポチポチ作れるが、Terraform使ってみたかったしテスト環境とか複数の環境用意するときに手順忘れるので使ってみた。

sacloud.github.io

バージョン

CentOS7
Terraform v0.11.13
Terraform for さくらのクラウド v1.11

インストール

curl -L https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip > ~/terraform/terraform.zip
unzip terraform
echo export PATH=$PATH:~/terraform/ ~/.bash_profile

http://releases.usacloud.jp/terraform/

さくらのクラウド for terraform をterraformのディレクトリに入れる

mkdir sakura
vi sakura 
provider "sakuracloud" {
    token = "[ACCESS_TOKEN]"
    secret = "[ACCESS_TOKEN_SECRET]"
}
terraform init

動く

Initializing provider plugins...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.sakuracloud: version = "~> 1.11"

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.

qiita.com を参考に進める

環境変数に設定して使うこともできる
export SAKURACLOUD_ACCESS_TOKEN=
export SAKURACLOUD_ACCESS_TOKEN_SECRET=
export SAKURACLOUD_ZONE=tk1v
  • tk1a … 東京第1ゾーン
  • is1a … 石狩第1ゾーン
  • is1b … 石狩第2ゾーン
  • tk1v ... サンドボックス

SAKURACLOUD_ZONEをサンドボックスにすると、料金がかからないで作ったり壊したりできる。ありがたい。

manual.sakura.ad.jp

terraformの基本的なコマンド

terraform init
terraform validate
terraform plan
terraform apply
terraform destroy

  • グラフ化
terraform graph > input.dot
dot -Tpng <(terraform graph) -o dependency.png

参考