Skip to content

Intermediate · Google Cloud and comparing the clouds · topic 30 of 50 · 30 min

The command line on all three clouds

Explanation

Every cloud has a command-line tool: the AWS CLI (aws), the Azure CLI (az) and the Google Cloud CLI (gcloud). They let you script anything you can click, choose an account and region per command, and print JSON you can filter or feed into scripts. Keep credentials out of code and Git, prefer single sign-on and short-lived credentials, and turn on secret scanning.

Why use the command line?

The web console is good for exploring, but slow for repeated work. A command-line interface (CLI) lets you type one line instead of twenty clicks, put commands in a script, run them again tomorrow exactly the same way, and paste them into a runbook (a step-by-step guide for operations). All three clouds have an official CLI, and all three can print JSON, so scripts can read the results.

AWS CLI version 2

The AWS CLI is the aws command. Its configuration lives in two files in your home folder: ~/.aws/config (regions, output format, SSO settings) and ~/.aws/credentials (access keys, if you use them). A profile is a named set of settings, so you can switch between a dev account and a prod account with --profile.

bash
aws configure sso --profile dev          # set up single sign-on (recommended)
aws sso login --profile dev              # get short-lived credentials in the browser
aws sts get-caller-identity --profile dev                # who am I?
aws ec2 describe-instances --profile dev --region ap-south-1 --output table
aws ec2 describe-instances --region ap-south-1 \
  --query "Reservations[].Instances[].[InstanceId,State.Name]" --output text
aws configure (without sso) asks for an access key, a secret key, a default region and an output format.

--region overrides the default region for one command. --output chooses json (the default), table (easy to read) or text (easy for shell scripts). --query filters the result with JMESPath, a small query language for JSON: Reservations[].Instances[].InstanceId means "from every reservation, from every instance, take InstanceId".

Azure CLI

The Azure CLI is the az command. az login opens a browser to sign in. One login can see several subscriptions (billing and access boundaries in Azure), so choose the right one before you change anything.

bash
az login                                            # sign in through the browser
az account list --output table                      # subscriptions you can use
az account set --subscription "Payroll-Prod"        # make one the default
az vm list --resource-group payroll-rg --output table
az group list --query "[?location=='centralindia'].name" --output tsv
Azure CLI also supports --output json, table, tsv and yaml, and --query with JMESPath.

Google Cloud CLI

gcloud is the main Google Cloud command (bq for BigQuery and gsutil for older storage scripts also exist). A configuration is a named set of settings: account, project and region. You can keep one per client or environment and switch with one command.

bash
gcloud auth login                                   # sign in your user account
gcloud config configurations create client-a        # a new named configuration
gcloud config set project client-a-prod
gcloud config set compute/region asia-south1
gcloud config configurations list                   # all configurations; one is active
gcloud config configurations activate default       # switch back
gcloud compute instances list --filter="status=RUNNING" --format=json
gcloud uses --format (json, yaml, table, value(...)) and --filter instead of --output and --query.

Cloud shells in the browser

AWS CloudShell, Azure Cloud Shell and Google Cloud Shell give you a terminal in the browser with the CLI already installed and already signed in as you. They are perfect for quick tasks, for learning, and for computers where you cannot install software, such as a college lab.

Keeping credentials safe

A credential is anything that proves who you are: a password, an access key, a token or a key file. If one leaks, an attacker can run up a huge bill or steal data. Bots scan public GitHub repositories for cloud keys within minutes of a push.

  • Never put keys in code, config files you commit, container images or chat messages.
  • Prefer single sign-on and short-lived credentials: aws sso login, az login and gcloud auth login all give tokens that expire.
  • On cloud servers, use roles and attached identities (IAM roles on EC2, managed identities on Azure, attached service accounts on Google Cloud) instead of keys.
  • Add secret files to .gitignore, for example .env and *.json key files.
  • Turn on secret scanning, for example GitHub secret scanning with push protection, or a tool such as gitleaks in your CI.
output
# .gitignore
.env
.env.*
*-key.json
.aws/
Keep secret files out of Git from the first commit.
Careful: If a key leaks, deleting the commit is not enough: the key is already copied. First deactivate or delete the key in the cloud console, then create a new one if you still need it, check the audit logs for misuse, and only then clean the Git history.

Scripting with JSON output

Because every CLI can print JSON, you can feed the output into Python or JavaScript: find all running VMs in Mumbai, list buckets without encryption, or count instances per team tag. Use --query or --filter for simple cases, and a small script when the logic grows.

