The Google Cloud Toolbox

The The Google Cloud Toolbox A simple, opinionated and minimalist documentation site for google cloud.

Info

The goal of this site is make easier use Google Cloud for anybody. Ths site is always under construction so use carefully.

Credits

This site uses Hugo and the Relearn theme. Thanks to all contributors of these projects.

Chapter 1

Tools & tricks

Useful information about command line tools, terraform and more.

gcloud CLI

gcloud CLI

The gcloud CLI lets you manage resources and services from the command line. It also contains service and data emulators to speed up local development. Here for install instructions

Note

You have already install the last version and your user logged in the Cloud Shell.

Useful commands

Getting started

gcloud init

Configuration

Set the default project,

gcloud config set project myproject001
Warning

Be careful with the default project, especially if is a production one. You can modificate the default this value using the --project in all the gcloud commands that afects a project. Example gcloud run deploy --project myproject001. Use always this flag if not sure of the default project or you are working with more than one project.

If you work with more than one profile you can create named configurations

gcloud config configurations create myconf
gcloud config configurations list
gcloud config configurations activate myconf

Terraform

Tip

You can get all the resources of a project in HCL (terraform lang) with this command gcloud beta resource-config bulk-export \ --project=PROJECT_ID \ --resource-format=terraform

Folders

Tip

ID of a folder gcloud resource-manager folders list --organization=1111111111 --filter="display_name: My Folder" --format="get(ID)" | sed 's/folders\///'

Credentials

For login with another google account. You will be redirected to the browser to login

gcloud auth login 

Get a token with your identity for authenticated apis calls (like to authenticated Cloud Run service)

gcloud auth print-access-token
Tip

Login as a service account is a good way to test the permision of a service account are the correct for some task before code the cicd

gcloud auth activate-service-account --key-file mykey.json

Global flags

Some flags are available throughout the gcloud CLI experience, like:

Warning

Use this when if using gcloud in cicd pipelines or similar

  • --quiet: Disabling interactive prompting (and applying default values for inputs).

  • --verbosity: Can set verbosity levels at debug, info, warning, error, critical, and none.
  • --format: Set output format as config, csv, default, diff, disable, flattened, get, json, list, multi, none, object, table, text, value, or yaml.

To load a service account credential for local dev:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"

firebase CLI

firebase command line tool

The firebase CLI lets you manage your firebase proyects and services from the command line. It also contains service and data emulators to speed up local development.

Tip

A lot of things in Firebase can managed in the website but its allways recommended use the cli and Git repo

    firebase init  
    firebase deploy
    firebase deploy --only hosting
    firebase deploy --only functions
    firebase deploy --only firestore:rules
    firebase emulators:start
    firebase emulators:exec "mocha ./test.js" //execute test of emulators
    firebase login:ci //get token for ci integration like gitlab cicd

gsutil

gsutil command line tool

The gcloud CLI lets you manage resources and services from the command line. It also contains service and data emulators to speed up local development. Here for install instructions

Tip

You have already install the last version and your user logged in the Cloud Shell.

The CLI has these command line tools:

  1. gcloud
  2. gsutil
  3. bq

To set the cache contol

‘‘‘gsutil -h “Content-Type:text/html”
-h “Cache-Control:public, max-age=3600” cp -r images
gs://bucket/images’’’

Set cors in a bucket

Create a file

[ { “origin”: ["*"], //poner los dominios autorizados “method”: [“GET”], “responseHeader”: [“Content-Type”], “maxAgeSeconds”: 3600 } ]

gsutil cors set cors gs://my-awesome-bucket

Terraform

Terraform in Google Cloud

Tip

You have already install the terraform client and your user logged in the Cloud Shell.

Tip

You can get all the resources of a project in HCL (terraform lang) with this command gcloud beta resource-config bulk-export --project=PROJECT_ID --resource-format=terraform

main.tf

terraform {
  required_providers {
    google = {
      source = "hashicorp/google"
      version = "3.5.0"
    }
  }
  backend "gcs" {
    bucket  = "tf-state-prod"
    prefix  = "terraform/state"
  }
}

provider "google" {
  credentials = file("<NAME>.json")

  project = "<PROJECT_ID>"
  region  = "us-central1"
  zone    = "us-central1-c"
}

resource "google_compute_network" "vpc_network" {
  name = "terraform-network"
}

GCS backend for status

terraform {
  backend "gcs" {
    bucket  = "tf-state-prod"
    prefix  = "terraform/state"
  }
}

Chapter 2

Services

Find out useful information and use advise about Google Cloud best services.

Compute Engine

Rerun the startup script

    sudo google_metadata_script_runner startup

View the result

    sudo journalctl -u google-startup-scripts.service

Cloud Run

Cloud Run

The The Google Cloud Toolbox A simple, opinionated and minimalist documentation site for google cloud.

Note

Cloud Run is a serveless for run containers, you can do over managed platform o GKE. IMO the best place for your workloads

Info
  • pay for use
  • min insances from 0
  • max instances
  • only internal traffic
  • eggress to VPC (all / internal)
  • great portability

Create

gcloud run deploy run-service --image gcr.io/...... --set-env-vars foo=lol --memory 1G --allow-unauthenticated --region europe-west1 --project myproject

delete

gcloud run services delete run-service --region europe-west1 --project=myProject --quiet

auth call to cloud run

curl -X GET -H "Authorization: Bearer $(gcloud auth print-identity-token)" https://hello-cloudunuri-uc.a.run.app
Tip

You can find example images here Google containers images repo

Tip

There is a very good list of resources in Awesome Cloud Run

Functions

Functions

Firebase

Schedulled function in europe

    exports.scheduledFunction = functions.region('europe-west1').pubsub.schedule('every 5 minutes').onRun((context) => {
        console.log('This will be run every 5 minutes!');
        return null;
    });

Registry

Registry

Upload an image to gcloud

    gcloud auth login
    gcloud auth configure-docker
    docker pull busybox
    docker tag busybox gcr.io/my-project/busybox
    docker push gcr.io/my-project/busybox

Firestore

Firestore

Note

Firestore can use directly from GCP but there is extra features if you use in Firebase

Rules

    rules_version = '2';
    service cloud.firestore {
        match /databases/{database}/documents {
            match /Users/{document} {
            allow create: 
            if request.auth.uid == document
                && request.resource.data.keys().hasAll(["name", "nick", "created_at"])
                && request.resource.data.keys().hasOnly(["name", "nick", "created_at"]);
            allow read: if request.auth.uid == document;
            allow update: 
            if request.auth.uid == document
                && (request.resource.data.diff(resource.data).affectedKeys()
                .hasOnly(["name", "nick"]));
            allow delete: if request.auth.uid == document; 
            }
        }
    }

Rules example for subscriptions

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    function hasBasicSubs() {
      return request.auth.token.stripeRole == "basic";
    }

    function hasPremiumSubs() {
      return request.auth.token.stripeRole == "premium";
    }

    match /content-basic/{doc} {
      allow read: if hasBasicSubs() || hasPremiumSubs(); 
    }
    match /content-premium/{doc} {
      allow read: if hasPremiumSubs(); 
    }

    match /customers/{uid} {
      allow read: if request.auth.uid == uid;

      match /checkout_sessions/{id} {
        allow read, write: if request.auth.uid == uid;
      }
      match /subscriptions/{id} {
        allow read: if request.auth.uid == uid;
      }
    }

    match /products/{id} {
      allow read: if true;
      allow write: if false;

      match /prices/{id} {
        allow read: if true;
        allow write: if false;
      }
    }
  }
}
Tip

Is allways a good idea manage the rules from code and store in a git repo and deploy with firebase cli

firebase init
firebase deploy