https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance
https://zenn.dev/nekoshita/articles/b6eab08861920c
https://dev.classmethod.jp/articles/google-cloud-advent-calendar-2021-18-terraform/
-- 1. macにTerraformインストール
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
brew update
brew upgrade hashicorp/tap/terraform
terraform -help
terraform -version
-- 2. GCPプロジェクト作成
gcloud init
gcloud auth list
gcloud --version
gcloud projects create project01-9999999 \
--name="project01"
gcloud config list
gcloud config set project project01-9999999
gcloud config set compute/region asia-northeast1 --quiet
gcloud config set compute/zone asia-northeast1-a --quiet
gcloud beta billing accounts list
gcloud beta billing projects link project01-9999999 --billing-account=111111-111111-111111
gcloud services enable compute.googleapis.com --project project01-9999999
-- 3. tfファイル作成
vim main.tf
provider "google" {
project = "project01-9999999"
region = "asia-northeast1"
}
resource "google_service_account" "sa99999999" {
account_id = "sa99999999"
display_name = "sa99999999"
}
resource "google_compute_instance" "vm01" {
name = "vm01"
machine_type = "e2-micro"
zone = "asia-northeast1-a"
tags = ["tag01"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
network = "default"
access_config {}
}
service_account {
email = google_service_account.sa99999999.email
scopes = ["cloud-platform"]
}
scheduling {
preemptible = true
automatic_restart = false
}
}
-- 4. terraform 実行
gcloud auth application-default login
terraform init
terraform plan
terraform apply
gcloud compute instances list
gcloud compute ssh vm01
-- 5. GCPクリーンアップ
terraform destroy
gcloud projects list
gcloud projects delete project01-9999999 \
--quiet