DEV Community

David Oliveira
David Oliveira

Posted on • Updated on

Setup Hasura at Google Cloud Run

UPDATE 01/11/2022: When I wrote this, there was no official guide about this, but now, there is, so you should check this: https://hasura.io/docs/latest/enterprise/getting-started/quickstart-google-cloud-run/

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]
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (6)

Collapse
 
clementac profile image
clementAC

Hi,
Nice and simple tutorial thank you. What about scaling?
Cloud run is by nature autoscalable but the cloud sql database is not.
I tried to make lots of request at the same time and everything went down 😕.
There is a max connection we can't outpass. It seems the pooling in hasura is not effective. I have either messages that indicates me 'server error try in 30 seconds' or 'too many connections'.
Reducing the number of concurrency and container make the 'too many connections' disapear but not the 'try in 30 seconds' one.

Do you face the same problem as me?

Collapse
 
davidoliveira profile image
David Oliveira

Hi,
Google Cloud SQL has enough power for my use cases, but maybe this article could help you: cloud.google.com/community/tutoria...
The example is with MySQL but I am pretty sure the same could be applied to Postgres at Goolgle Cloud.

Collapse
 
lughnizaid profile image
Lughnizaid • Edited

hi

this is great guide. I have a question.

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

Collapse
 
lughnizaid profile image
Lughnizaid

Hi i managed it, the data about user, password, and database can be found in cloud sql.

Now i wonder ho to push the local hasura in google cloud....

Best regards

Collapse
 
davidoliveira profile image
David Oliveira

Hi,
First of all, I am sorry by not replied to you about the password of database. I need to check why I am not receiving notifications. Yes, everything about the database is at Google Cloud SQL section.
About your last question "push the local hasura", well, it's also not about Hasura, but about Database management. The direct way and easy way is to dump your local database and restore it into Google Cloud, but it's not the correct way, it will override all your data.
So, even Hasura born as a GraphQL Engine, they do offer us extra cool things, like Migrations, you can use them to do your development locally and apply migrations to your online database instance, and they can be automated with a Continuous Pipeline to push them into your DEV, QA, PRD environments if you have that kind of organization.
You can check all the documentation about it at: hasura.io/docs/1.0/graphql/manual/...
Best Regards

Collapse
 
misterkevin_js profile image
Kevin Rodríguez

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