Creating a VM in GCE - Command-Line



In this tutorial, we are going to see how to create a VM instance through the command-line. If you want to check how to create a VM using the web console, please follow this article. To learn the Google Compute Engine internal concepts, please do check out this link.

Creating a set of VM instances can be easily achieved by the command-line utility method, in which the series of creating compute instance commands are passed and automate the execution through shell script for instance. 

In order to generate a base command for creating the VM, we can leverage the web console method and piggyback the configurations to command. 

This equivalent command to set up the VM with the exact settings that you have selected, would be available at the end of the VM creation page. I generally prefer this way to generate the command using a web console and multiple VM creations. 

gcloud beta compute --project=spartan-metric-267403 instances create datacloudies-cmd --zone=us-central1-a --machine-type=f1-micro --subnet=default --network-tier=PREMIUM --metadata=startup-script=\#\!\ /bin/bash$'\n'sudo\ su\ -$'\n'apt-get\ update$'\n'apt-get\ install\ -y\ apache$'\n'service\ http\ start\’ --maintenance-policy=MIGRATE --service-account=517159505824-compute@developer.gserviceaccount.com --scopes=https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/trace.append --tags=https-server --image=debian-9-stretch-v20200420 --image-project=debian-cloud --boot-disk-size=10GB --boot-disk-type=pd-standard --boot-disk-device-name=datacloudies --labels=type=demo,createdby=yoke --reservation-affinity=any

We can further set up the default values in the configuration file so that any new instances will use these values. For example if you set the default zone as us-central1, then all the new VM's will be chosen in the same zone. To set up the config values, perform the below command,

gcloud config set compute/zone us-central1-a

Check the VM status once it is created,

datacloudies google compute engine

Login into the datacloudies-cmd VM using below command,

gcloud beta compute ssh --zone "us-central1-a" "datacloudies-cmd" --project "spartan-metric-267403"

Play around with the instance and delete it once you complete it. 

datacloudies google compute engine

Similarly for POST Rest API commands can be generated with the above method and apply to the relevant API creation. Please do check out the other tutorials in the cloud branch



Recent Posts