Resources in Google Cloud are zonal, regional, or multiregional. That is, they apply to a single zone (location), multiple zones within a region, or distributed across multiple regions.
It is useful to specify defaults for your region and zone so that you do not have to specify them every time you create a resource. For example, if you are working in London (like me), you may want to specify the default region as europe-west2 and the default zone as europe-west2-a.
These commands set the default region and zone:
gcloud
config
set
compute/region
europe-west2
gcloud
config
set
compute/zone
europe-west2-a
gcloud
config
set
run/region
europe-west2
These settings are stored on your local machine; for example, on a Mac, in ~/.config/gcloud/configurations/config_default, they are set for your machine rather than for an account.
Tip
If you encounter a message like “API [compute.googleapis.com] not enabled on project [your-project-id],” it means that the specific service—in this case, the Google Compute Engine service—is not enabled for your project by default.
Not all services are enabled for new projects to avoid unnecessary usage and cost. When you create a new project, only a subset of services that are commonly used or required by other core services will be enabled automatically. This approach helps keep the project environment clean and reduces potential security risks.
Alternatively, you can use the gcloud init command to be guided through the process of setting up the gcloud CLI.
Create a Project
All Google Cloud Platform resources live in a project. A project is a container for resources that allows you to manage access and billing. Each project has a unique ID and a name. The name is unique to your account, but the ID is unique across all Google Cloud Platform accounts.
Tip
You will create several projects in this book. Rather than explaining how to set up a project each time, you will be referred back to this section.
I find it useful to store the PROJECT_ID in an environment variable so that I can easily reference it later on. As you move on to the projects, you will make a lot of use of this pattern:
export
PROJECT_ID
=[
PROJECT_ID
]
To create the new project, enter the following gcloud command:
gcloud
projects
create
$PROJECT_ID
Then set the gcloud CLI to use the new project as the default:
gcloud
config
set
project
$PROJECT_ID
Similarly, if you have an environment variable named $REGION you could set the default region and zone like this:
gcloud
config
set
compute/region
$REGION
gcloud
config
set
compute/zone
$REGION
-a
gcloud
config
set
run/region
$REGION
At any time, you can check your current project using:
gcloud
config
list
project
If you ever want to set your current project to an environment variable again you use the output of the gcloud command like this:
export
PROJECT_ID
=
$(
gcloud
config
get
project
)