Setup Hasura at Google Cloud Run
Tags: hasura, cloudrun, graphql, postgres
Published: 2020-05-30 15:23:48

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](https://hasura.io/) instance on Google Cloud Run, using your database hosted at Google Cloud through the [Cloud SQL Proxy](https://cloud.google.com/sql/docs/postgres/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](https://console.cloud.google.com/iam-admin/iam) options.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/em44x4hpfdengavph9oa.JPG)

Now you are ready to start here: 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8vee2tj9hh91ft59vx26.png)

Depending on your [options](https://console.cloud.google.com/marketplace/details/google-cloud-platform/cloud-run), 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**](https://console.developers.google.com/apis/api/sqladmin.googleapis.com/) 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](https://console.cloud.google.com/iam-admin/serviceaccounts) account and the add the role.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/95tls13l87v1gqamkj14.JPG)

##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](https://dev-to-uploads.s3.amazonaws.com/i/d2e128l2q2jbhyihboha.JPG)
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*](https://cloud.google.com/container-registry/docs/pushing-and-pulling#pulling_images_from_a_registry)
In my case I used: eu.gcr.io/grizzly-987e4/hasura
You may need to [enable](https://console.cloud.google.com/apis/library/containerregistry.googleapis.com) 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*](https://console.cloud.google.com/run/create).

##Service Settings

###1st Step
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/887p9d51iou8j7a60jsx.JPG)

###2nd Step
You will need to enter the Container Image URL of Hasura that we push it earlier.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/47z0qq4u1or4zj4m2dvr.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nlhfsmtre39zhrj3rqta.png)

###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](https://dev-to-uploads.s3.amazonaws.com/i/yvukx8hpnqxk830djcxb.png)

####Variables
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t83ho3mfc5hfei2u0x4q.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/xikqrmy57y7efbbqlvel.JPG)

####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](https://dev-to-uploads.s3.amazonaws.com/i/olif21heryzbvzpscx08.JPG)

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