Randy's Blog

Deploy Logto to Fly.io

I often create some experimental SaaS projects on my spare time. The paint point is when every time I want a user system on my project, I need to setup the auth part again and again. Even next-auth did a lot of work for it, I'm looking for an online identity service that can be integrated in all of my projects.

Logto is an open-source identity service. It looks very promising and fit my need. I played with it and feel it can be the auth solution of my next project.

In this post, I'm going to share how did I deploy Logto to Fly.io, which is a full stack application deploy platform.

Fork and clone the project

Although Logto provides an prebuilt Docker image, I recommend just fork and clone the project to your local machine since it has a Dockerfile in the project. It can be directly deploy on Fly.io by this Dockerfile.

Create a Postgres instance in Fly.io

I suppose you had already setup the fly.io environment. If not, see https://fly.io/docs/

We need a database when using Logto. Fortunately, you can create a Postgres instance in Fly.io easily.

flyctl postgres create

This command will create a postgres install on Fly.io. You must give it an unique service name.

When it finished, you will see the database connection credentials on the output, like:

Postgres cluster randysoft-logto created
  Username:    postgres
  Password:    xxxx
  Hostname:    [your-db-name].internal
  Proxy port:  5432
  Postgres port:  5433

Note that you need to keep it credentials yourself.

Then, proxy the 5432 port to local machine:

flyctl proxy 5432 -a [your-db-name]

Now you can connect the database by postgres://postgres:[password]@localhost:5432.

Seed the database

You need to seed the new database before launching Logto, with Logto CLI:

logto db seed

When it asks you the database url, use postgres://postgres:[password]@localhost:5432/postgres

Launch the app

After creating the database instance, clone the Logto repository and cd in the folder. You can create and launch the app by running the launch command in the project root:

flyctl launch

In this step, you need to give the app a name. For example: your-logto-service.

When you finished this command, a fly.toml file will be generated to the project.

Configurations

In the toml file, you need to configure some environment variables that required by the Logto:

[env]
  TRUST_PROXY_HEADER = 1
  ENDPOINT = "https://[your-logto-service].fly.dev"

The ENDPOINT is the url of the application. Every Fly.io applications has it own subdomain as [service-name].fly.dev.

Next, you need to set a environment variable named DB_URL which should be point to the postgres url you created before. You should not set it in the toml file in plain text, which is not securied. Instead, use flyctl secrets to save secret environment variables:

flyctl secrets set DB_URL="postgres://postgres:[password]@your-db-name.internal/postgres"

Another thing you need to configure is the internal_port. It's 8080 by default but since Logto listen to 3001 by default, you need to modify it to 3001:

[[services]]
  http_checks = []
  internal_port = 3001
  processes = ["app"]

Deploy the app

Everything is ready. Just run flyctl deploy.