# How to initiate the GCP project and use gcloud to access

# Introduction

GCP(Google Cloud Platform) is a great platform, it provides many free Tier products for us to try. Let's create a project for learning or development.

# Create New Project

We can use GCP free of charge(GCP Free Trial (opens new window)), so I initiated for myself. If your GCP account is ready, logon the GCP console and create a new project:

# Create Service Account

We can manage all the resource on GCP console, but it's best to use a service account and manage the GCP resource with SDK.

Let's create a Service Account:

Input the name:

Select the roles, I selected the owner to grant all right to the account:

# Create Key of Service Account

We use the key for the credentials instead of username/password, so we need to create the key:

Create a new key with JSON format. It will download the json file.

# Setup gcloud SDK

Set the environment variable GOOGLE_APPLICATION_CREDENTIALS:

$ export GOOGLE_APPLICATION_CREDENTIALS=/Users/larry/Software/google-cloud-sdk/pkslow-admin-for-all.json

Active the Service Account:

$ gcloud auth activate-service-account admin-for-all@pkslow.iam.gserviceaccount.com --key-file=${GOOGLE_APPLICATION_CREDENTIALS}

Set the project:

$ gcloud config set project pkslow

Check the configuration:

$ gcloud auth list
               Credentialed Accounts
ACTIVE  ACCOUNT
*       admin-for-all@pkslow.iam.gserviceaccount.com

To set the active account, run:
    $ gcloud config set account `ACCOUNT`


$ gcloud config list
[core]
account = admin-for-all@pkslow.iam.gserviceaccount.com
disable_usage_reporting = True
project = pkslow

Your active configuration is: [default]

# Use the gcloud

We can use the gcloud after the SDK is ready.

Create the MQ(Google Pus/Sub) Topic and Subscription:

$ gcloud pubsub topics create pkslow-test
Created topic [projects/pkslow/topics/pkslow-test].

$ gcloud pubsub subscriptions create pkslow-sub --topic=pkslow-test
Created subscription [projects/pkslow/subscriptions/pkslow-sub].

Check the Pub/Sub with SDK:

$ gcloud pubsub topics list
---
name: projects/pkslow/topics/pkslow-test


$ gcloud pubsub subscriptions list
---
ackDeadlineSeconds: 10
expirationPolicy:
  ttl: 2678400s
messageRetentionDuration: 604800s
name: projects/pkslow/subscriptions/pkslow-sub
pushConfig: {}
topic: projects/pkslow/topics/pkslow-test

Check the Pub/Sub on GCP console:


Reference:

Mac安装Google Cloud SDK (opens new window)

GCP常用命令(gcloud、gsutil) (opens new window)

Generate Google Cloud API Credentials (opens new window)

Last Updated: 8/18/2023, 11:39:36 PM