Skip to main content

Python

Getting an application running on Boltic is essentially working out how to package it as a deployable image. Once packaged, it can be deployed to the Boltic global application platform.

In this guide we’ll learn how to deploy a Python application on Boltic.

Note

Before starting this guide, ensure you have a Boltic account and have created a serverless app in your account.

Initial Local Setup

Make sure that Python is already installed on your computer along with a way to create virtual environments.

This allows you to run your project locally, and test that it works, before deploying it to Boltic.

tip

We recommend the latest supported versions of Python.

Virtual Environment

For this guide, we use venv but any of the other popular choices such as Poetry, Pipenv, or pyenv work too.

# Create a new project directory
mkdir my-new-app

# Navigate to the project directory
cd my-new-app

# Create a virtual environment
python -m venv .venv

# Activate the virtual environment
source .venv/bin/activate

Run a Flask App

In this guide we recreate and deploy this minimal Flask application to demonstrate how quickly Flask apps can be deployed to Boltic!

tip

You can follow the guide to recreate the app or just git clone https://github.com/bolticio/serverless-samples.git to get a local copy.

Make sure to navigate to appropriate directory using: cd serverless-samples/python/applications/docker/hello-world

We assume you already have Python installed and your virtual environment is activated.

Install Flask

With your virtual environment activated, install the latest version of Flask using pip:

python -m pip install Flask

This will load Flask and all its dependency packages. You can check all of the packages with pip freeze command:

pip freeze
blinker==1.6.2
click==8.1.3
Flask==2.3.2
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.2
Werkzeug==2.3.3
tip

If you have a local copy of hello-world, you can install all the dependencies using python -m pip install -r requirements.txt

Create the Flask App

The hello-world application is, as you’d expect for an example, very minimal.

Create a new file app.py and add the following code:

# app.py
import os
from flask import Flask, request, jsonify

# Get the port from the environment variable, or use 8080 as default
PORT = int(os.environ.get('PORT', 8080))

app = Flask(__name__)

@app.route('/')
@app.route('/<name>')
def hello(name="World"):
response_body = {
'message': f'Hello, {name.capitalize()}!',
}
return jsonify(response_body)

if __name__ == '__main__':
app.run(port=PORT)

Start the Development Server

Flask apps are run with the flask run command:

flask run
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:8080
Press CTRL+C to quit

or

flask --app app run
 * Serving Flask app 'app'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:8080
Press CTRL+C to quit

Note that flask run command works since our file is named app.py. It also works if your file is named wsgi.py, so you don’t have to use --app to tell Flask where your app is. More details here.

If your file is saved as hello.py instead of app.py, you would need to use the --app option to point to Flask where your app is:

flask --app hello run
 * Serving Flask app 'hello'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:8080
Press CTRL+C to quit

If you open http://127.0.0.1:8080/ in your web browser, it displays Hello, World!.

If you open, for example, http://127.0.0.1:8080/fynd , it displays Hello, Fynd!.

Before Deployment

A few more steps are necessary before deploying the app.

For production, the web application will be served by gunicorn. To install it, run:

python -m pip install gunicorn

And now, save all the dependencies to a requirements.txt file using:

pip freeze > requirements.txt

In order to deploy the app to Boltic, you need to create a boltic.yaml/boltic.toml file in the root of your project. This file contains the configuration for the deployment. Read more about the Application Configuration for more information.

app: python-docker-app-sample
region: asia-south1
entrypoint: "app.py"

build:
builtin: dockerfile
ignorefile: .gitignore

env:
PORT: '8080'

The system will always refer to this file in the current directory if it exists, specifically for the app name/value at the start. That name will be used to identify the application to the Boltic service. The rest of the file contains settings to be applied to the application when it deploys:

  • primary_region: it configures where the primary region is, used to create the application;
  • builtin: it configures the builder to use the built-in Dockerfile strategy, which uses a system generated generic Dockerfile to build the application;
  • env: it configures the environment variables to set in the application.
info

This application is being built using the builtin strategy. This strategy uses a system-generated generic Dockerfile to build the application. The ignorefile key is used to specify the name of the file that contains the list of files and directories to be ignored during the build process. The ignorefile key is optional and if not provided, the default value is .dockerignore. For more information on the available builders in Boltic, check out the Builders page.

If you would rather like to use buildpacks to build your application, you can use the following configuration:

app: python-docker-app-sample
region: asia-south1
entrypoint: app.py

build:
builtin: dockerfile
ignorefile: .gitignore

env:
PORT: '8080'
info

This application will now be built using the paketo-buildpacks/python buildpack. Learn more about Paketo Buildpacks.

Now, let’s move on to deploying this app to Boltic.

Deploy to Boltic

We are now ready to deploy our app to Boltic. Ensure you have a Boltic account and have created a serverless app in your account.

tip

In order to clone the repository created for your app after registering it on the console, make sure you have added an SSH key to your Boltic account. You can find the instructions on how to add an SSH key in the SSH Keys section.

Initialize a new git repository in your project:

git init --initial-branch=main

Add a new origin to your local git repository:

git remote add boltic GIT_URL_DISPLAYED_IN_THE_CONSOLE

Commit your changes and push them to the bolt remote:

git add .
git commit -m "serverless initialized"
git push bolt main

The deployment process will start automatically. You can check the status of the deployment in the Boltic console. If everything goes well, the deployment will finish successfully in a few minutes. You can also check the logs to see the if there are any errors. Once the deployment is finished, you can access your app through the URL provided in the Boltic console.

info

The URL will be in the format https://<region>.api.boltic.io/apps/<system-version>/<org-id>/<app-id>.

Run a Function

In this guide we recreate and deploy this sample python function to demonstrate how easy it is to setup serverless Python functions on Boltic!

tip

You can follow the guide to recreate the app or just git clone https://github.com/bolticio/serverless-samples.git to get a local copy.

Make sure to navigate to appropriate directory using: cd serverless-samples/python/functions/docker/hello-world

note

Currently there is not native way to test serverless functions locally. You can test the function by directly deploying it to Boltic.

We assume you already have Python installed and your virtual environment is activated.

Create the Function

The hello-world application is, as you’d expect for an example, very minimal.

Create a new file index.py and add the following code:

# index.py
# Import the required modules
from flask import jsonify

# Define the handler function
def handler(request):
# Create the response JSON
response_json = {
'message': 'Hello World'
}

# Return a JSON response
return jsonify(response_json)

Before Deployment

A few more steps are necessary before deploying the function.

Save all the dependencies to a requirements.txt file using:

pip freeze > requirements.txt

As opposed to the Flask app, we will not be using a Procfile for the function. The handler will be the entry point for the function. The handler is the function that will be called when the function is invoked. The handler should be in the format module_name.function_name. The module_name is the name of the file where the function is defined and the function_name is the name of the function that will be called. Checkout handler for now information

app: python-buildpack-func-sample
region: asia-south1
handler: "index.handler"

build:
builtin: dockerfile
ignorefile: .gitignore

Testing the function locally

To test any boltic serverless function locally please refer boltctl serverless test command.

Deploy to Boltic

Deployment of the function is similar to deploying an application. You can deploy the function by following the same steps as mentioned in the Deploy to Boltic section.

Congrats! You have successfully built, deployed, and connected to your first Python application/function on Boltic. 🚀