Closed6

GCPでterraform使うメモ

nabeyangnabeyang

参考にしている本のリポジトリ
https://github.com/toshi0607/Learning-Terraform-with-GCP

本が使ってるバージョンをインストール。古いのでarm版無い。rosetta2を有効にして、x86_64版のbrew -> asdf -> terraform入れてからやり直した。多分、rosetta2必要。

$ ASDF_HASHICORP_OVERWRITE_ARCH=amd64 asdf install terraform 0.13.0
$ asdf local terraform 0.13.0
$ terraform version

Your version of Terraform is out of date! The latest version
is 1.1.9. You can update by downloading from https://www.terraform.io/downloads.html
Terraform v0.13.0

参考
https://zenn.dev/kentama/articles/78d4caae4c6307

nabeyangnabeyang

gcloud入れる

$ brew install --cask google-cloud-sdk
$ echo "source /opt/homebrew/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc" >> ~/.zshrc
$ echo "source /opt/homebrew/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc" >> ~/.zshrc
$ gcloud init

サービスの使用の許可

$ gcloud services enable compute.googleapis.com
Operation "xxxxx" finished successfully.
$ gcloud services enable storage-component.googleapis.com
$ gcloud services enable storage.googleapis.com

terraformで設定変える時必要

$ gcloud auth application-default login
nabeyangnabeyang

インスタンス作る

GOOGLE_PROJECTの環境変数の設定なしでterraform applyしてyesとタイプする。

google_compute_instance.default: Creating...

Error: project: required field is not set

  on main.tf line 1, in resource "google_compute_instance" "de

今度はこんなエラー。asia-northeast1-aを変えたら良いのかな?

Error: Error waiting for instance to create: The zone 'projects/$PROJECT_ID/zones/asia-northeast1-a' does not have enough resources available to fulfill the request.  Try a different zone, or try again later.

asia-northeast1-bにしたら出来た。

google_compute_instance.default: Creating...
google_compute_instance.default: Still creating... [10s elapsed]
google_compute_instance.default: Creation complete after 15s [id=projects/$PROJECT_ID/zones/asia-northeast1-b/instances/test]

terraform destroyで削除。作るのに比べて時間かかる。

google_compute_instance.default: Destroying... [id=projects/$GOOGLE_PROJECT/zones/asia-northeast1-b/instances/test]
google_compute_instance.default: Still destroying... [id=projects/$GOOGLE_PROJECT/zones/asia-northeast1-b/instances/test, 10s elapsed]
google_compute_instance.default: Still destroying... [id=projects/$GOOGLE_PROJECT/zones/asia-northeast1-b/instances/test, 20s elapsed]
google_compute_instance.default: Still destroying... [id=projects/$GOOGLE_PROJECT/zones/asia-northeast1-b/instances/test, 30s elapsed]
google_compute_instance.default: Still destroying... [id=projects/$GOOGLE_PROJECT/zones/asia-northeast1-b/instances/test, 40s elapsed]
google_compute_instance.default: Still destroying... [id=projects/$GOOGLE_PROJECT/zones/asia-northeast1-b/instances/test, 50s elapsed]
google_compute_instance.default: Still destroying... [id=projects/$GOOGLE_PROJECT/zones/asia-northeast1-b/instances/test, 1m0s elapsed]
google_compute_instance.default: Still destroying... [id=projects/$GOOGLE_PROJECT/zones/asia-northeast1-b/instances/test, 1m10s elapsed]
google_compute_instance.default: Still destroying... [id=projects/$GOOGLE_PROJECT/zones/asia-northeast1-b/instances/test, 1m20s elapsed]
google_compute_instance.default: Still destroying... [id=projects/$GOOGLE_PROJECT/zones/asia-northeast1-b/instances/test, 1m30s elapsed]
google_compute_instance.default: Still destroying... [id=projects/$GOOGLE_PROJECT/zones/asia-northeast1-b/instances/test, 1m40s elapsed]
google_compute_instance.default: Still destroying... [id=projects/$GOOGLE_PROJECT/zones/asia-northeast1-b/instances/test, 1m50s elapsed]
nabeyangnabeyang

サービスアカウント作る

多分、これ有効にする必要がある

$ gcloud services enable iam.googleapis.com 
Operation "operations/xxxx" finished successfully.

tf-state保存用にバケット作る

$ gsutil mb -l ASIA-NORTHEAST1 gs://tf-state-xxxxxxxx
Creating gs://gs://tf-state-xxxxxxxx/...

バケット名は実行時に渡すことにする。

terraform init -backend-config="tf-state-xxxxxxxx"
このスクラップは2022/06/04にクローズされました