Upsun User Documentation

Fast-track guide to deploying a Flask app on Upsun

Upsun Beta access

Test and provide feedback for our newest offering - Upsun!

You can register for the Beta by clicking here and completing the form.

Sign up for Beta access

Here are all the steps required to successfully generate a Flask app with Cookiecutter, and deploy it on Upsun.

T: Run the line in a terminal/command prompt

  1. T: pip3 install cookiecutter

  2. T: cookiecutter https://github.com/cookiecutter-flask/cookiecutter-flask.git

  3. Follow the prompts to submit the following information:

    • Name
    • Email
    • GitHub username
    • Project name
    • App name
    • Description
    • Pipenv
    • Python version
    • Node version
    • Heroku
  4. cd into project directory (app_name defined at step 3)

  5. T: git init .

  6. T: git branch -m main

  7. T: upsun project:init

  8. Follow the prompts:

    • Select Python
    • Select your services
  9. T: git add .

  10. T: git commit -m "Init commit"

  11. T: upsun p:create

  12. Follow the prompts:

    • Create or select your organization
    • Name your project
    • Choose a region
    • Enter the branch name defined at step 6
    • Set the project as the remote (for now)
    • Select Y to continue
  13. Open .upsun/config.yaml

  14. Find the section describing variables

  15. Uncomment # variables and the env: line

  16. Add the following configuration:

    .upsun/config.yaml
    variables:
      env:
        FLASK_APP: autoapp.py
  17. Find the section for mounts

  18. Add the following configuration:

    .upsun/config.yaml
    mounts:
      "app_name/static":
        source: "storage"
        source_path: "static_assets"
  19. Find the section for hooks:build

  20. Add the following configuration:

    .upsun/config.yaml
    hooks:
      build: |
        set -eux
        pip install --upgrade pip
        pip install -r requirements.txt
        npm install      
  21. Find the section for hooks:deploy

  22. Add the following configuration:

    .upsun/config.yaml
       deploy:
         set -eux
         npm run build 
  23. Find the section for web:commands:start

  24. Add the following configuration:

    .upsun/config.yaml
    web:
      commands:
        start: "flask run -p $PORT"
      upstream:
        socket_family: tcp
  25. T: git add .upsun/config.yaml

  26. T: git commit -m "Adds FLASK_APP env var, adds mount for static builds, build commands, npm run build on deploy, web start command"

  27. Open your .environment file

  28. Change DATATABASE_URL to: your_service_type://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}

  29. Add the following lines to .environment:

    .environment
        export SECRET_KEY="${PLATFORM_PROJECT_ENTROPY}"
        export FLASK_DEBUG=$( [ "${PLATFORM_ENVIRONMENT_TYPE}" = "production" ] && echo 0 || echo 1)
        export FLASK_ENV="${PLATFORM_ENVIRONMENT_TYPE}"
        export GUNICORN_WORKERS=1
        export LOG_LEVEL=$( [ "${PLATFORM_ENVIRONMENT_TYPE}" = "production" ] && echo "info" || echo "debug")
        # In production, set to a higher number, like 31556926
        export SEND_FILE_MAX_AGE_DEFAULT=$( [ "${PLATFORM_ENVIRONMENT_TYPE}" = "production" ] && echo 31556926 || echo 0)
  30. T: git add .environment

  31. T: git commit -m "Adds needed flask env vars"

  32. T: upsun environment:push

  33. Enter Y.

  34. If error, T: upsun resources:set and follow the prompts.

  35. T: python3 -m venv env

  36. T: source venv/bin/activate

  37. T: pip install --upgrade pip

  38. T: pip install -r requirements.txt

  39. T: upsun tunnel:open -y

  40. Open .upsun/config.yaml

  41. Add the following variables:

    .upsun/config.yaml
    export PLATFORM_RELATIONSHIPS="$(upsun tunnel:info --encode)"
    export PORT=8888
    export PLATFORM_PROJECT_ENTROPY=$(openssl rand -base64 32)
    export PLATFORM_ENVIRONMENT_TYPE=production
  42. T: source ./.environment

  43. T: flask db init

  44. T: flask db migrate

  45. Open .upsun/config.yaml

  46. Find the section for hooks:deploy

  47. Add the following configuration:

    .upsun/config.yaml
    deploy: |
    set -eux
    npm run build
    flask db upgrade   
  48. T: git add migrations/*

  49. T: git commit -m "Adds migrations"

  50. T: git add .upsun/config.yaml

  51. T: git commit -m "Adds flask db upgrade to deploy hook"

  52. T: upsun environment:push

The above steps do not include setting up a local development environment, or switching to Gunicorn as a web server.

Is this page helpful?