Provisioning VPS instance on Google Cloud with gcloud CLI
This brief guide teaches you how to provision gcloud
compute resources using the gcloud
CLI. You can always read more about the CLI on Google Cloud CLI documentation . I am using the documentation as a reference as well.
Authentication
To use gcloud
CLI you need to be authenticated.
gcloud auth login
This will launch a browser window asking you to sign in to Google Cloud SDK
using your Google account.
Make sure you are familiar with the permissions you are granting the gcloud
CLI
Set the Project
Once you are logged in you will be asked to provide a project
id. This scopes the gcloud
CLI. To list all the projects linked to your account run the following and then set the gcloud
CLI project.
gcloud projects list # To list all of your projects. Copy desired project ID and
# Set the project scope for the CLI
gcloud config set project PROJECT_ID
Create an Instance
When creating a Compute instance we want to be able to set the following specifications. You can always view the man
pages for the CLI or the gcloud compute instaces create INSTNACE_NAME --help
which actually opens the man
pages for that particular command. Alternatively, you can also view the gcloud
Instance Createreference
The following shows the specification/parameter name, the associated gcloud
CLI flags and a brief description of the flag
.
- Zone
--zone=[ZONE]
- The
zone
your instance will be hosted in. Make sure the instance type is supported, as only certain instance types are available in certain regions. To check the available zones refer to gcloud sdk region zones reference - eg. London is in
--zone=europe-west2-a
could bea,b, c
- The
- Instance Type
--machine-type=INSTANCE_TYPE
- Specifies the machine type used for the instances. To get a list of available machine types, run ‘gcloud compute machine-types list’.
- Linux Distro
--image=IMAGE_NAME
or--image-family=IMAGE_FAMILY
- To list the available images run
gcloud compute images list
andgrep
the desired Linux distribution. I normally go withDebian
. If you want the latest version of a family such as Debian, you can useimage-family='debian-12'
, this will use the latest version of the image. Otherwise you have to list the full name of the image. eg.image=debian-12-bookworm-v20241009
- To list the available images run
- Disk Size
--boot-disk-size=SIZE_IN_GB
- The disk size in GB. By default it creates disk of type
pd-standard
or Persistent Disk Standard, which is fine for normal use.
- The disk size in GB. By default it creates disk of type
- Hostname
--hostname=HOSTNAME
- Sets the hostname of the instance created. Gcloud requires to input two labels sperated by a ‘dot’. eg:
--hostname=personal.server
- Sets the hostname of the instance created. Gcloud requires to input two labels sperated by a ‘dot’. eg:
- Startup Script and Other Metadata
--metadata=[KEY=VALUE]
or--metadata-from-file
- You can use this to setup a startup script that runs on instance creation. For larger scripts its recommended to use
--metadata-from-file startup-script=./PATH_TO_FILE
- You can use this to setup a startup script that runs on instance creation. For larger scripts its recommended to use
This command creates e2-small
instance running debian-12
with 20GB
disk size.
gcloud compute instances create personal \
--zone europe-west2-a \
--machine-type e2-small \
--boot-disk-size 20GB \
--image-family debian-12 \
--image-project debian-cloud \
--hostname personal.server