Step1:
Firstly, we will create project from GCP,



Step2:
Create new Service Account and give permission to create VM on GCP.


Add roles like below.



Step4:
After above action, click the your sa name and go to the keys area and click the add keys section.


Step5:
Go To the your Terraform machine and add file for sa token.
mkdir GCP-VM
cd GCP-VM
vi gcp-sa-token.json

Create main.tf file and codes like below.
vi main.tf
provider "google" {
credentials = file("gcp-sa-token.json")
project = "terraform-project-466022"
region = "europe-west1"
zone = "europe-west1-b"
}
# Create custom VPC
resource "google_compute_network" "vpc" {
name = "test-vpc-for-vm"
auto_create_subnetworks = false
routing_mode = "GLOBAL"
}
# Create public subnet
resource "google_compute_subnetwork" "test_public_subnet" {
name = "test-public-subnet-1"
ip_cidr_range = "10.10.1.0/24"
network = google_compute_network.vpc.id
region = "europe-west1"
}
# Firewall rule to allow HTTP
resource "google_compute_firewall" "allow-http" {
name = "test-fw-allow-http"
network = google_compute_network.vpc.id
allow {
protocol = "tcp"
ports = ["80"]
}
source_ranges = ["0.0.0.0/0"]
target_tags = ["http"]
}
# Firewall rule to allow HTTPS
resource "google_compute_firewall" "allow-https" {
name = "test-fw-allow-https"
network = google_compute_network.vpc.id
allow {
protocol = "tcp"
ports = ["443"]
}
source_ranges = ["0.0.0.0/0"]
target_tags = ["https"]
}
# Firewall rule to allow SSH
resource "google_compute_firewall" "allow-ssh" {
name = "vm-fw-allow-ssh"
network = google_compute_network.vpc.id
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = ["0.0.0.0/0"]
target_tags = ["ssh"]
}
# Firewall rule to allow RDP
resource "google_compute_firewall" "allow-rdp" {
name = "vm-fw-allow-rdp"
network = google_compute_network.vpc.id
allow {
protocol = "tcp"
ports = ["3389"]
}
source_ranges = ["0.0.0.0/0"]
target_tags = ["rdp"]
}
# Compute instance in the public subnet
resource "google_compute_instance" "test_vm_instance_public" {
name = "test-vm-instance-terraform"
machine_type = "f1-micro"
zone = "europe-west1-b"
tags = ["ssh", "http"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
network = google_compute_network.vpc.id
subnetwork = google_compute_subnetwork.test_public_subnet.name
# Assign public IP
access_config {}
}
}
terraform init
terraform apply
Please type to “yes”

Step6:
Check the vm instances from your GCP account.

Thats’s it. Have nice works…
No responses yet