{Terraform} VPC/GCP

 

-- 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_compute_network" "vpc01" {
  name = "vpc01"
  auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "subnet01" {
  name = "subnet01"
  ip_cidr_range = "10.2.0.0/16"
  network = google_compute_network.vpc01.id
  private_ip_google_access =true
}


-- 4. terraform 実行


gcloud auth application-default login
terraform init

terraform plan
terraform apply

terraform state list

terraform state show google_compute_network.vpc01


-- 5. GCPクリーンアップ

terraform destroy


gcloud projects list

gcloud projects delete project01-9999999 \
--quiet