faceless woman sitting on couch and working online on laptop

Google Cloud Platform using GCloud CLI to create projects and billing

0

Well in the world of Infrastructure as Code, the idea is that you try not to use the graphical interface and instead use the command line. Put it in scripts and other tools. So here are the basics for doing this with Google Cloud Platform. The nice thing is that command completion works really well, if you need a list of active projects, it will appear. So decently easy.

First, you need to install the gcloud pieces (on a Mac, this is brew install gcloud) and there a zillion cheat sheets for quick commands.

As an example, to create a bunch of project, here is what you do. First, you need to initialize it and then logon and then scripting is pretty easy for instance to list all resources in a project

# To initialize is all
brew install gcloud
gcloud init
# now logon, note if you have multiple accounts, you have to login different things then switch configuration.
gcloud auth login
# to switch to a new configuration
gcloud config list && gcloud config configuration activate _new_config
# Now to see the projects and things
gcloud projects list
# Or to just look at project Ids
gcloud projects list --format="value(projectId)"
# set the default project
gcloud config set project _a project id_

All of these let’s you create a new project with a single long command line where the important things are a unique project id (it has to be unique across GCP I think like AWS has the same requirement):

# if you want to see actual field names
gcloud organizations list --format=json
# now set an environment so you have that organization id in name
GCP_ORG=$(gcloud organizations list --format="value(name)"
gcloud project create _new_project_id_ 
# Find billing accounts
gcloud beta billing accounts list
# Use json to figure out field names
gcloud beta billing accounts list --format=json
# now assuming you want the second line
GCP_BILL=$(glcoud beta billing accounts list --format="value(name)" | sed -n 2p)
# Assuming you want the second
# Now you are ready to create a project
gcloud projects create richtong-1 --organization=$GCP_ORG \
    --labels="cost_center=dev,env=dev"
# now link the project to the right billing
gcloud beta billing projects link richtong-1 --billing-account=$GCP_BILL

Related Posts

This site uses Akismet to reduce spam. Learn how your comment data is processed.