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.
Note: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
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 app.py
file locally. Visit http://127.0.0.1:5000
in a browser, and you can access the sample Express project locally.
$ python3 app.py
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 requirements.txt
file:
pip install -r requirements.txt
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.
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 0.0.0.0:9000
.
Note:You can also configure the listening port through the environment variable in
scf_bootstrap
.
3. (Optional) Configure the scf_bootstrap
file.
Create 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 777
or 755
is required for the service to start normally.
chmod 777 scf_bootstrap
Note:
- You can also complete the configuration in the console.
- 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
.
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.
Note:The actual
scf_bootstrap
file in your project prevails. If thescf_bootstrap
file 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.
Was this page helpful?