Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[Terraform] Lesson 4: Google Compute Engine (VM – Virtual Machine) with Terraform

Posted on April 10, 2022April 25, 2022 By nim No Comments on [Terraform] Lesson 4: Google Compute Engine (VM – Virtual Machine) with Terraform

Contents

Toggle
  • 1) Create VPC from Console
  • 2) Hands-on Compute Engine with Terraform
    • 2.1) Compute Engine + Update Compute Engine parameter + Service Account – Terraform
      • 2.1.1) Finding out a suitable machine_type!
      • 2.1.2) service_account in google_compute_instance
    • 2.2) Attach Disk – Terraform
  • 2.3) Destroy resources

1) Create VPC from Console

Giờ chúng ta tạo VM trên cloud.
ghê hem!!!!

Giờ chúng ta tạo 1 VM
chọn lại region.
VM của bạn sẽ năm ở đâu?
Chỗ này đơn giản là bạn chọn VM dòng trung bình, hay dòng chíp xịn hay hay dòng nhiều Ram, Dòng có GPU
ngoài ra bạn chọn lại là VM của bạn chạy bao nhiều CPU or bao nhiều GB RAM
Tiếp theo bạn change các thông tin liên quan đến boot disk
Như trên là mình chọn ubuntu 20
chạy disk SSD luôn!
Tiếp tục click vào đây để cấu hình network, disk data,…
Chỗ này mình sẽ chọn các VPC
mà mình đã tạo ở bài trước!

Giờ các bạn có thể click create rồi

Anh em cần stop vm thế là chúng ta không bị trừ tiền
mình nhớ câu pay at you go

2) Hands-on Compute Engine with Terraform

Giờ mình sẽ cung cấp file terraform full:

resource "google_compute_instance" "vm-from-tf" {
  name = "vm-from-tf"
  zone = "asia-southeast1-a"
  machine_type = "n1-standard-2"

  allow_stopping_for_update = true

  network_interface {
    network = "custom-vpc-tf"
    subnetwork = "sub-sg"
  }

  boot_disk {
    initialize_params {
      image = "debian-9-stretch-v20210916"
      size = 35
      
    }
    auto_delete = false
  }

  labels = {
    "env" = "tfleaning"
  }

  
  scheduling {
    preemptible = false
    automatic_restart = false
  }
  
  service_account {
    email = "terraform-gcp@terraform-gcp-326702.iam.gserviceaccount.com"
    scopes = [ "cloud-platform" ]
  }

  lifecycle {
    ignore_changes = [
      attached_disk
    ]
  }

}

resource "google_compute_disk" "disk-1" {
  name = "disk-1"
  size = 15
  zone = "asia-southeast1-a"
  type = "pd-ssd"
}

resource "google_compute_attached_disk" "adisk" {
  disk = google_compute_disk.disk-1.id
  instance = google_compute_instance.vm-from-tf.id
}

2.1) Compute Engine + Update Compute Engine parameter + Service Account – Terraform

resource "google_compute_instance" "vm-from-tf" {
  name = "vm-from-tf"
  zone = "asia-southeast1-a"
  machine_type = "n1-standard-2"

  allow_stopping_for_update = true

  network_interface {
    network = "custom-vpc-tf"
    subnetwork = "sub-sg"
  }

  boot_disk {
    initialize_params {
      image = "debian-9-stretch-v20210916"
      size = 35
      
    }
    auto_delete = false
  }

  labels = {
    "env" = "tfleaning"
  }

  
  scheduling {
    preemptible = false
    automatic_restart = false
  }

  lifecycle {
    ignore_changes = [
      attached_disk
    ]
  }

}

Giờ thực hiện: terraform init và terraform apply

2.1.1) Finding out a suitable machine_type!

Links:
https://cloud.google.com/compute/docs/machine-types

machine_type – (Required) The machine type to create.

Note: If you want to update this value (resize the VM) after initial creation, you must set allow_stopping_for_update to true.

Khi bạn change machine type thì bạn nên set allow_stopping_for_update = true
allow_stopping_for_update – (Optional) If true, allows Terraform to stop the instance to update its properties. If you try to update a property that requires stopping the instance without setting this field, the update will fail.

Chúng ta đã tạo được VM trên GCP bằng terraform
Chúng ta đã tạo được VM giờ thử SSH
Connection via Cloud Identity-Aware Proxy Failed

Lỗi này thì do VM của chúng ta chưa mở SSH

Bạn nhấn vào tên VM xong lăn xuống kiếm phần network
Giờ để lẹ thì chúng ta chỉnh bằng UI luôn
còn nếu đúng chúng nên quay lại terraform VPC để update firewall rule bên đó
Giờ là chúng ta tạo xong 1 firewall rule

Giờ quay trở lại VM và mở console ssh

Mở được console òi

gcloud auth list

Giờ gõ thử command bên trên:

Chỗ này chúng ta sử dụng service_account

2.1.2) service_account in google_compute_instance

