[A-00204] Terraform入門(GoogleCloud)

googlecloudでterraformの使用方法についてメモ

・ComputeEngin作ってみる

アーキテクチャは下記の通りです。

PROJECT_ID = "<project-id>"
REGION     = "asia-northeast1"
variable "PROJECT_ID" {
  type = string
}

variable "REGION" {
  type = string
}
terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "5.34.0"
    }
  }
}

provider "google" {
  project = var.PROJECT_ID
  region  = var.REGION
}
resource "google_compute_network" "vpc1" {
  name                    = "vpc1"
  auto_create_subnetworks = "false"
}

resource "google_compute_subnetwork" "subnet1" {
  name          = "subnet1"
  ip_cidr_range = "10.2.0.0/16"
  region        = var.REGION
  network       = google_compute_network.vpc1.id
}
resource "google_compute_instance" "instance1" {
  name         = "instance1"
  machine_type = "e2-micro"
  zone         = "asia-northeast1-a"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
      labels = {
        my_label = "value"
      }
    }
  }

  network_interface {
    network    = google_compute_network.vpc1.name
    subnetwork = google_compute_subnetwork.subnet1.name
    network_ip = "10.2.0.2"
    access_config {
    }
  }
}

下記のコマンドを実行して確認して後片付けします。

terraform init
terraform plan -var-file=gcp.tfvars
terraform apply -var-file=gcp.tfvars
terraform destroy -var-file=gcp.tfvars

・Appendix

参考文献はこちら

https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance

https://cloud.google.com/compute/docs/general-purpose-machines?hl=ja#e2_machine_types

https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_subnetwork

https://zenn.dev/oyasumipants/articles/8f0ac1a3395520

https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/getting_started

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

*