Skip to content

Instantly share code, notes, and snippets.

@nathanborror
Last active May 31, 2022 15:50
Show Gist options
  • Save nathanborror/a0cead27dfbf346ca88b3ce47d9404dd to your computer and use it in GitHub Desktop.
Save nathanborror/a0cead27dfbf346ca88b3ce47d9404dd to your computer and use it in GitHub Desktop.
How to setup a new Go project using Google Cloud Run and Cloud SQL.

Go Google Cloud

How to setup a new Go project using Google Cloud Run and Cloud SQL.

Setup a new project

Update gcloud and authenticate

$ gcloud components update
$ gcloud auth login

Create project

$ gcloud projects create <PROJECT_NAME>
$ gcloud config set project <PROJECT_NAME>
$ gcloud config set run/region <REGION>

Deploy local code to Google Cloud Run

$ mkdir <PROJECT_NAME>
$ cd <PROJECT_NAME>
$ gcloud run deploy <PROJECT_NAME> --source . --region <REGION>

I'm deploying from a Go project directory. In order for this to work you need to have a main.go in the root of the project and all dependencies need to either be in the directory or publicly accessable on Github. This guide may help.

Configure DNS

$ gcloud dns managed-zones create <PROJECT_NAME>-dns --dns-name <DOMAIN> --description "Example DNS"
$ gcloud dns managed-zones describe <PROJECT_NAME>-dns

Add the printed out nameservers to wherever the domain is being hosted.

Verify domain

$ gcloud domains verify <DOMAIN>

This will open a web browser and provide a code to be entered into the TXT record below.

Add Google verification

$ gcloud beta dns record-sets transaction start --zone <PROJECT_NAME>-dns
$ gcloud beta dns record-sets transaction add <GOOGLE_VERIFICATION_CODE> --name "<DOMAIN>." --ttl 300 --type TXT --zone <PROJECT_NAME>-dns
$ gcloud beta dns record-sets transaction execute --zone <PROJECT_NAME>-dns

Map Cloud Run instance to domain

$ gcloud beta run domain-mappings create --service <PROJECT_NAME> --domain <DOMAIN>

This will print out IP addresses to be entered as DNS A records below.

Add DNS records

$ gcloud beta dns record-sets transaction start --zone <PROJECT_NAME>-dns
$ gcloud beta dns record-sets transaction add <IPv4> <IPv4> <IPv4> --name "<DOMAIN>." --ttl 300 --type A --zone <PROJECT_NAME>-dns
$ gcloud beta dns record-sets transaction add <IPv6> <IPv6> <IPv6> --name "<DOMAIN>." --ttl 300 --type AAAA --zone <PROJECT_NAME>-dns
$ gcloud beta dns record-sets transaction execute --zone <PROJECT_NAME>-dns

Setup Cloud SQL

Enable Cloud SQL API

$ gcloud services enable sqladmin.googleapis.com

Create PostgreSQL instance

$ gcloud sql instances create <PROJECT_NAME>-postgres --database-version POSTGRES_14 --tier db-f1-micro --region <REGION>
$ gcloud sql users set-password postgres --instance <PROJECT_NAME>-postgres --password <PASSWORD>
$ gcloud sql databases create <DATABASE_NAME> --instance <PROJECT_NAME>-postgres

Add Cloud SQL instance to Cloud Run

$ cd <PROJECT_NAME>
$ gcloud run services update <PROJECT_NAME> --add-cloudsql-instances <PROJECT_NAME>-postgres

Create secrets

$ gcloud secrets create <PROJECT_NAME>-postgres-user --replication-policy automatic
$ gcloud secrets create <PROJECT_NAME>-postgres-password --replication-policy automatic
$ gcloud secrets create <PROJECT_NAME>-postgres-db --replication-policy automatic

$ echo -n "postgres" | gcloud secrets versions add <PROJECT_NAME>-postgres-user --data-file -
$ echo -n "<PASSWORD>" | gcloud secrets versions add <PROJECT_NAME>-postgres-password --data-file -
$ echo -n "<DATABASE_NAME>" | gcloud secrets versions add <PROJECT_NAME>-postgres-db --data-file -

Update Cloud Run environment variables

$ gcloud run services update <PROJECT_NAME> \ 
    --update-env-vars INSTANCE_CONNECTION_NAME=<PROJECT_NAME>:<REGION>:<PROJECT_NAME>-postgres \
    --update-secrets DB_USER=<PROJECT_NAME>-postgres-user:latest \
    --update-secrets DB_PASS=<PROJECT_NAME>-postgres-password:latest \
    --update-secrets DB_NAME=<PROJECT_NAME>-postgres-db:latest

Install Cloud SQL Proxy

Follow tutorial: https://cloud.google.com/sql/docs/postgres/connect-admin-proxy#macos-64-bit

Connect

$ cloud_sql_proxy -instances=<PROJECT_NAME>:<REGION>:<PROJECT_NAME>-postgres=tcp:5432 -dir=/tmp
$ psql "host=127.0.0.1 port=5432 dbname=<DATABASE_NAME> user=postgres password=<PASSWORD> sslmode=disable"

Maintanence

Push new revision

$ cd <PROJECT_NAME>
$ gcloud run deploy <PROJECT_NAME> --source .

Update instance secret

$ gcloud run services update <PROJECT_NAME> --update-secrets <ENV_VAR_NAME>=<SECRET_NAME>:latest

Remove secret from instance

$ gcloud run services update <PROJECT_NAME> --remove-secrets <ENV_VAR_NAME>

Delete all this work

$ gcloud projects delete <PROJECT_NAME>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment