LarryDpk
发布于 2021-11-15 / 1607 阅读
0

Three ways to create GCE on GCP(Console,gcloud,Terraform)

1 Introduction

We always need to create the GCE(Google Compute Engine) if we use GCP. There are many ways to create GCE:

(1) Create on Console

(2) Create with gcloud

(3) Create with Terraform

For GCP initiation, can go to: How to initiate the GCP project and use gcloud to access.

2 GCP Console

Logon the GCP console and click the button to create GCE VM, select what you need as below:

3 gcloud

On the GCP console, before you create the VM, you can get the equivalent command line as gcloud one:

We can just run the command to create:

$ gcloud compute instances create pkslow-vm \
--project=pkslow \
--zone=us-west1-a \
--machine-type=e2-micro \
--network-interface=network-tier=PREMIUM,subnet=default \
--maintenance-policy=MIGRATE \
--service-account=admin-for-all@pkslow.iam.gserviceaccount.com \
--scopes=https://www.googleapis.com/auth/cloud-platform \
--tags=http-server,https-server \
--create-disk=auto-delete=yes,boot=yes,device-name=instance-1,image=projects/centos-cloud/global/images/centos-8-v20211105,mode=rw,size=20,type=projects/pkslow/zones/us-west1-a/diskTypes/pd-standard \
--no-shielded-secure-boot \
--shielded-vtpm \
--shielded-integrity-monitoring \
--reservation-affinity=any

Created [https://www.googleapis.com/compute/v1/projects/pkslow/zones/us-west1-a/instances/pkslow-vm].
NAME       ZONE        MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
pkslow-vm  us-west1-a  e2-micro                   10.138.0.5   34.145.124.xxx  RUNNING

                10.138.0.5   34.145.124.xxx  RUNNING

Check the instance:

$ gcloud compute instances list
NAME       ZONE        MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
pkslow-vm  us-west1-a  e2-micro   

4 Terraform

We can use the Terraform to manage Infrastucture:

provider "google" {
  project     = "pkslow"
}

resource "google_compute_instance" "test" {
  name         = "pkslow-test"
  machine_type = "e2-micro"
  zone         = "us-west1-a"

  tags = ["http-server", "https-server"]

  boot_disk {
    initialize_params {
      image = "projects/centos-cloud/global/images/centos-8-v20211105"
    }
  }


  network_interface {
    network = "default"

    access_config {
      // Ephemeral public IP
    }
  }

  metadata = {
    foo = "bar"
  }

  metadata_startup_script = "echo hi > /test.txt"

  service_account {
    # Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
    email  = "admin-for-all@pkslow.iam.gserviceaccount.com"
    scopes = ["cloud-platform"]
  }
}
$ gcloud compute instances list
NAME         ZONE        MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
pkslow-test  us-west1-a  e2-micro                   10.138.0.6   34.83.138.xxx   RUNNING
pkslow-vm    us-west1-a  e2-micro                   10.138.0.5   34.145.124.xxx  RUNNING

5 Code

Code on GitHub: https://github.com/LarryDpk/pkslow-samples


Reference:

Terraform GCP Compute Instance