Google Cloud Platform for Architects
上QQ阅读APP看书,第一时间看更新

Creating a VM instance using the command line

An easy way to get the right command line is to piggyback on the web console—it has a neat little way to get the command line corresponding to a particular UI operation:

  1. As before, start with the hamburger (three horizontal lines).
  2. Navigate to Compute, and then to VM instances.
  3. Click on Create a VM instance in the VM instances page and you'll get the UI to set up the configuration for this virtual machine.
  4. Specify the configuration parameters using this console, as we saw in the previous section. At the very bottom of this form, you can find the Equivalent REST or command line option.
  5. Click on both of these to get the Equivalent REST and gcloud commands respectively to set up the VM with the exact settings specified. The gcloud command is what you can use within scripts in order to create multiple VM instances with the same configuration.
  1. Click on the icon with ≥ sign on the top-right side beside search bar. Open up your Cloud Shell from the console, which is completely free to use, and paste the gcloud command and hit Enter to create a new VM instance. Refresh the browser to see the new VM listed in the VM instances page.

Running help on this gcloud compute instance create command should show you all the options that you have available and you can use these to create custom instances from the Cloud Shell. But if you want to customize your instance with a lot of parameters, it's better to specify them on the web console, generate the corresponding command-line command, and use that:

gcloud compute instances create --help  

You can further set up some default values in your configuration file so that any new instances that you create will use this default value. For example, if you set the default zone to be us-central1-a, all new instances will be created in this zone:

gcloud config set compute/zone us-central1-a 

Note that if you wish to SSH into an instance that is not in the default zone from the Cloud Shell, you'll need to explicitly specify the zone using the zone parameter in the SSH command.

Now we know that if we wanted to automate the creation of virtual machines with specialized configurations, we can simply write a script with the required gcloud command, which is easily obtained by setting up the configuration on our web console and clicking on the Equivalent REST or command line link at the very bottom.