├── .env.example ├── .gitignore ├── Caddyfile ├── README.md └── docker-compose.yml /.env.example: -------------------------------------------------------------------------------- 1 | HOST=http://localhost 2 | DB_USERNAME=db-username 3 | DB_PWD=db-pwd 4 | DB_HOST=my-rds-instance.eu-west-1.rds.amazonaws.com 5 | DB_PORT=3306 6 | INTERNAL_GRAPHCOOL_DB_NAME=graphcool 7 | 8 | PORT=4466 9 | 10 | SQL_CLIENT_CONNECTION_LIMIT=30 11 | SQL_INTERNAL_CONNECTION_LIMIT=30 12 | 13 | SCHEMA_MANAGER_SECRET=269172f04d342a9748a71bb9caf1489f45f85b49 14 | CLUSTER_PUBLIC_KEY=acb1ca4ee29ca7c83cad65cdbe7c38afa3c72032 15 | 16 | CLUSTER_VERSION=local 17 | BUGSNAG_API_KEY="empty" 18 | RABBITMQ_URI="" -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .env 3 | package.json 4 | yarn.lock 5 | proxieserrors.log -------------------------------------------------------------------------------- /Caddyfile: -------------------------------------------------------------------------------- 1 | prisma.my-domain.com 2 | 3 | proxy / localhost:4466/ { 4 | transparent 5 | } 6 | 7 | errors proxieserrors.log -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

[Unmaintained] - How to connect a Prisma docker instance to your AWS RDS MySQL database

