docs/1.12/03-Tutorials2/06-Deploy-Prisma-Servers/06-Zeit-Now-and-Google-Cloud-SQL.md
In this tutorial, you’re going to learn how to deploy a Prisma server to Zeit Now. The server will be backed by a MySQL database hosted in Google Cloud Platform (GCP).
Zeit Now provides real-time Node and Docker cloud deployments.
Google Cloud SQL provides fully-managed PostgreSQL & MySQL instances. You may follow the tutorials and quick guides to configure and setup your SQL instance using either the console or command line.
For the purpose of this tutorial you will need to have signed up for GCP, created a project and enabled the Cloud SQL Admin API (see quickstart for more information).
You also should install the Cloud SDK.
Sign up to zeit.co.
Install the now-cli)
We'll be creating a MySQL instance called prisma on GCP via the command line. You may also do this via the console.
gcloud sql instances create prisma --tier=db-f1-micro --authorized-networks=0.0.0.0/0 --region=europe-west1
You'll see the following output:
NAME DATABASE_VERSION LOCATION TIER ADDRESS STATUS
prisma MYSQL_5_6 europe-west1-d db-f1-micro xx.xx.xx.xx RUNNABLE
europe-west1 region as this is the same location as Zeit in Europe. The default region is us-central.db-f1-micro is the smallest possible. The default tier is db-n1-standard-1.Create a user called prisma with a password of my-secret.
gcloud beta sql users create prisma --instance=prisma --password=my-secret
Your MySQL Instance is now up and running.
Note down the IP address of the instance, your username and password. You'll need this information later!
Deployment to Now consists of 3 files:
First we will add secrets to Now to store the SQL password from earlier and the Prisma API password.
now secret add sql-password my-secret
now secret add prisma-management-api-secret so-secret
Learn more about Now secrets: https://zeit.co/docs/getting-started/secrets
Create a file config.yml. This contains the Prisma Server configuration.
managementApiSecret: PRISMA_MANAGEMENT_API_SECRET
port: 4466
databases:
default:
connector: mysql
host: SQL_HOST
port: 3306
user: prisma
password: SQL_PASSWORD
migrations: true
active: true
SQL_HOST, SQL_PASSWORD and PRISMA_MANAGEMENT_API_SECRET will be replaced with Now build-time environment variables.Create now.json. This file contains our build environment variables and references to the secrets.
Change xx.xx.xx.xx to the Cloud SQL IP address returned in 1.1.
{
"name": "prisma-now",
"type": "docker",
"build": {
"env": {
"SQL_HOST": "xx.xx.xx.xx",
"SQL_PASSWORD": "@sql-password",
"PRISMA_MANAGEMENT_API_SECRET": "@prisma-management-api-secret"
}
},
}
Learn more about environment variables: https://zeit.co/docs/features/build-env-and-secrets and more about
now.json: https://zeit.co/docs/features/configuration. To take advantage of Zeit's automatic scaling: https://zeit.co/docs/getting-started/scaling
Create a file called Dockerfile. This tells Now how to build and configure the Prisma server.
FROM prismagraphql/prisma:1.13-beta
ARG SQL_HOST
ARG SQL_PASSWORD
ARG PRISMA_MANAGEMENT_API_SECRET
ARG PRISMA_CONFIG_PATH
ENV PRISMA_CONFIG_PATH prisma.yml
COPY config.yml prisma.yml
RUN sed -i s/SQL_HOST/$SQL_HOST/g prisma.yml
RUN sed -i s/SQL_PASSWORD/$SQL_PASSWORD/g prisma.yml
RUN sed -i s/PRISMA_MANAGEMENT_API_SECRET/$PRISMA_MANAGEMENT_API_SECRET/g prisma.yml
EXPOSE 4466
You can edit Dockerfile with the version of Prisma you wish to deploy from dockerhub
Then you are ready to deploy.
now
Now will go through it's deployment process, which will take a few minutes.
From here you may wish to alias the deployment in Now: https://zeit.co/docs/features/aliases before updating your endpoint in
prisma.yml.
An example project can be found here: https://github.com/develomark/prisma-now.