Skip to content

Instantly share code, notes, and snippets.

@intabulas
Last active June 3, 2023 01:10
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save intabulas/d617ed6df5fef8c50dd99fbcedbacdcd to your computer and use it in GitHub Desktop.
Save intabulas/d617ed6df5fef8c50dd99fbcedbacdcd to your computer and use it in GitHub Desktop.
Deploying SurrealDB to Fly.io

These are the rough steps for getting a surrealdb instance running on fly.io and connecting to it. This is heavily based off of steps posted in the surrealdb discord by rvdende. View Origional Post.

These steps work just fine for the hobby (pay as you go) plan.

HEADS UP By default this will create an instance on a single shared cpu with 256m memory. This is pretty darn small to do anything but experiment, but its free. You can scale your instance to something more useable by visiting the https://fly.io/apps/<appname>/scale. Obviously scaling to larger instances will incur higher costs, please refer to fly.io pricing

Installing fly.io client

source: fly.io docs

brew install flyctl

Optionally create a free account if needed

source: fly.io docs

flyctl auth signup

Login to your fly.io account

source: fly.io docs

flyctl auth login

Create a Dockerfile to run SurrealDB

Create a directory locally, cd into it and create a new Dockerfile. Note we aren't providing a root user username/password to the start command, we will bre providing those as environment variables.

FROM surrealdb/surrealdb:latest
EXPOSE 8080
CMD ["start", "--bind","0.0.0.0:8080", "file://data/srdb.db"]

Launch your Instance.. sort of :)

fly launch

You will be prompted for various questions. Give your app a rememberable name since it will be used in the URL. Pick a region close to you and most importantly, answer NO when prompted if you wish to deploy immediately.

Create a volume

We need to create a volume for surreal to write the database to. Lets create a small 1g volume. Right now you can not scale a volume (you need to create a new one), but fly.io is working on supporting this. You can get up a total of 3g of persistent volume storage (total) for free.

fly volumes create data --region <region> --size 1

Now lets edit and the fly.toml file that was created by `fly launch. Put this at the end, no indentations or spaces.

[mounts]
source="data"
destination="/data"

Secrets

Now lets add our username and password as secrets. You can see the environment variables suurealdb supports by running surreal start -h

fly secrets set USER=root
fly secrets set PASS=<passwordhere>

Let's Deploy

fly deploy

Connecting

Now that your surrealdb instance is up and running, you need to be able to connect to it. Even though we exposed port 8080 in the Dockerfile, fly will map that appropriatly to port 80/443.

If your connecting via a websocket client library, surrealdb-go in my case, you would connect like the following. Note the use of wss://

db, err := surrealdb.New("wss://<appname>.fly.dev/rpc")
if err != nil {
	fmt.Printf("error creating socket: %s", err)
	os.Exit(2)
}

Surreal sql Command

If your connecting via the surreal cli either, use the connection string of --conn https://<appname<.fly.dev. Please don't forget to pass --user/--pass with the values you used when creating secrets.

Third Party tools like rvdende's surrealreact

When creating a connection, specify the host as https://<appname>.fly.dev/rpc

@MisesGdynia
Copy link

Env variables are not used automatically when docker image runs, and it runs with root authentication disabled.

@MisesGdynia
Copy link

Solved! XD Latest version surrealdb of env variables name is SURREAL_PASS and SURREAL_USER

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment