This document describes how to quickly deploy a Flask business to the cloud through an SCF HTTP-triggered Function.
Explanation
This document mainly describes how to deploy in the console. You can also complete the deployment on the command line. For more information, please see Deploying Framework on Command Line.
Template Deployment: One-click deployment of Flask projects
1. Log in to the Serverless console and click on Function Service in the left navigation bar.
2. Select the region where to create a function at the top of the page and click Create to enter the function creation process.
3. Choose to create a new function using Template Creation. Enter WebFunc in the search box to filter all HTTP-triggered Function templates, select the Flask Framework Template and click Next. As shown below:
4. On the Create page, you can view and modify the specific configuration information of the template project.
5. Click Complete. After creating the HTTP-triggered Function, you can view its basic information on the Function Management page.
6. You can access your deployed Flask project through the automatically generated access path URL. Click on Trigger Management in the left menu bar to view the access path. As shown below:
7. Click the access path URL to access the Flask project.
Custom deployment: Quick migration of local project to cloud
Local development
1. Run the following command to confirm that Flask has been installed in your local environment.
pip install Flask
2. Create the Hello World sample project locally.
In the project directory, create a new app.py file to implement the Hello World application. The sample code is as follows:
from flask import Flask
app = Flask(__name__)
@app.route('/')
defhello_world():
return'Hello World'
if __name__ =='__main__':
app.run()
3. Execute the python3 app.py command locally to run the app.py file. The example is as follows:
$ python3 app.py
* Serving Flask app "app"(lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/(Press CTRL+C to quit)
4. Visit http://127.0.0.1:5000 in a browser, and you can access the sample Flask project locally. As shown in the figure below:
Deployment in cloud
Next, perform the following steps to make simple modifications to the locally created project, so that it can be quickly deployed through a HTTP-triggered Function. The steps of project transformation for Flask are as follows:
1. Install dependencies
1.1 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 with the following content:
#requirements.txt
Flask==1.0.2
werkzeug==0.16.0
Note
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.
1.2 Run the following installation command:
pip install -r requirements.txt
2. Modify the listening address and port
Within the HTTP-triggered Function, the listening port is restricted to 9000. Therefore, the listening address port needs to be changed to 0.0.0.0:9000, as shown in the following figure:
Explanation
You can also configure the listening port through the environment variable in scf_bootstrap.
3. Add a new scf_bootstrap startup file.
3.1 Create the scf_bootstrap bootstrap file in the project root directory and add the following content to it (which is used to configure environment variables, specify service bootstrap commands, and make sure that your service can be started normally through this file):
#!/bin/bash
/var/lang/python3/bin/python3 app.py
3.2 After the file is created, you need to run the following command to modify the executable permission of the file. By default, the permission 777 or 755 is required for the service to start normally. Below is the sample code:
chmod777 scf_bootstrap
Note
In the SCF environment, only files in the /tmp directory are readable/writable. We recommend you select /tmp when outputting files. If you select other directories, write will fail due to the lack of permissions.
If you want to output environment variables in logs, you need to add the -u parameter before the bootstrap command, such as python -u app.py.
4. After the local configuration is completed, run the following command to start the service (take running the command in the scf_bootstrap directory as an example) and make sure that your service can be normally started locally.
./scf_bootstrap
5. Log in to the Serverless console and click on Function Service in the left navigation bar.
6. Select the region where to create a function at the top of the page and click Create to enter the function creation process.
7. Choose to create a function from scratch and configure the relevant options according to the page prompts. As shown in the figure below:
Function Type: Select "HTTP-triggered Function".
Function name: Enter the name of your function.
Region: enter your function deployment region, such as Chengdu.
Deployment Method: Select "Code Deployment" and upload your local project.
Runtime Environment: Select "Python3.6".
8. Click Complete.
Development management
Upon completion of the deployment, you can quickly access and test your web service via the SCF console. You can also experience various unique features of cloud functions, such as layer binding, log management, and enjoy the benefits of a Serverless architecture, such as low cost and elastic scaling. As shown in the figure below: