loading...

Setup Hasura at Google Cloud Run

davidoliveira profile image David Oliveira Updated on ・4 min read

Introduction

This walkthrough is about how we can setup an Hasura instance on Google Cloud Run, using your database hosted at Google Cloud through the Cloud SQL Proxy without having to build your own container with the Hasura and Cloud SQL Proxy embedded. I will also show to you, step by step how you can do it from the Google Cloud Platform interface. Yeah, I'm not a command-line guy, my memory works better with images, but everything you will see below could be done by gcloud command-line.

Hasura

I will assume that you know Hasura already, but if not, here's a quick catch up. Why Hasura? Hasura is an open source engine that connects to your databases & microservices and auto-generates a production-ready GraphQL backend.

  • High performance GraphQL serverArrow forward: Hasura’s compiler approach avoids the N+1 problem. Get thousands of GraphQL queries/sec on ~50MB of RAM.
  • Planet-scale workloads with PostgreSQLArrow forward Hasura works with your favourite Postgres flavours and extensions. Read how we’ve taken Hasura to over 100TB databases and other experiments with the cutting-edge Postgres ecosystem.
  • Scaling to 1 million GraphQL subscriptionsArrow forward Hasura provides scalable & secure GraphQL subscriptions out-of-the-box.

Google Cloud Run

Why deploy at Cloud Run? Cloud Run abstracts away all infrastructure management by automatically scaling up and down from zero almost instantaneously—depending on traffic. Cloud Run only charges you for the exact resources you use. And you get a free SSL domain like https://something.a.run.app which for a back-end service is more than enough for a lot of scenarios.

Google Cloud Pre-Configs

IAM Permissions

Before you can start using the Google Cloud Run resource, and if you are not the administrator, at least you need to have the "Cloud Run Admin" and "Service Account User" roles assigned to your account from your IAM options.
Alt Text

Now you are ready to start here:
Alt Text

Depending on your options, you will see "Start Using Cloud Run" or "Go To Cloud Run", click on it.

Enable Cloud SQL Admin API

If Cloud SQL Admin API has not been used in project before or it is disabled, you have to enable it here or the container running Hasura will fail the connection to the database.

Create a Service Account for Hasura

Or your Cloud Run instance won't have permissions to use the Cloud SQL Proxy, so you need to create a service account with the Role "Cloud SQL Client". First create the service account and the add the role.
Alt Text

Push the Hasura Image

Before you go ahead, you will need to enter the Container Image URL of Hasura in order to have it available later. To do this, we will need a little bit of command-line. Open the cloud shell:
Alt Text
And run the following commands:

docker pull hasura/graphql-engine
docker tag docker.io/hasura/graphql-engine:latest [HOSTNAME]/[PROJECT-ID]/[IMAGE]
docker push [HOSTNAME]/[PROJECT-ID]/[IMAGE]
gcloud container images list-tags [HOSTNAME]/[PROJECT-ID]/[IMAGE]

Check the values you should use for [HOSTNAME]/[PROJECT-ID]/[IMAGE] here
In my case I used: eu.gcr.io/grizzly-987e4/hasura
You may need to enable the Google Container Registry API in Cloud Console

Google Cloud Run Setup

Finally we are ready to start the setup of an Cloud Run instance with Hasura here.

Service Settings

1st Step

Alt Text

2nd Step

You will need to enter the Container Image URL of Hasura that we push it earlier.
Alt Text
Alt Text

3rd Step

Now you need to set Hasura configs at Advanced Settings, where you can set the Container, Variables and Connections. I will assume that you know the meaning of the Hasura variables to set an instance.

Container

Alt Text

Variables

Alt Text

And now the tricky part, usually, an connection string structure is

postgres://<user>:<password>@<host>/<database>

And here is the little detail in HASURA_GRAPHQL_DATABASE_URL that does the magic of not needing to embed the Cloud SQL Proxy into the container, look to the host variable as a querystring parameter:

postgres://<user>:<password>@/<database>?host=/cloudsql/<instance_name>

The instance_name you can grab it from the SQL section of your account, look for the Instance connection name:
Alt Text

Connections

Now, let's return to the configuration where you need to also select the Cloud SQL Connection, the same of the above.

And now, if everything is OK, you are ready to click on "Create" and check the success. If anything goes wrong you can check the Logs section.
Alt Text

If you have any questions or if something is not clear, post your comments and I will try to help you.

Posted on May 30 by:

davidoliveira profile

David Oliveira

@davidoliveira

20 years of experience in full-stack web development. Always looking around to choose my dev stack and own my path.

Discussion

markdown guide
 

hi

this is great guide. I have a question.

how to define this? "user" "password" and "database" ? :) thank you

 

Great Guide! Now the missing piece is Websockets and Event Trigger support!