Skip to content

Instantly share code, notes, and snippets.

@angstyloop
Last active November 8, 2022 09:25
Show Gist options
  • Save angstyloop/3a174a3c7508298dfd93454ecdb6052c to your computer and use it in GitHub Desktop.
Save angstyloop/3a174a3c7508298dfd93454ecdb6052c to your computer and use it in GitHub Desktop.
getting started with gcloud / create gcloud vm with ssh access / ssh into gcloud vm / delete gcloud vm
################################################################################
# Getting started with GCloud.
# You'll need a google developer account, so that you have access to the GCloud
# console. You'll need to create a GCloud project and attach a Billing Method
# to it. After that you'll need to install the gcloud API.
# On Ubuntu, you can do that really quickly with Snap.
sudo snap install google-cloud-cli --classic
# ... and then add the tools to your PATH and enable commmand completion by
# sourcing the "gcloud/completion.bash.inc" file (e.g., in your bash profile).
# You can find the file path like this:
sudo find / -name completion.bash.inc 2>/dev/null
# And you can source the file like this
. /path/to/gcloud/completion.bash.inc
# Now you can start the gcloud CLI, which is written in Python 3.4+ .
gcloud init
# Follow the prompts until you have what you need to run `gcloud projects list`
# successfully.
# List projects (the command you just ran to check your gcloud CLI is set up)
gcloud projects list
# Read off your gcloud PROJECT_ID.
# (optional) List networks
# gcloud compute networks list --project=$PROJECT_ID
# We will use the "default" network, so this step is optional.
# (optional) List subnets
# gcloud compute networks subnets list --network=default --project=$PROJECT_ID
# Just to see what subnet each region gets, which might matter to you.
# List regions
gcloud compute regions list
# Pick a gcloud REGION.
# List zones
gcloud compute zones list --filter="region=$REGION"
# Pick a gcloud ZONE.
# List machine types in zone.
gcloud compute machine-types list --zones="$ZONE"
# Pick a MACHINE_TYPE, and save for later. You may have to research them.
# List image project for os type, e.g., for ubuntu. You can also leave off the
# --filter option and use grep instead
gcloud compute images list --filter="family=ubuntu"
# Read off the IMAGE_PROJECT and IMAGE_FAMILY.
# Know the BOOT_DISK_TYPE you want. Solid-state drives are more expensive to run
# on. If you don't specify the boot disk type AND you're using the gcloud CLI
# (and not the cloud shell), it will use "pd-standard" by default.
# To make it interesting, we add a public SSH key. GCloud will automatically put
# the key into a file in the authorized directory, as well as in the instance
# metadata. The $USERNAME specified by the --metadata option will be
# created with root level access for SSH sessions, and should match the USERNAME
# used to create the key in the command below.
# Create SSH key. You pick the PUBLIC_KEY_PATH.
ssh-keygen -t ed25519 -f $PUBLIC_KEY_PATH -C $USERNAME -b 2048
# Create VM
gcloud compute instances create $INSTANCE_NAME
--metadata="ssh-keys=$REMOTE_USER:$(cat $PUBLIC_KEY_PATH)" \
--zone=$ZONE \
--machine-type=$MACHINE_TYPE \
--image-project=$IMAGE_PROJECT \
--image-family=$IMAGE_FAMILY \
--boot-disk-type=$BOOT_DISK_TYPE
# E.g.
#
# gcloud compute instances create my-instance \
# --metadata="ssh-keys=$USERNAME:$(cat ~/.ssh/id_ed25519.pub)" \
# --zone=us-west1-b \
# --machine-type=t2d-standard-8 \
# --image-project=ubuntu-os-cloud \
# --image-family=ubuntu-2204-lts \
# --boot-disk-type=pd-standard
# You can also add the --boot-disk-size flag. If not specified, the default size
# for the image family and image project are used
# Read off the VM's PUBLIC_IP address. You can SSH to that with your secure
# ed25519 2048-bit encrypted connection.
# (Optional) List all VMs
#gcloud compute instances list
# (Optional) Describe a specific VM
#gcloud compute instances describe $INSTANCE_NAME --zone=$ZONE
# Connect to your Linux VM in the Google Cloud
ssh $USERNAME@$PUBLIC_IP
# Do stuff in your Ubuntu VM! Remember, you pay for IO and compute time. So if
# you just dont' DO anything, you're paying basically nothing.
# Anyways let's walk out and blow it up.
<VM SHELL> exit
# Delete VM
gcloud compute instances delete [ $INSTANCE_ID | $INSTANCE_NAME ] --zone=$ZONE
# E.g.
#
# gcloud compute instances delete my-instance --zone=us-west1-b
# All done!
################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment