Tencent Cloud Flask Serverless Component supports deploying RESTful API services but does not support Flask Command.
Note:
Any Python server framework that supports Web Server Gateway Interface (WSGI) can be deployed through this component, such as Falcon.
Flask
and werkzeug
to the dependent file requirements.txt
as follows: Flask==1.0.2
werkzeug==0.16.0
Meanwhile, add the API service app.py
. The following code is for reference only:from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/")
def index():
return "Hello Flask"
@app.route("/users")
def users():
users = [{'name': 'test1'}, {'name': 'test2'}]
return jsonify(data=users)
@app.route("/users/<id>")
def user(id):
return jsonify(data={'name': 'test1'})
Note:
The following steps are mainly for deployment on the command line. For deployment in the console, please see Console Deployment Guide.
Use npm to install Serverless CLI globally:
npm install -g serverless
If you don't have a local Flask project, you can initialize a Flask project with the following commands (if you already have one, you can ignore this step):
serverless init flask-starter --name example
cd example
Create a serverless.yml
file in the project root directory and paste the following configuration template into it to implement basic project configuration.
Note:
You can add more configuration items in
serverless.yml
based on your actual deployment needs. For more information on the configuration of the .yml file, please see Flask Component Configuration.
touch serverless.yml
#serverless.yml
component: flask
name: flashDemo
stage: dev
inputs:
src:
hook: 'pip install -r requirements.txt -t ./'
dist: ./
exclude:
- .env
region: ap-guangzhou
runtime: Python3.6
apigatewayConf:
protocols:
- http
- https
environment: release
Deploy by running the sls deploy
command, and you can add the --debug
parameter to view the information during the deployment process:
sls deploy --debug
After the deployment is completed, access the application by accessing the output API Gateway link.
After the deployment is completed, you can log in to the Serverless Framework console to view the basic information of the application and monitor logs.
Currently, you can scan a QR code to log in to the CLI by default. If you want to configure persistent environment variables/key information, you can also create a local .env
file:
touch .env # Tencent Cloud configuration information
Configure Tencent Cloud's SecretId
and SecretKey
information in the .env
file and save it:
# .env
TENCENT_SECRET_ID=123
TENCENT_SECRET_KEY=123
Note:
- If you don't have a Tencent Cloud account yet, please sign up first.
- If you already have a Tencent Cloud account, you can get
SecretId
andSecretKey
in API Key Management.
Was this page helpful?