头图

Serverless Web Function practical tutorial (2): Deploy your Flask project based on Web function

Serverless
中文

Flask is a lightweight web framework based on Python. It implements business logic based on a series of third-party dependency packages, making Flask more flexible to use, and can be customized or expanded through a series of extensions. The two main cores are The modules are the WSGI toolset Werkzeug and the rendering template framework Jinja. Flask's light weight and flexibility make it popular among developers for quickly building a website or web service.

This tutorial will guide you how to quickly deploy your Flask business to the cloud SCF Web Function

01. Template deployment-no need to change business code, one-click deployment

  1. Log in to the Serverless console, click "Function Service" in the left navigation bar, select the region where you want to create a function at the top of the main interface, and click "New" to enter the function creation process.
  2. Select "Template Creation" to create a new function, enter " WebFunc " in the search box, filter all Web function templates, select "Flash Framework Template" , click "Next", as shown below:

  1. On the "Configuration" page, you can view and modify the specific configuration information of the template project;
  2. Click " complete " to create the function. After the function is created, you can view the basic information of the Web function on the "Function Management" page, and access it through the access path URL generated by the API gateway to view your deployed Flash project.

02. Custom deployment-3 steps to quickly migrate local projects to the cloud

1. Local development

  1. First, you need to confirm that Flask has been installed in your local environment
pip install Flask
  1. Create the Hello World sample project locally, and create a new app.py project in the project directory to implement the simplest Hello World application. The sample code is as follows:
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
   return 'Hello World'

if __name__ == '__main__':
   app.run()
  1. Run the app.py file locally and visit http://127.0.0.1:5000 in the browser to complete the access to the Flash sample project locally:
$ 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 -

2. Deploy to the cloud

Next, we make simple modifications to the locally created projects so that they can be quickly deployed through Web Function. For Flask, the specific transformation steps are as follows:

1. Install dependency package

Since there is no Flask dependent library in the standard environment on the SCF cloud, you must install the dependent files and upload them together with the project code. First, create a new requirements.txt file:

#requirements.txt

Flask==1.0.2
werkzeug==0.16.0

Next perform the installation:

pip install -r requirements.txt

2. Modify the listening address and port

In the Web function, the listening port must be limited to 9000, so you need to modify the listening address port to 0.0.0.0:9000

You can also configure the listening port through environment variables scf_bootstrap

3. Add scf_bootstrap startup file

scf_bootstrap startup file in the project root directory, complete the environment variable configuration in it, specify service startup commands and other custom operations to ensure that your service can be started normally through this file

#!/bin/bash
/var/lang/python3/bin/python3 app.py

After creation, pay attention to modify your executable file permissions. By default, 777 or 755 permissions are required

chmod 777 scf_bootstrap

note

  • In the SCF environment, only the /tmp file can be read and written. It is recommended to select /tmp when outputting the file. Other directories will fail to write due to lack of permissions.
  • If you want to output environment variables in the log, you need to add the -u parameter before starting the command, for example: python -u app.py

4. After the local configuration is complete, execute the startup file

Make sure that your service can be started normally locally. Next, log in to the Tencent Cloud Cloud Function Console and create a new Web function to deploy your Flash project.

3. Development Management

After the deployment is complete, you can quickly access and test your web services on the SCF console, and experience multiple features of cloud functions such as layer binding, log management, etc., and enjoy the low cost and flexible expansion and contraction brought by the serverless architecture. Advantage.

Web Function experience

Web Function is currently released in "Chengdu, Beijing, Shanghai" regions, and other regions will be opened one after another, so stay tuned!


One More Thing

Experience Tencent Cloud Serverless Demo now and receive the Serverless new user gift pack Tencent Cloud Serverless newbie experience .

阅读 427

玩转Serverless
我们专注于 Serverless 架构最佳实践,欢迎关注:[链接]

Serverless 中文社区是由一群拥有相同价值观和理念的志愿者们共同发起,于 2019 年 12 月正式成立。

124 声望
13 粉丝
0 条评论
你知道吗?

Serverless 中文社区是由一群拥有相同价值观和理念的志愿者们共同发起,于 2019 年 12 月正式成立。

124 声望
13 粉丝
宣传栏