Projects

Projects are the biggest units of organization inside GCP. They allow you to configure access control to your engineers.

This is a collection of a few useful commands.

Projects and Compute Instances

List all the Compute Instances (virtual machines) in the currently configured project and
return the results as JSON.

gcloud compute instances list --format='json'

List all the Compute Instances in the project supplied on the command line. For this you don't need to configure each project, you just need to be logged in and you need to have access rights to the specific project.

gcloud compute instances list --project PROJECT --format=json

Stop instance

It will stop the instance (move to TERMINATED state) but won't delete it.

In this example we would like to know what values does the label "role" have in the project called "development-42". First we list the values from all the instances in CSV format. This will contain the list but it will also contain the name of the field ("role" in our case) as the first line of the result. So we filter it out using the tail command. Then we sort the values and run through uniq. That gives us each name once.

List all the local configurations

Instead of supplying all the values on the command line for every command (e.g. name of the project, zone, etc.) we can have local configurations with with defaults. Each configuration has a name and a bunch of key-value pairs.

The command

gcloud init

can be used to create new configurations or to change existing ones.

BTW the configuration files are stored in ~/.config/gcloud/configurations/

Comments

Author: Gabor Szabo

Gabor who runs the Code Maven site helps companies set up test automation, CI/CD
Continuous Integration and Continuous Deployment and other DevOps related
systems. Gabor can help your team improve the development speed and reduce the risk of bugs.