Skip to content

Instantly share code, notes, and snippets.

@himat
Created April 20, 2020 02:29
Show Gist options
  • Save himat/0c67a7c23f069c3da0593850484ca150 to your computer and use it in GitHub Desktop.
Save himat/0c67a7c23f069c3da0593850484ca150 to your computer and use it in GitHub Desktop.
[deploy gcloud containers] Create containers that restart automatically (for Hasura)

This sets up a Postgres DB and Hasura container as a long-running instance that restarts automatically on failure

  • Spin up your SQL DB: gcloud sql instances create "$db_instance" --database-version=POSTGRES_11 --zone="${project_zone}" --tier=db-g1-small --storage-type=SSD --maintenance-window-day=sunday --maintenance-window-hour=00
  • Then set up your Hasura instance: gcloud compute instances create-with-container "$hasura_instance" --container-image="hasura/graphql-engine:latest" --machine-type=e2-micro --container-env-file="./hasura.env" --container-env HASURA_GRAPHQL_ADMIN_SECRET="$hasura_admin_secret",HASURA_GRAPHQL_ACCESS_KEY="$hasura_admin_secret",HASURA_GRAPHQL_DATABASE_URL="postgres://hasurauser:${hasura_db_pw}@${sql_ip}:5432/postgres" --zone="${project_zone}" --container-restart-policy=always --tags="http-server" --address="${hasura_fixed_ext_ip}"
  • You will additionally need to patch the SQL DB to allow the console to change firewall rules with: yes | gcloud sql instances patch "$db_instance" --authorized-networks="$shell_ip"
  • And then you need to allow Hasura to connect to the SQL DB: gcloud sql users set-password hasurauser --instance="$db_instance" --password="$db_pw"

Development workflow:

  1. Local development with Docker-Compose (has some test containers)
  2. Test deployment on Google Cloud (runs just long enough to execute the same test containers)
  3. Prod deployment on Google Cloud

Steps 2 and 3 run with the above scripts, whereas step 1 is docker-compose
Step 2 looks onerous, but there are differences between Google Cloud SQL and local Postgres that have broken Hasura before
However the upsides of having a managed DB greatly outweigh the downsides overall, imho

A g1-small DB in Google Cloud, which is very badly advertised, doesn't go above 5% usage even with about 20 semi-complicated transactions per second
it's aobut $1 per day IIRC
check out the E2 instances for compute also. Very cheap

  • This is all from a chat with a helpful person online
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment