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"
  }
}