2 | 3 |
4 | 5 | - Only works with Prisma 1.6 and below. I will be updated the repo in the coming weeks 6 | - Project uses [graphcool/prisma](https://github.com/graphcool/prisma), Docker and AWS RDS MySQL. 7 | - The ultimate goal to is create a graphql server with [`graphql-yoga`](https://github.com/graphcool/graphql-yoga), `prisma` and AWS RDS MySQL. 8 | - In this repo, we are focused on setting up `prisma` so it can talk to our MySQL instance 9 | - Please feel free to make suggested improvements and raise issues. 10 | 11 | ## Current status 12 | 13 | I had to hardcode `CLUSTER_PUBLIC_KEY` into docker-compose.yml for Prisma but it **works!** 14 | 15 | ## Setting up your AWS MySQL RDS database 16 | 17 | Use the following [tutorial](https://gist.github.com/marktani/8631cb9c63d0973bcdd8bff19d6162c2) to setup a MySQL database on AWS RDS's free tier 18 | 19 | ## Check your MySQL instance is available 20 | 21 | Using the local mysql client, connect to your AWS RDS database 22 | 23 | ```sh 24 | mysql -u {username} -p{password} -h {remote server ip} {DB name} 25 | ``` 26 | 27 | ## Configure Docker 28 | 29 | - Make sure you have Docker installed 30 | - Save the [`docker-compose.yml`](./docker-compose.yml) file 31 | - Create your `.env` file. You can use [`.env.example`](./.env.example) as a template 32 | - `SQL_INTERNAL_DATABASE` needs to be set to `graphcool`. I think it might be an internal db used by prisma and the name of the db is not related to your db name in AWS 33 | 34 | **Note** When creating your `.env` file, do not use double quotes (`"`). Especially on the DB_HOST because they cause a `java.net.UnknownHostException` error. 35 | 36 | For those interested, I'm using the following docker repo: [`prismagraphql/prisma`](https://hub.docker.com/r/prismagraphql/prisma/) 37 | 38 | ## Configure Prisma 39 | 40 | Open `~/.prisma/config.yml` and add a new entry (here called example-cluster) to the clusters map: 41 | 42 | ``` 43 | clusters: 44 | example-cluster: 45 | host: 'http://localhost:4466' 46 | ``` 47 | 48 | Before applying that definition, we have to generate a public/private-keypair so that the CLI is able to communicate with this Prisma server. Head over to https://api.cloud.prisma.sh/ and execute the following query: 49 | 50 | ``` 51 | { 52 | generateKeypair { 53 | public 54 | private 55 | } 56 | } 57 | ``` 58 | 59 | Make sure to store those values in a safe place. Normally I would copy the public key and paste it into your `.env` file under `CLUSTER_PUBLIC_KEY=your-key`. However I've had issues here and have been hardcoding my `CLUSTER_PUBLIC_KEY` into my `docker-compose.yml` file. 60 | 61 | Copy and paste the private key into `~/.prisma/config.yml` 62 | 63 | ``` 64 | clusters: 65 | example-cluster: 66 | host: 'http://localhost:4466' 67 | clusterSecret: my-private-key 68 | ``` 69 | 70 | Alternatively you can use the `prisma cluster add` command to manage your `~/.prisma/config.yml` file 71 | 72 | ## Launch Docker 73 | 74 | The following command starts the docker instance using the docker-compose.yml file 75 | 76 | ```sh 77 | docker-compose up 78 | ``` 79 | 80 | Check your instance is running 81 | ```sh 82 | docker ps 83 | ``` 84 | 85 | Get the logs 86 | ```sh 87 | docker logs prisma 88 | ``` 89 | 90 | Your instance playground should now be available at http://localhost:4466/cluster 91 | 92 | If you need it you can use the following to take down the instance 93 | ```sh 94 | docker-compose down 95 | ``` 96 | 97 | ## Configure and deploy your prisma service 98 | 99 | Initialize your project using 100 | 101 | ``` 102 | prisma init 103 | ``` 104 | 105 | In your `.env` file, update `PRISMA_SECRET` (and if you selected the graphql-server option your `APP_SECRET`) with a secret. You could use the `openssl rand -hex 20`. 106 | 107 | 108 | Deploy your project 109 | 110 | ``` 111 | prisma deploy 112 | ``` 113 | 114 | Select your new private cluster (in this case `example-cluster`) 115 | 116 | ``` 117 | ? Please choose the cluster you want to deploy "project-name@dev" to 118 | 119 | workspace/prisma-eu1 Free development cluster (hosted on Prisma Cloud) 120 | workspace/prisma-us1 Free development cluster (hosted on Prisma Cloud) 121 | ❯ example-cluster Local cluster (requires Docker) 122 | 123 | You can learn more about deployment in the docs: http://bit.ly/prisma-graphql-deployment 124 | ``` 125 | 126 | **Alternatively you can edit your `.env` file on an exiting project** 127 |
128 | In order to deploy to your local prisma cluster from an existing project, you can edit your `.env` file. 129 | 130 | ``` 131 | PRISMA_ENDPOINT=http://localhost:4466// 132 | PRISMA_CLUSTER=example-cluster 133 | ``` 134 | 135 | ## Open playground 136 | 137 | To query with your new service on your cluster. Open your browser and go to: `http://localhost:4466//`. `name-of-service` and `stage` can be found in your `prisma.yml` file which you used to deploy your prisma service. 138 | 139 | At this stage you haven't been authenticated so in the CLI go to your `prisma.yml` directory and run the `prisma token` command. 140 | 141 | In your browser, copy and paste your token into your playground in the `HTTP HEADERS` section like so: 142 | 143 | `{ "Authorization": "Bearer "}` 144 | 145 | ## Deploying your cluster to AWS EC2 or another cloud provider 146 | 147 | I'm not doing to tell you how to configure your Linux server in this section. That will up to you. You need to install [Docker](https://www.docker.com/get-docker) and [Caddy](https://caddyserver.com/download) (our webserver). 148 | 149 | I've been using Ubuntu on AWS EC2 so open up ports 80 and 443 on `ufw` and in the security group for HTTP and HTTPS respectively. Don't worry Caddy automatically forwards all HTTP traffic to HTTPS for you. 150 | 151 | Then save the `docker-compose.yml` file and `Caddyfile` from this repo on to your server. 152 | 153 | Launch your `prisma` app with: 154 | 155 | ``` 156 | docker-compose up -d 157 | ``` 158 | 159 | Edit your `Caddyfile` so it looks like the example below. `Caddy` will automatically generate and renew your SSL certificates with Let's Encrypt which is amazing. The first line of the config file is your domain at which you want to host your prisma cluster. **Note** Let's Encrypt does not like AWS domains so you can't use your EC2 URL. I created a subdomain and pointed it to my EC2 instance. 160 | 161 | ``` 162 | prisma.my-domain.com 163 | 164 | proxy / localhost:4466/ { 165 | transparent 166 | } 167 | 168 | errors proxieserrors.log 169 | ``` 170 | 171 | In the same directory as your `Caddyfile`, launch `caddy` using: 172 | 173 | ``` 174 | caddy& 175 | ``` 176 | 177 | Then you're done and your private prisma cluster should be up and running with an encrypted endpoint. 178 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | services: 3 | prisma: 4 | image: prismagraphql/prisma:1.2.8 5 | container_name: "prisma" 6 | restart: always 7 | ports: 8 | - "0.0.0.0:${PORT}:${PORT}" 9 | environment: 10 | PORT: $PORT 11 | SCHEMA_MANAGER_SECRET: $SCHEMA_MANAGER_SECRET 12 | SCHEMA_MANAGER_ENDPOINT: ${HOST}:${PORT}/cluster/schema 13 | CLUSTER_ADDRESS: ${HOST}:${PORT} 14 | # CLUSTER_PUBLIC_KEY: "${CLUSTER_PUBLIC_KEY}" 15 | CLUSTER_PUBLIC_KEY: "-----BEGIN PUBLIC KEY-----\r\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo5gLpcZNsSM/JU95fm+b\r\nmoT1Q1GifzFB3NX0ckSDj4l4ex2rDNFm9cfxbfo7kYLW7zOUTZY7o5KUUnrXkbv2\r\n2lwdfLc0wsMwYAxxIFGC5bz1WnwApbbsSrHAjyEIW1AoNlGOyWFcaX9MYQoiRM03\r\nwhnlx3n0JF9onbde3dagUB+wX3JxTYTYTJYCDqpakAtBCL665lB2jq8x1ZuNvTJC\r\n9qrziPTZipz7rsgZBoq8lMls9eIoU1Q4HzxqFadMbwuG7Lnk74qknquPcoyBSRWg\r\nuLdkcFuyqBWroswjUFyN+ay0DNzA2A6FTV52w4smqIAupuPG8A+Bx+JeI5/83tDb\r\n9QIDAQAB\r\n-----END PUBLIC KEY-----\r\n" 16 | SQL_CLIENT_HOST_CLIENT1: $DB_HOST 17 | SQL_CLIENT_HOST_READONLY_CLIENT1: $DB_HOST 18 | SQL_CLIENT_HOST: $DB_HOST 19 | SQL_CLIENT_PORT: $DB_PORT 20 | SQL_CLIENT_USER: $DB_USERNAME 21 | SQL_CLIENT_PASSWORD: $DB_PWD 22 | SQL_CLIENT_CONNECTION_LIMIT: $SQL_CLIENT_CONNECTION_LIMIT 23 | SQL_INTERNAL_HOST: $DB_HOST 24 | SQL_INTERNAL_PORT: $DB_PORT 25 | SQL_INTERNAL_USER: $DB_USERNAME 26 | SQL_INTERNAL_PASSWORD: $DB_PWD 27 | SQL_INTERNAL_DATABASE: $INTERNAL_GRAPHCOOL_DB_NAME 28 | SQL_INTERNAL_CONNECTION_LIMIT: $SQL_INTERNAL_CONNECTION_LIMIT 29 | BUGSNAG_API_KEY: "" 30 | ENABLE_METRICS: "0" 31 | JAVA_OPTS: "-Xmx1G" --------------------------------------------------------------------------------