Creating a Lucky App
Our example will be using a basic Lucky app with PostgreSQL. We’ll assume you already have lucky+crystal installed. Once lucky is installed, we can create a new project by running the following command:Running The Application
To run the application, first runscript/setup to install dependencies and
create the database. Then run lucky dev to start the application.
Connect to the address displayed in the console.
This should bring you to the “Hello Lucky” page.
Install Flyctl and Login
It’s time to installflyctl, the CLI app for managing apps on Fly.io. If you’ve already installed it, carry on. If not, hop over to our installation guide. Once that’s installed you’ll want to log in to Fly.io.
Launch the app on Fly
When you runfly launch from the newly-created project directory, the launcher provides some defaults for your new app, and gives you the option to tweak the settings.
Run:
y at the prompt to open the Fly Launch page, and make the following changes to your app config:
- Change the default app name and region, if needed.
- For Databases, select Fly Postgres, give the Postgres database app a name (for example, your app name with
-dbappended) and choose a configuration.
- Run the deployment setup task
- Build the image
- Set secrets required by (
SECRET_KEY_BASE, for example) - Deploy the application in your selected region
Make sure to note your Postgres credentials from the output.
fly apps open to see your deployed app in action.
Try a few other commands:
fly logs- Tail your application logsfly status- App deployment detailsfly status -a postgres-database-app-name- Database deployment detailsfly deploy- Deploy the application after making changes
Inside fly.toml
The fly.toml file now contains a default configuration for deploying your app. In the process of creating that file, flyctl has also created a Fly-side application slot of the same name, “hello-lucky”. If we look at the fly.toml file we can see the name in there:
flyctl command will always refer to this file in the current directory if it exists, specifically for the app name/value at the start. That name will be used to identify the application to the Fly platform. The rest of the file contains settings to be applied to the application when it deploys.
We’ll have more details about these properties as we progress, but for now, it’s enough to say that they mostly configure which ports the application will be visible on.
Deploying to Fly
To deploy changes to your app, just run just run:fly.toml file, and get the app name hello-lucky from there. Then flyctl will start the process of deploying our application to the Fly platform. Flyctl will return you to the command line when it’s done.
Alternatively, you can use GitHub Actions to deploy your app to Fly
Viewing the Deployed App
Now the application has been deployed, let’s find out more about its deployment. The commandfly status will give you all the essential details.
flyctl ips list:
Connecting to the App
The quickest way to browse your newly deployed application is with theflyctl apps open command.
Multi-Region Deployment with Postgres (Optional)
Fly also supports multi-region deployments. To deploy to multiple regions, first Create a PostgreSQL Cluster, then follow these steps:- Configure your primary region by setting the
PRIMARY_REGIONin your fly.toml.
- Add the superfly/fly.cr shard to your project
- Require
fly/pg/error_handlerandfly/avramafteravram
- Add the
Fly::PG::ErrorHandlermiddleware to your list of middlewares, after theLucky::ErrorHandler
-
Deploy with
fly deploy -
You can see replays in the
fly log