This document describes how to quickly deploy a Flask business to the cloud through an SCF web function.
Note: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.
Before using SCF, you need to sign up for a Tencent Cloud account and complete identity verification first.
WebFunc
in the search box to filter all web function templates, select Flask Framework Template, and click Next as shown below:Run the following command to confirm that Flask has been installed in your local environment.
pip install Flask
Create the 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__)
@app.route('/')
def hello_world():
return 'Hello World'
if __name__ == '__main__':
app.run()
Run the python3 app.py
command locally to start the app.py
file. Below is the sample code:
$ 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)
127.0.0.1 - - [22/Jun/2021 09:41:04] "GET / HTTP/1.1" 200 -
Visit http://127.0.0.1:5000
in a browser, and you can access the sample Flask project locally as shown below:
Next, perform the following steps to make simple modifications to the locally created project, so that it can be quickly deployed through a web function. The steps of project transformation for Flask are as follows:
Install dependencies
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
Run the following command to install:
pip install -r requirements.txt
Modify the listening address and port
The listening port in the web function must be 9000, so you need to change the listening address and port to 0.0.0.0:9000
as shown below:
Note:You can also configure the listening port through the environment variable in
scf_bootstrap
.
Add the scf_bootstrap
bootstrap file
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
777
or 755
is required for it to start normally. Below is the sample code:chmod 777 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 the log, you need to add the
-u
parameter before the bootstrap command, such aspython -u app.py
.
scf_bootstrap
directory as an example) and make sure that your service can be normally started locally../scf_bootstrap
After the deployment is completed, you can quickly access and test your web service in the SCF console and try out various features of SCF, such as layer binding and log management. In this way, you can enjoy the advantages of low cost and flexible scaling brought by the serverless architecture.
Was this page helpful?