service_account – (Optional) Service account to attach to the instance. Structure is documented below. Note: allow_stopping_for_update must be set to true or your instance must have a desired_status of TERMINATED in order to update this field.

The service_account block supports:

  • email – (Optional) The service account e-mail address. If not given, the default Google Compute Engine service account is used. Note: allow_stopping_for_update must be set to true or your instance must have a desired_status of TERMINATED in order to update this field.
  • scopes – (Required) A list of service scopes. Both OAuth2 URLs and gcloud short names are supported. To allow full access to all Cloud APIs, use the cloud-platform scope. See a complete list of scopes here. Note: allow_stopping_for_update must be set to true or your instance must have a desired_status of TERMINATED in order to update this field.
resource "google_compute_instance" "vm-from-tf" {
  name = "vm-from-tf"
  zone = "asia-southeast1-a"
  machine_type = "n1-standard-2"

  allow_stopping_for_update = true

  network_interface {
    network = "custom-vpc-tf"
    subnetwork = "sub-sg"
  }

  boot_disk {
    initialize_params {
      image = "debian-9-stretch-v20210916"
      size = 35
      
    }
    auto_delete = false
  }

  labels = {
    "env" = "tfleaning"
  }

  
  scheduling {
    preemptible = false
    automatic_restart = false
  }
  
  service_account {
    email = "terraform-gcp@terraform-gcp-346216.iam.gserviceaccount.com"
    scopes = [ "cloud-platform" ]
  }

  lifecycle {
    ignore_changes = [
      attached_disk
    ]
  }

}

Khi mà gõ terraform apply nếu bạn gặp lỗi bên dưới

Error: Error waiting for updating service account: The user does not have access to service account ‘terraform-gcp@terraform-gcp-346216.iam.gserviceaccount.com’. User: ‘terraform-gcp@terraform-gcp-346216.iam.gserviceaccount.com’. Ask a project owner to grant you the iam.serviceAccountUser role on the service account

Bạn để ý phần mà tô đỏ

bạn cần add thêm role
Giờ đã chạy được!
Giờ start lại VM

2.2) Attach Disk – Terraform

Khác với việc cài VM trên esxi của vmware trên on-premise.

Thì khi bạn tạo VM trên cloud phần disk sẽ có phần boot và phần data riêng.

Giờ chúng ta cần add thêm data disk.

resource "google_compute_instance" "vm-from-tf" {
  name = "vm-from-tf"
  zone = "asia-southeast1-a"
  machine_type = "n1-standard-2"

  allow_stopping_for_update = true

  network_interface {
    network = "custom-vpc-tf"
    subnetwork = "sub-sg"
  }

  boot_disk {
    initialize_params {
      image = "debian-9-stretch-v20210916"
      size = 35
      
    }
    auto_delete = false
  }

  labels = {
    "env" = "tfleaning"
  }

  
  scheduling {
    preemptible = false
    automatic_restart = false
  }
  
  service_account {
    email = "terraform-gcp@terraform-gcp-346216.iam.gserviceaccount.com"
    scopes = [ "cloud-platform" ]
  }

  lifecycle {
    ignore_changes = [
      attached_disk
    ]
  }

}

resource "google_compute_disk" "disk-1" {
  name = "disk-1"
  size = 15
  zone = "asia-southeast1-a"
  type = "pd-ssd"
}

resource "google_compute_attached_disk" "adisk" {
  disk = google_compute_disk.disk-1.id
  instance = google_compute_instance.vm-from-tf.id
}

Bạn tham khảo google_compute_disk và google_compute_attached_disk để khiểu được cái khai báo

Giờ lại terraform apply tiếp

thông qua kết quả console thì đã add được disk.
trên web site đã thấy add local disk

2.3) Destroy resources

Để delete các resources do terraform tạo ra chúng ta cần gõ:

terraform destroy
GCP - Google Cloud, Terraform

Post navigation

Previous Post: [Java-script] adding HTML by Javascript based on a specified template.
Next Post: [Terraform] Lesson 5: Google Cloud Run with Terraform

More Related Articles

[Terraform] Lesson 8: Cloud PubSub with Terraform GCP - Google Cloud
[DevSecOps] Scan Terraform DevSecOps
[AWS] – Terraform Beginner – Lesson 1: Create a Free Account of AWS AWS - Amazon Web Service
[Terraform] Use “IF/ELSE” through the Merge function in Terraform Terraform
[Terraform] – Terraform Beginner – Lesson 2: Working with Terraform. AWS - Amazon Web Service
[Terraform] DRY in Terraform Configurations With Dynamic Blocks Terraform

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [Laravel] Laravel Helpful June 26, 2025
  • [VScode] Hướng dẫn điều chỉnh font cho terminal June 20, 2025
  • [WordPress] Hướng dấn gửi mail trên WordPress thông qua gmail. June 15, 2025
  • [Bitbucket] Git Clone/Pull/Push with Bitbucket through API Token. June 12, 2025
  • [Teamcity] How to transfer the value from pipeline A to pipeline B June 9, 2025

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.