https://cloud.google.com/compute/docs/instances/changing-machine-type-of-stopped-instance
-- 1. プロジェクト作成
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
-- 2. VPC、サブネット
cat <<-'EOF' > 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.0.0.0/16"
network = google_compute_network.vpc01.id
private_ip_google_access =true
}
resource "google_service_account" "sa99999999" {
account_id = "sa99999999"
display_name = "sa99999999"
}
resource "google_compute_firewall" "fw01" {
name = "fw01"
network = google_compute_network.vpc01.name
direction = "INGRESS"
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = [
"0.0.0.0/0"
]
target_tags = ["tag01"]
}
resource "google_compute_firewall" "fw02" {
name = "fw02"
network = google_compute_network.vpc01.name
direction = "INGRESS"
allow {
protocol = "all"
}
source_ranges = [
"10.1.0.0/24"
]
target_tags = ["tag01"]
}
EOF
terraform init
terraform fmt
terraform -version
terraform plan
terraform apply -auto-approve
# terraform destroy -auto-approve
gcloud iam service-accounts list
gcloud compute instances create vm01 \
--machine-type=e2-micro \
--zone=asia-northeast1-a \
--tags=tag01 \
--image-project=centos-cloud \
--image=centos-7-v20221004 \
--subnet=subnet01 \
--service-account=sa99999999@project01-9999999.iam.gserviceaccount.com \
--scopes=cloud-platform
gcloud compute instances list
gcloud compute instances describe vm01
gcloud compute ssh vm01
gcloud compute instances delete vm01 --quiet
-- e2-micro -> vCPUs=2 Memory=1GB $0.010745/h
-- e2-standard-2 -> vCPUs=2 Memory=8GB $0.085964/h
gcloud compute instances stop vm01
gcloud compute instances set-machine-type vm01 \
--machine-type e2-standard-2
gcloud compute instances start vm01
gcloud compute instances stop vm01
gcloud compute instances set-machine-type vm01 \
--machine-type e2-micro
gcloud compute instances start vm01
-- 5. プロジェクト削除
gcloud projects list
gcloud projects delete project01-9999999 \
--quiet
gcloud beta billing projects unlink project01-9999999