Upsun User Documentation

Configure your infrastructure

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

To prepare your Flask project for use on Upsun, you need to generate Upsun configuration files, as well as create and configure a Upsun project.

To do so, make sure you have the Upsun CLI installed and authenticated with your Upsun account.

Generate your Upsun configuration files Anchor to this heading

  1. To generate the configuration files needed to deploy your app on Upsun, run the following command:

    Terminal
    upsun project:init

    This command is also available as upsun ify.

  2. Follow the prompts to determine your project requirements:

    • Select Python as the language for your project.
      Upsun then automatically detects your dependency manager.

    • Enter the name of your app.

    • Select the services you need, including at least PostgreSQL, as we’ll use it later.

    • Hit Enter.

    Your Upsun configuration files are generated.

  3. Add all the generated files to your Git repository. To do so, run the following commands:

    Terminal
    git add .
    git commit -m "Initial commit"

Create your Upsun project Anchor to this heading

To deploy your Flask project on Upsun, you need to create and configure a Upsun project first.

  1. To create your Upsun project, run the following command:

    Terminal
    upsun project:create
  2. To configure your Upsun project, follow the prompts:

    • Create or select your organization.

    • Name your project.

    • Select the region where you want your project to be hosted.

    • Specify the branch name.
      Use the same branch name as defined previously.

    • Set Upsun as your repository remote.

    Your Upsun project is created.

Configure your Upsun project Anchor to this heading

To complete the deployment of your Flask app on Upsun, you need to add app-specific configurations to your Upsun project.
To do so, make the following changes to the .upsun/config.yaml file automatically generated by the Upsun CLI:

  1. Specify how your app must be loaded.
    To do so, add a FLASK_APP variable for all your environments and point it to your autoapp.py file. Locate the section dedicated to environment variables:

    .upsun/config.yaml
    # Variables to control the environment. More information: https://docs.upsun.com/create-apps/app-reference.html#variables
    # variables:
    #   env:
    #     # Add environment variables here that are static.
    #     PYTHONUNBUFFERED: "1"

    Uncomment the variables:and env: lines, and add your environment variable:

    .upsun/config.yaml
    variables:
      env:
        FLASK_APP: autoapp.py
  2. Configure some writable disk space to hold the static assets that flask-static-digest generates and npm builds.
    To do so, define the ./<APP_NAME>/static directory as a mount. Locate the section dedicated to mounts:

    .upsun/config.yaml
    # Mounts define directories that are writable after the build is complete.
    # More information: https://docs.upsun.com/create-apps/app-reference.html#mounts
    # mounts:
    #   "/.cache": # Represents the path in the app.
    #     source: "storage" # "local" sources are unique to the app, while "service" sources can be shared among apps.
    #     source_path: "cache" # The subdirectory within the mounted disk (the source) where the mount should point.

    Uncomment the # mounts: line, and add an entry describing where you want to add a writable mount:

    .upsun/config.yaml
    mounts:
        "<APP_NAME>/static":
            source: "storage"
            source_path: "static_assets"

Replacing <APP_NAME> above with the app_name you defined previously.

  1. Instruct Upsun to automatically run npm install when building the application container.
    To do so, customize your build hook. Locate the section dedicated to it:

    .upsun/config.yaml
    # Hooks allow you to customize your code/environment as the project moves through the build and deploy stages
    # More information: https://docs.upsun.com/create-apps/app-reference.html#hooks
    hooks:
      # The build hook is run after any build flavor.
      # More information: https://docs.upsun.com/create-apps/hooks/hooks-comparison.html#build-hook
      build: |
          set -eux
          pip install -r requirements.txt      

    The Upsun CLI automatically added pip install -r requirements.txt to your build hook configuration.
    If you want pip to be upgraded first, add pip install --upgrade pip to the above line.
    Then, add npm install:

    .upsun/config.yaml
    hooks:
        build: |
            set -eux
            pip install --upgrade pip
            pip install -r requirements.txt
            npm install        
  2. Instruct Upsun to automatically run npm run build when building the application container.
    To do so, customize your deploy hook. Locate the section dedicated to it:

    .upsun/config.yaml
    # The deploy hook is run after the app container has been started, but before it has started accepting requests.
    # More information: https://docs.upsun.com/create-apps/hooks/hooks-comparison.html#deploy-hook
    deploy: |
         set -eux
         # echo 'Put your deploy command here'     

    Add npm run build:

    .upsun/config.yaml
    deploy: |
         set -eux
         npm run build     
  3. Configure the web server running in front of your app to define how your app handles dynamic requests. To do so, locate the section dedicated to the web server:

    .upsun/config.yaml
    # The web key configures the web server running in front of your app.
    # More information: https://docs.upsun.com/create-apps/app-reference.html#web
    web:
      # Commands are run once after deployment to start the application process.
      # More information: https://docs.upsun.com/create-apps/app-reference.html#web-commands

    To add a basic Flask server, replace the default information added by the Upsun CLI with flask run -p $PORT.
    Also, change the socket_family value from unix to tcp:

    .upsun/config.yaml
    web:
      commands:
        start: "flask run -p $PORT"
      upstream:
        socket_family: tcp
  4. To commit your changes, run the following commands:

    Terminal
    git add .upsun/config.yaml
    git commit -m "Adds FLASK_APP env var, adds mount for static builds, build commands, npm run build on deploy, web start command"

Is this page helpful?