16. $ terraform init
Initializing provider plugins...
- Checking for available provider plugins on
https://releases.hashicorp.com...
- Downloading plugin for provider "azurerm" (1.5.0)...
[snip]
Terraform has been successfully initialized!
[snip]
リソース操作体験 #3
terraform initコマンドで初期化しま
す
17. $ ll
total 1
drwxrwxrwx 2 root root 0 May 20 01:45 ./
drwxrwxrwx 2 root root 0 May 20 01:45 ../
-rwxrwxrwx 1 root root 121 May 20 02:22 main.tf*
drwxrwxrwx 2 root root 0 May 20 02:30 .terraform/
$ ll .terraform/plugins/linux_amd64/
total 40331
drwxrwxrwx 2 root root 0 May 20 02:30 ./
drwxrwxrwx 2 root root 0 May 20 02:30 ../
-rwxrwxrwx 1 root root 83 May 20 02:30 lock.json*
-rwxrwxrwx 1 root root 41297888 May 20 02:30 terraform-provider-
azurerm_v1.5.0_x4
リソース操作体験 #4
initで何が起こったのか、ディレクト
リを確認します
.terraformディレクトリが作られまし
た
“azurerm”プラグインがインストール
されたことが分かります
18. $ terraform plan
Refreshing Terraform state in-memory prior to plan...
[snip]
Terraform will perform the following actions:
+ azurerm_resource_group.ex01
id: <computed>
location: "japaneast"
name: "tfbc-ex01-rg"
tags.%: <computed>
Plan: 1 to add, 0 to change, 0 to destroy.
[snip]
リソース操作体験 #5
terraform planコマンドで実行計画を
作成します
この計画を実行すると、リソースを1
つaddできることが分かります
change、destroy対象はありません
“<computed>” は、実行時に決定さ
れる値です
“-out”パラメーターで保管しておか
ないと、実行時にこの計画通りにで
きるか保証はないよ、とか言われま
すが、ひとまず無視でOK
19. $ az group list | grep ex01
$ terraform apply
[snip]
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
[snip]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
リソース操作体験 #6
Azure CLI(azコマンド)で、リソース
グループの存在を確認します
念のため、存在しないことを確認し
てください
ではいよいよterraform applyコマン
ドで実行します
実行していいかな?と聞かれるの
で、”yes”で答えます
無事にリソースを1つaddできたよう
です
20. $ az group list | grep ex01
tfbc-ex01-rg japaneast Succeeded
$ ll
total 2
drwxrwxrwx 2 root root 0 May 20 01:45 ./
drwxrwxrwx 2 root root 0 May 20 01:45 ../
-rwxrwxrwx 1 root root 121 May 20 02:22 main.tf*
drwxrwxrwx 2 root root 0 May 20 02:35 .terraform/
-rwxrwxrwx 1 root root 1152 May 20 02:37 terraform.tfstate*
$ cat terraform.tfstate
リソース操作体験 #7
Azure CLI(azコマンド)で、リソース
グループの存在を確認します
作成できたことが分かります
ディレクトリの変化を確認します
terraform.tfstateというファイルがで
きているはずです、これがリソース
の状態管理ファイルです
中身をのぞいてみてください
21. $ terraform destroy
[snip]
Terraform will perform the following actions:
- azurerm_resource_group.ex01
Plan: 0 to add, 0 to change, 1 to destroy.
Do you really want to destroy?
[snip]
Destroy complete! Resources: 1 destroyed.
リソース操作体験 #8
では terraform destroyコマンドでリ
ソースを削除してみましょう
対象のリソースを確認のうえ、”yes”
で答えます
削除できたようです
22. $ az group list | grep ex01
$ ll
total 3
drwxrwxrwx 2 root root 0 May 20 01:45 ./
drwxrwxrwx 2 root root 0 May 20 01:45 ../
-rwxrwxrwx 1 root root 121 May 20 02:22 main.tf*
drwxrwxrwx 2 root root 0 May 20 02:35 .terraform/
-rwxrwxrwx 1 root root 317 May 20 02:39 terraform.tfstate*
-rwxrwxrwx 1 root root 1152 May 20 02:39 terraform.tfstate.backup*
$ cat terraform.tfstate
$ cat terraform.tfstate.backup
リソース操作体験 #8
Azure CLIで、リソースグループを削
除できたことを確認します
ディレクトリ内の変化も確認します
terraform.tfstate.backupというファ
イルが増えています
.tfstateの変化と、.backupの中身を
確認してみましょう
おめでとうございます、Terraformの
ライフサイクルを体験できました!
23. $ terraform plan -out=./terraform.tfplan
[snip]
Plan: 1 to add, 0 to change, 0 to destroy.
------------------------------------------------------------------
------
This plan was saved to: ./terraform.tfplan
To perform exactly these actions, run the following command to
apply:
terraform apply "./terraform.tfplan"
リソース操作体験 #9
ところでplan時に「この計画が実行
されるかは保証できない」と言われ
たのを覚えているでしょうか
たしかにplanからapplyまでの間に、
誰かがファイルを編集してしまう可
能性がなくはない
今後planするたびに目にするメッ
セージなので、スッキリしておきま
しょう
-outオプションでplanを実行します
実行計画がファイルに保存されます
27. .gitignoreの例
除外を忘れずに
• GitHubにTerraformむけ.gitignoreのサンプル
があります
• 見せたくない定義が入っているファイルは確
実に除外しましょう
• また、.tfstate系ファイルにはIDなどリソース
の詳細情報が入るので、除外しましょう
• 例にある.tfvarsファイルは変数指定の別手法
ですが、今回は触れません
# Local .terraform directories
**/.terraform/*
# .tfstate files
*.tfstate
*.tfstate.*
# Ignore any .tfvars files that are generated
automatically for each Terraform run. Most
# .tfvars files are managed as part of
configuration and so should be included in
# version control.
#
# example.tfvars