Configure your infrastructure
Back to home
On this page
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
-
To generate the configuration files needed to deploy your app on Upsun, run the following command:
Terminalupsun project:initThis command is also available as
upsun ify. -
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.
-
-
Add all the generated files to your Git repository. To do so, run the following commands:
Terminalgit add . git commit -m "Initial commit"
Create your Upsun project
To deploy your Flask project on Upsun, you need to create and configure a Upsun project first.
-
To create your Upsun project, run the following command:
Terminalupsun project:create -
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
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:
-
Specify how your app must be loaded. To do so, add a
FLASK_APPvariable for all your environments and point it to yourautoapp.pyfile. 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:andenv:lines, and add your environment variable:.upsun/config.yamlvariables: env: FLASK_APP: autoapp.pyNote
When you uncomment a section in a YAML file, remove both the comment marker
#and the extra space. Failure to do so results in anInvalid block mapping key indenterror when the configuration file is validated. -
Configure some writable disk space to hold the static assets that
flask-static-digestgenerates andnpmbuilds. To do so, define the./<APP_NAME>/staticdirectory 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.yamlmounts: "<APP_NAME>/static": source: "storage" source_path: "static_assets"
Replacing <APP_NAME> above with the app_name you defined previously.
-
Instruct Upsun to automatically run
npm installwhen 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.txtThe Upsun CLI automatically added
pip install -r requirements.txtto your build hook configuration. If you wantpipto be upgraded first, addpip install --upgrade pipto the above line. Then, addnpm install:.upsun/config.yamlhooks: build: | set -eux pip install --upgrade pip pip install -r requirements.txt npm install -
Instruct Upsun to automatically run
npm run buildwhen 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.yamldeploy: | set -eux npm run build -
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-commandsTo add a basic Flask server, replace the default information added by the Upsun CLI with
flask run -p $PORT. Also, change thesocket_familyvalue fromunixtotcp:.upsun/config.yamlweb: commands: start: "flask run -p $PORT" upstream: socket_family: tcp -
To commit your changes, run the following commands:
Terminalgit 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"Note
By default, the
upsun project:initcommand sets the language runtime to the latest possible version.If your project requires an older version, before pushing your code to Upsun, open your
.upsun/config.yamlfile and change the value of thetypekey (near the top of the file) to the desired version. See the complete list of supported Python versions.Make sure you add and commit your changes to Git before pushing your code.