Giter Club home page Giter Club logo

hello_elixir's Introduction

HelloElixir!

This is an example project demonstrating how to deploy an Elixir and Phoenix application on Fly.io.

Application Structure

This creates a basic Phoenix application that uses a PostgreSQL database. The Elixir deployment approach used here is building a release through Docker.

  • Dockerfile - An Elixir release is built through Docker
  • .dockerignore - Exclude pulling in Elixir deps and Node packages since they might have native compilation from your dev environment
  • lib/hello_elixir/release.ex - Executed during deploy from fly.toml to run our database migrations
  • config/runtime.exs - The runtime ENV values we expect for production

Fly Configuration

  • fly.toml - Fly deployment configuration

Secrets

Elixir has a mix task that can generate a new Phoenix key base secret. Let's use that.

mix phx.gen.secret

It generates a long string of random text. Let's store that as a secret for our app. When we run this command in our project folder, flyctl uses the fly.toml file to know which app we are setting the value on.

fly secrets set SECRET_KEY_BASE=<GENERATED>

Postgres Database

fly postgres create
? App name: hello-elixir-db
Automatically selected personal organization: Mark Ericksen
? Select region: sea (Seattle, Washington (US))
? Select VM size: shared-cpu-1x - 256
? Volume size (GB): 10
Creating postgres cluster hello-elixir-db in organization personal
Postgres cluster hello-elixir-db created
  Username:    <USER>
  Password:    <PASSWORD>
  Hostname:    hello-elixir-db.internal
  Proxy Port:  5432
  PG Port: 5433
Save your credentials in a secure place, you won't be able to see them again!

Monitoring Deployment

2 desired, 2 placed, 2 healthy, 0 unhealthy [health checks: 6 total, 6 passing]
--> v0 deployed successfully

Connect to postgres
Any app within the personal organization can connect to postgres using the above credentials and the hostname "hello-elixir-db.internal."
For example: postgres://<USER>:<PASSWORD>@hello-elixir-db.internal:5432

See the postgres docs for more information on next steps, managing postgres, connecting from outside fly:  https://fly.io/docs/reference/postgres/

We can take the defaults which select the lowest values for CPU, size, etc. This is perfect for getting started.

Attach our App to the Database

We can use flyctl to attach our app to the database which also sets our needed DATABASE_URL ENV value.

fly postgres attach --postgres-app hello-elixir-db
Postgres cluster hello-elixir-db is now attached to icy-leaf-7381
The following secret was added to icy-leaf-7381:
  DATABASE_URL=postgres://<NEW_USER>:<NEW_PASSWORD>@hello-elixir-db.internal:5432/icy_leaf_7381?sslmode=disable

We can see the secrets that Fly is using for our app like this.

fly secrets list
NAME            DIGEST                           DATE
DATABASE_URL    830d8769ff33cba6c8b29d1cd6a6fbac 1m10s ago
SECRET_KEY_BASE 84c992ac7ef334c21f2aaecd41c43666 9m20s ago

Looks like we're ready to deploy!

Deploy

Once you've went through the steps of fly launch:

fly deploy

... will bring up your app!

hello_elixir's People

Contributors

brainlid avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.