The SLS framework deployment scheme has been upgraded. You can use an SCF HTTP-triggered function to quickly deploy your Flask service to the cloud.
What are the differences between SLS console deployment and direct function deployment?
Both SLS console deployment and function deployment can be based on HTTP-triggered functions, and quick deployment is usually used for web frameworks.
This document introduces the SLS console deployment scheme. You can also complete the deployment in CLI by referring to Deploying Web Function on Command Line.
Confirm that Flask has been installed in your local environment.
pip install Flask
Hello World sample project locally.
In the project directory, create the
app.py file to implement the Hello World application. Below is the sample code:
from flask import Flask app = Flask(__name__) def hello_world(): return 'Hello World' if __name__ == '__main__': app.run()
app.py file locally. Visit
http://127.0.0.1:5000 in a browser, and you can access the sample Express project locally.
127.0.0.1 - - [22/Jun/2021 09:41:04] "GET / HTTP/1.1" 200 -
Next, perform the following steps to make simple modifications to the locally created project, so that it can be quickly deployed through an HTTP-triggered function. The steps of project transformation for Flask are as follows:
1. Install the dependency package
As the Flask dependency library is not provided in the standard cloud environment of SCF, you must install the dependencies and upload them together with the project code. Please create the
requirements.txt file first:
#requirements.txt Flask==1.0.2 werkzeug==0.16.0
Then install the
pip install -r requirements.txt
Due to the limitation of SCF's built-in runtime environment version (Python 3.6), only lower versions (1.0.x or earlier) of Werkzeug can be used, while higher versions may not work. The runtime environment version upgrade has been planned. Please stay tuned.
2. Modify the listening address and port
The listening port in the HTTP-triggered function must be
9000, so you need to change the listening address and port to
You can also configure the listening port through the environment variable in
3. (Optional) Configure the
scf_bootstrap file in the root directory of the project. This file is used to configure environment variables, specify service start commands, and make sure that your service can be started normally through this file.
!/bin/bash /var/lang/python3/bin/python3 app.py
After the file is created, you need to run the following command to modify the executable permission of the file. By default, the permission
755 is required for the service to start normally.
chmod 777 scf_bootstrap
- You can also complete the configuration in the console.
- In the SCF environment, only files in the
/tmpdirectory are readable/writable. We recommend you select
/tmpwhen outputting files. If you select other directories, write will fail due to the lack of permissions.
- If you want to output environment variables in the log, you need to add the
-uparameter before the bootstrap command, such as
python -u app.py.
4. Console upload
Log in to the SLS console, select Web Application > Flask Framework, and select Local Upload or Code Repository Pull as the upload mode.
You can configure the
scf_bootstrap file in the console. When the configuration is completed, the console automatically generates the
scf_bootstrap file and packages it and the project code for deployment.
scf_bootstrapfile in your project prevails. If the
scf_bootstrapfile already exists in your project, its content will not be overwritten.
When the configuration is completed, click Complete to deploy your Flask project.
In Advanced Configuration, you can perform more application management operations, such as creating layers, binding custom domains, and configuring environment variables.