Guide

October 23, 2023

Launch a Phoenix App

This guide covers all aspects of setting up an Phoenix Elixir local development environment and deploying the application to the Cloud.

Overview

Phoenix Elixir apps and Noop work very well together.

The Noop Template Blueprint contains all the details about the application. Including build process, required resources, application routing rules, runtime configuration, etc.

With some basic setup (available as a Noop Template on Github), it’s possible to have a Phoenix app up and running in a few minutes.

The technique outlined below uses Mix Releases available with Elixir 1.9.

The Template repository can be used as a reference to retrofit an existing Phoenix app, or a starting point for a new one.

The Template contains a Runbook, which will automate most of the setup in Noop Cloud. All the remaining configuration is outlined here.

Setup

Noop dependencies

Make sure you have Workshop installed locally or a Github account connected to Noop Cloud. The remaining setup is identical in both.

Project setup

If you don’t already have Elixir installed, follow the instructions on the Elixir site.

Instructions for creating a new Phoenix project can be found on the official Phoenix website.

This is the command to create a new Phoenix app:

mix phx.new hello
cd hello

Noop leverages Mix Releases, which requires one additional setup step:

mix phx.gen.release

The Phoenix website also includes instructions for setting up Mix Releases. The Noop-specific build and deployment configuration are modeled after the Dockerfile in the instructions above.

Template Configuration

After you have the Phoenix project set up, you need to add the Noop Template configuration to create a local or cloud Deployment.

Create a .noop directory in your project root.

mkdir .noop

Next create a blueprint.yaml file to define the application setup.

The content below is a basic configuration:

---
components:
  - name: WebApp
    type: service
    image: elixir:latest
    port: 4000
    build:
      steps:
        - directory: /app
        - run: apt-get update
        - run: apt-get install -y postgresql-client
        - run: mix local.rebar --force
        - copy: [mix.exs, mix.lock]
          destination: ./
        - run: mix deps.get --only prod
        - run: mkdir config
        - copy: [config/config.exs, config/prod.exs]
          destination: ./config/
        - run: MIX_ENV=prod mix deps.compile
        - copy: priv
          destination: ./priv
        - copy: assets
          destination: ./assets
        - run: MIX_ENV=prod mix assets.deploy
        - copy: lib
          destination: ./lib
        - run: MIX_ENV=prod mix compile
        - copy: config/runtime.exs
          destination: ./config/
        - copy: rel
          destination: ./rel
        - run: MIX_ENV=prod mix release
        - run: mv /app/_build/prod/rel/phoenix_blueprint ./output
    runtime:
      command: /app/output/bin/server
      resources:
        - PgDatabase
      variables:
        DATABASE_URL:
          $resources: PgDatabase.url
routes:
  - target:
      component: WebApp

resources:
  - name: PgDatabase
    type: postgresql

Automating Cloud Setup

For Cloud users, most of the Application setup is automated, including Environment creation, database provisioning, building and deploying, etc., using a Runbook. To create a Runbook, make a file called quickstart.yaml in the .noop/runbooks directory.

Here is the Runbook content:

name: Quickstart Setup

description: Demo full stack setup of a Phoenix app with internet endpoint

workflow:
  inputs:
    - name: EnvironmentName
      description: What should we name your new environment?
      type: string
      required: true
      default: Quickstart Demo

    - name: Cluster
      type: Cluster
      description: Which cluster should the environment launch on?
      required: true

  timeout: 300

  steps:
    - name: Environment
      action: EnvironmentCreate
      params:
        name:
          $inputs: EnvironmentName
        production: false
        appId:
          $runbook: Application.id
        clusterId:
          $inputs: Cluster.id

    - name: EnvironmentVarPlaceholder
      action: VariableCreate
      params:
        envId:
          $steps: Environment.id
        key: SECRET_KEY_BASE
        value: PLACEHOLDER
        component: WebApp # Refers to the component specified in the .noop/blueprint.yaml
        secret: true

    - name: Build
      action: BuildExecute
      params:
        sourceCodeId:
          $runbook: SourceCode.id
        appId:
          $runbook: Application.id

    - name: Resources
      action: ResourceLaunch
      params:
        envId:
          $steps: Environment.id
        sourceCodeId:
          $runbook: SourceCode.id

    - name: Deploy
      action: DeploymentExecute
      params:
        envId:
          $steps: Environment.id
        buildId:
          $steps: Build.id

    - name: Endpoint
      action: InternetEndpointRandom
      params:
        orgId:
          $runbook: Organization.id
        routes:
          - name: 'Demo Environment'
            target:
              environments:
                - $steps: Environment.id

Launch on Noop

Cloud users can use the Runbook to automate most of the setup described below.

Create an Application

Select “New Application” and assign an appropriate name for your new app.

Cloud Setup

In Cloud, once the Application is created you will be asked if you want to execute a Runbook. The name of the Runbook for this Template is “Quick Start”.

Choose the default cluster and run it. Behind the scenes this Runbook is:

  • Creating an Environment
  • Setting up placeholders for required environment variables
  • Launching the database
  • Building the source code
  • Deploying the build
  • And finally, creating a public-facing endpoint to serve the application from

In order for the application to run properly we need to set the SECRET_KEY_BASE environment variable. From your development environment copy the output generated from the following command:

mix phx.gen.secret

Set the value of the SECRET_KEY_BASE secret in the Environment setting page to the output from the command above.

Once complete, visit the Application page to get the generated endpoint. Your new Phoenix app is now running at that URL.

The Noop Template Blueprint contains the complete source code, ready for deployment.

Workshop Setup

While workshop doesn’t have a Runbook, the setup is also simple.

In Workshop select your Application, which should have been automatically detected by Workshop, or select the appropriate project directory (must include a .noop directory).

Workshop will automatically create a default environment with all Resources launched.

All that is left is to set the SECRET_KEY_BASE environment variable. From your development environment copy the output generated from the following command:

mix phx.gen.secret

Set the value of the SECRET_KEY_BASE secret in the Environment setting page to the output from the command above.

Once done, you should see your application running on the associated endpoint.