How to create GCP instance with terraform

First you want to install terraform on Linux OS, mine is ubuntu 20.04.

How to install terraform on ubuntu 20.04

Then you should install gcloud cli for operation of google GCP.

Install gcloud CLI

Next, init your GCP account to log into the system.

 $ gcloud init

In GCP web interface, create your first project and create an user for managing the project with "project editor" permissions.

And, generate a key which is a JSON file for that user. The key will be downloaded automatically to your computer.

Now, we would run the terraform jobs. Create a project dir and step into this dir.

 $ mkdir project
$ cd project

Create the file "main.tf" which has the content as follows.

 terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "3.5.0"
}
}
}

provider "google" {
credentials = file("/home/xxx/project/hostcache.json")

project = "blissful-canyon-xxx"
region = "us-central1"
zone = "us-central1-c"
}

resource "google_compute_network" "vpc_network" {
name = "terraform-network"
}

resource "google_compute_instance" "vm_instance" {
name = "terraform-instance"
machine_type = "f1-micro"

boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}

network_interface {
network = google_compute_network.vpc_network.name
access_config {
}
}
}

This terraform configuration defines a network VPC whose name is "terraform-network", and specifies a VM instance whose name is "terraform-instance" under that VPC. You should provide the script with correct key file in option "credentials" above, and correct project name in option "project".

Next, run the following command to validate configuration.

 $ terraform validate
Success! The configuration is valid.

Then, init terraform to download the plugin "google".

 $ terraform init
Initializing the backend...

The last step is "apply" configuration to implement the deployment to GCP.

 $ terraform apply

Go to GCP web interface you will see the new VPC and new VM instance have got deployed.

Don't forget to create a firewall rule in web interface for this VPC which allow incoming traffic to SSH port etc.

Finally, we can log into new VM instance by the following command.

 $ gcloud compute ssh --zone "us-central1-c" "terraform-instance"  --project "blissful-canyon-xxx"

I made a yabs benchmark to this GCP instance, the results are follows.

 Basic System Information:
---------------------------------
Uptime : 0 days, 0 hours, 24 minutes
Processor : Intel(R) Xeon(R) CPU @ 2.30GHz
CPU cores : 1 @ 2299.998 MHz
AES-NI : ✔ Enabled
VM-x/AMD-V : ❌ Disabled
RAM : 571.9 MiB
Swap : 0.0 KiB
Disk : 9.7 GiB
Distro : Debian GNU/Linux 11 (bullseye)
Kernel : 5.10.0-19-cloud-amd64

fio Disk Speed Tests (Mixed R/W 50/50):
---------------------------------
Block Size | 4k (IOPS) | 64k (IOPS)
------ | --- ---- | ---- ----
Read | 407.00 KB/s (101) | 5.69 MB/s (89)
Write | 428.00 KB/s (107) | 6.03 MB/s (94)
Total | 835.00 KB/s (208) | 11.73 MB/s (183)
| |
Block Size | 512k (IOPS) | 1m (IOPS)
------ | --- ---- | ---- ----
Read | 27.55 MB/s (53) | 36.09 MB/s (35)
Write | 29.47 MB/s (57) | 38.68 MB/s (37)
Total | 57.03 MB/s (110) | 74.78 MB/s (72)

iperf3 Network Speed Tests (IPv4):
---------------------------------
Provider | Location (Link) | Send Speed | Recv Speed | Ping
----- | ----- | ---- | ---- | ----
Clouvider | London, UK (10G) | 526 Mbits/sec | 1.29 Gbits/sec | 95.4 ms
Scaleway | Paris, FR (10G) | 911 Mbits/sec | 1.18 Gbits/sec | 103 ms
NovoServe | North Holland, NL (40G) | 915 Mbits/sec | 1.21 Gbits/sec | 99.5 ms
Uztelecom | Tashkent, UZ (10G) | 799 Mbits/sec | 607 Mbits/sec | 188 ms
Clouvider | NYC, NY, US (10G) | 962 Mbits/sec | busy | 26.8 ms
Clouvider | Dallas, TX, US (10G) | 962 Mbits/sec | 5.86 Gbits/sec | 20.2 ms
Clouvider | Los Angeles, CA, US (10G) | 951 Mbits/sec | 2.72 Gbits/sec | 45.5 ms

Geekbench 5 test failed. Run manually to determine cause.

Maybe due to it's a micro instance, it has only 512 MB memory and the disk IO is really worse.

For now you have got a VPC network and a VM instance provided by GCP via the deployment of terraform.