Oh Crap… I have to use gcloud in the command line

November 24, 2023
Google Cloud

A No-Coder’s guide to using your Terminal & the gcloud CLI

Some Google Cloud Console operations are easier done (or only done) through the gcloud CLI (Command Line Interface). Unfortunately, for many of us in the low code / no code space, the command line is dangerous territory, and reserved for copy and paste from tutorials. This walkthrough takes you through the Command Line fundamentals for basic interactions with the gcloud CLI.

These are the things I find myself preferring the gcloud for over the Web Interface:

  • Deploying Serverless Cloud Functions
  • Managing Cloud SQL resources (updating ip whitelists)

FYI - Examples will be using MacOS and the Mac Terminal.

Command Line Basics

Styling your Terminal

Before doing anything practical you must first set a cool color theme for your terminal. An easy built-in theme is the Homebrew theme. Looks a little Matrix-like or green screen terminal from the 80’s

  1. Open Terminal by pressing Command+Space and typing Terminal. Then press Enter
  2. Mouse over the top menu and open Terminal > Settings
  1. Click on the Profile section
  2. Select Homebrew and press the Default action

Running a program

When you open a command line terminal, programs can be accessed by typing the program name. I use a lot of Node / npm (node package manager) for my serverless javascript work. 

Need to know Terms

  • Directory: This is the formal name for a “Folder” that can contain files in your file system
  • Command: This is a text statement that tells your machine to run a program by that name
  • Flag: Statements following a command that have a dash (-) in front are called flags. They specify a certain setting to pair with the command you called
  • Variable: When running a program, it may expect you to provide certain input values. These can be named variable_1=, or if only one input is expected, it may not be named (like how the directory name follows the cd command).  
  • CLI: Command line interface
  • SDK: Software Development Kit

Need to know Commands

  • ls : This lists all the contents of the current directory
  • cd: Change Directory. 
  • cd .. : Change Directory up one level
  • cd / : Change Directory to the Root directory
  • cd ~ : Change Directory to the Home directory for Current Mac User. 
  • rm : Remove a File
  • rmdir : Remove a Directory and all its contents
  • rm -r : Remove a Directory and recursively remove all its contents
  • curl : A program that runs web requests (API Calls) from the terminal
  • Control+c : This keyboard shortcut exits whatever is currently running in your Terminal session. This is useful for getting out of programs or canceling operations that are not doing what you want.

Install GCloud

To get gcloud on your machine, you’ll need to follow this slightly tedious setup document from Google.

Things required:

  1. Have Python installed. Python is a very common programming language that is required for many command line programs. Python or Python3 is necessary to interpret the gcloud scripts. 
  2. Know what type of hardware you’re running on your mac

Navigate to a directory in the Terminal.

cd ~

Download and extract a zip file. 

You can use the curl command to run web requests from the terminal. In the command below I call the curl command with a special flag (things with a dash in front are called flags and they are like specifying a certain setting or variable to pair with the command you called). I use the -O flag (case sensitive) to say I want to download the file in the current directory and leave the filename the same as specified in from the host of that file. 

curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-455.0.0-darwin-arm.tar.gz

After running the command, you should see some outputs (text on screen) appear describing the download progress. 

Unzip the file

You’ll use the tar command with the -xvf flag followed by the name of the file relative to the current directory. This will finish with the original .gz file still in the directory along with a new directory called google-cloud-sdk.

You can learn more about unzipping things via Terminal here.

tar -xvf google-cloud-cli-455.0.0-darwin-arm.tar.gz

After the unzip, you should see google-cloud-sdk appear when you run an ls on the home directory. 

Now you can rm (remove) the tar.gz file. Run the command below and then ls again. The tar.gz file should be tossed.

rm google-cloud-cli-455.0.0-darwin-arm.tar.gz

Run the Install command from the Terminal. 

Now with the file unpacked, you can install all the components. The folder has an install shell script that we will call to make things easy. It will ask a few questions during install such as: Do you want to update your Python version (i said no), Do you want to update your PATH (I said yes and accepted the suggested path).

Run the command below from the Home directory (or whatever directory you downloaded the sdk into). 

./google-cloud-sdk/install.sh

Once installed, you may need to close Terminal and reopen before you can run any gcloud commands. 

Deleting gcloud SDK from your system

If you foul up the install process for any reason, or simply want to get this program off your machine, just follow these official uninstall instructions from Google.

Getting gcloud going

What can gcloud do?

To get a sense for what you can do with gcloud, run the following command. Appending the --help flag to any gcloud command will display an explanation of what that command does and what flags/variables you can use to interact with it. 

Go ahead and run this to get a 30,000ft view of what you can use gcloud for. 

gcloud --help

Logging into your Google Cloud Platform account

Just like using GCP through the web console, the first thing you have to do is to Login with your Google Account. If you don’t have a Google Account, or have not logged into GCP (console.cloud.google.com) then go get an account setup. 

After install, the first things you need to do in order to actually use this tool is noggin with your Google OAuth credentials. To see if you have any credentials stored on your machine, run this command to list them out.

gcloud auth list

If no credentials are present, run this gcloud auth login to initiate a browser based OAuth login session. 

This automatically opens a browser session. However, this might not be ideal as it could conflict with certain browser profiles. If you navigate back to the Terminal session, you should see a long URL provided for you. Copy that and paste it in a browser where you’re already logged into that google account. 

Create a new GCP Project

To start creating/modifying resources we first need to set up a Project to start working in. In this tutorial, I’m going to create a new project from scratch, but you could follow along in an existing project. Use gcloud projects list to see all available projects to your account. Copy the Project_ID from one of them and use gcloud config set project PROJECT_ID to set it as the current working project. 

Create a new project by running:

gcloud projects create --name=test-proj --set-as-default

Accept the default project ID suggestion.

This should create the new project and set it as the current project you’re working on. Run this command to confirm:

gcloud config configurations list

You should see your current user listed and the Project ID you created as the current project associated to you.

With your User authenticated, and a Project selected, you can now fully manage your project and its resources from the command line. The CLI allows you to start automating various deployment or service tasks, and saving tons of time in your development efforts.  Down below, I have some good boilerplate scripts for common GCP tasks. 

Stefan Quartemont

Stefan Quartemont

Stefan Quartemont is a No Code & Low Code app development expert. Co-founder of QREW Technologies, AppSheetTraining.com, and NoCodeList.co, his leadership and problem-solving skills drive the enhancement of business operation technology, aiming to positively impact employees and customers through efficient and effective solutions.

Bring your Business into the Future
Throw the paper forms in the trash 🗑️ ... delete the spreadsheets! 🚫 It's time to gain massive technical leverage over your operation💪
Start Now