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.
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.
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.
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.
Depending on your options, you will see "Start Using Cloud Run" or "Go To Cloud Run", click on it.
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.
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.
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:
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]
Finally we are ready to start the setup of an Cloud Run instance with Hasura here.
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.
And now the tricky part, usually, an connection string structure is
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:
Now, let's return to the configuration where you need to also select the Cloud SQL Connection, the same of the above.
If you have any questions or if something is not clear, post your comments and I will try to help you.