Common mistakes and how to fix them

  • Running a command in the wrong account or subscription. Fix: run aws sts get-caller-identity, az account show or gcloud config list first.
  • Forgetting --region and creating resources far away. Fix: set a default region in your profile or configuration.
  • Parsing table output in scripts. Fix: tables are for people; use json or text output for scripts.
  • Sharing one long-lived admin key in a team. Fix: give each person their own SSO login with least-privilege roles.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

The command line on all three clouds · 1/7

aws az gcloud

Each cloud has a command line tool: aws for AWS, az for Azure and g cloud for Google Cloud.

Prefer a video by a teacher? Find free videos on “aws cli azure cli gcloud command line tutorial beginners” on YouTube ↗

In real life: A managed services company in Noida checks three clients

A managed services company in Noida looks after three clients: one on AWS, one on Azure and one on Google Cloud. Every morning an engineer runs a small script. It calls aws ec2 describe-instances with the client's SSO profile, az vm list -d for the Azure client, and gcloud compute instances list for the Google client, all with JSON output. A Python function reads the JSON and lists VMs that are running outside the agreed regions, so nobody forgets a test server in a US region. Nobody stores access keys: each CLI uses SSO tokens that expire after a few hours.

bash
gcloud compute instances list --format=json > gcp-vms.json
python3 check_regions.py gcp-vms.json asia-south1
Save JSON output, then let a script decide.

Practice (0/3 done)

Task 1 · Question

Which command shows which AWS identity your CLI is currently using?

Task 2 · Code · Python

json_text is the JSON output of a VM list: a list of objects with "name", "zone" and "status". The zone may be a short name like "asia-south1-a" or a full URL ending in "/zones/asia-south1-a" (gcloud prints the URL). Write running_in_region(json_text, region) that returns the names of VMs whose status is "RUNNING" and whose zone is in the given region, in their original order. The region of a zone is the zone without its last "-letter" part, so asia-south1-a is in asia-south1.

Task 3 · Code · Python

Before you paste a config file into a chat or a ticket, mask the secrets. Write mask_secrets(text): for every line that contains "=", take the part before the first "=" as the name. If the name, in lower case, contains "secret", "password", "token" or "key", replace the line with the stripped name, then " = ****". Leave every other line exactly as it is, and keep the lines in order (join them with a newline).

Quick quiz

Question 1

1.Which AWS CLI option filters the output on your machine using JMESPath?

Question 2

2.What does az account set --subscription "Payroll-Prod" do?

Question 3

3.Why use gcloud config configurations?

Question 4

4.You pushed an access key to a public GitHub repository. What should you do FIRST?

Question 5

5.Which output format should a shell script usually read?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

Which AWS CLI option filters the output on your machine using JMESPath?

Answer: --query

--query takes a JMESPath expression and filters the JSON before it is printed.

What does az account set --subscription "Payroll-Prod" do?

Answer: Makes Payroll-Prod the default subscription for later az commands

It only changes which subscription the Azure CLI uses by default.

Why use gcloud config configurations?

Answer: To keep separate named sets of account, project and region, and switch between them

Configurations let you switch between clients or environments without retyping settings.

You pushed an access key to a public GitHub repository. What should you do FIRST?

Answer: Deactivate or delete the key in the cloud console, then check logs and clean up

The key may already be copied, so revoke it at once. Cleaning Git history comes after.

Which output format should a shell script usually read?

Answer: json or text

Tables are formatted for people. JSON or text output is stable and easy for scripts to parse.

Which tool lets you keep an encrypted Kubernetes Secret in Git that only the cluster can decrypt?Related

Answer: Sealed Secrets

Sealed Secrets encrypts with the controller's public key; only the controller in the cluster has the private key.

From Secrets management

What is the closest Google Cloud equivalent of AWS Lambda?Related

Answer: Cloud Run functions

Cloud Run functions (formerly Cloud Functions) run single functions on events, like Lambda.

From AWS vs Azure vs Google Cloud: the service map

Why are roles with temporary credentials better than access keys for apps?Related

Answer: Credentials expire automatically, so a leak is useful for a short time only

Short-lived credentials shrink the window of misuse.

From Cloud identity and access management

Which command shows the account and identity your CLI is using?Related

Answer: aws sts get-caller-identity

aws sts get-caller-identity returns the UserId, Account and Arn.

From Creating and securing an AWS account

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. This is the last topic of “Google Cloud and comparing the clouds”. The module exam comes next.
  2. Module 7 exam: Google Cloud and comparing the clouds · 12 questions · 20 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Containers in the cloud. 50 topics left in the course before the final exam.

← Previous: AWS vs Azure vs Google Cloud: the service map