1) Create VPC from Console
Giờ chúng ta tạo VM trên cloud.
ghê hem!!!!
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-167.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-168.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-169.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-170.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-171.png)
VM của bạn sẽ năm ở đâu?
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-172.png)
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
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-173.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-174.png)
chạy disk SSD luôn!
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-175.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-176.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-177.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-178.png)
mà mình đã tạo ở bài trước!
Giờ các bạn có thể click create rồi
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-179.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-180.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-181.png)
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!
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-182.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-183.png)
Links:
https://cloud.google.com/compute/docs/machine-types
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-184.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-185.png)
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.
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-186.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-187.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-188.png)
Lỗi này thì do VM của chúng ta chưa mở SSH
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-189.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-190.png)
còn nếu đúng chúng nên quay lại terraform VPC để update firewall rule bên đó
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-192.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-193.png)
Giờ quay trở lại VM và mở console ssh
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-194.png)
gcloud auth list
Giờ gõ thử command bên trên:
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-195.png)
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 adesired_status
ofTERMINATED
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 thecloud-platform
scope. See a complete list of scopes here. Note:allow_stopping_for_update
must be set to true or your instance must have adesired_status
ofTERMINATED
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ô đỏ
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-196.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-197.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-198.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-199.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-200.png)
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
}
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-201.png)
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
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-202.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-203.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-204.png)
2.3) Destroy resources
Để delete các resources do terraform tạo ra chúng ta cần gõ:
terraform destroy
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-206.png)
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-207.png)