Deploying Python Flask

Last updated: 2020-09-21 14:52:04

    Overview

    The Tencent Cloud Flask Serverless Component supports deploying RESTful API services but does not support Flask Command.

    Note:

    Any Python server framework that supports the Web Server Gateway Interface (WSGI), such as Falcon, can be deployed through this component.

    Prerequisites

    1. Before using this component, please make sure that you have installed the Python environment locally.
    2. Initialize a Flask project first and then add Flask and werkzeug to the dependent file requirements.txt, as shown below:
      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/")
    def user(id):
    return jsonify(data={'name': 'test1'})

    
    ## Directions
    ### Installation
    Use npm to install [Serverless CLI](https://github.com/serverless/serverless) globally:
    
    ```shell
    npm install -g serverless

    Configuration

    Create a serverless.yml file in the project root directory:

    touch serverless.yml

    Configure serverless.yml as follows:

    # serverless.yml
    
    component: flask
    name: flashDemo
    org: orgDemo
    app: appDemo
    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

    Detailed Configurations >>

    Deployment

    Run the following command to deploy granting permissions via code scanning:

    sls deploy

    Note:

    To grant persistent permissions, please see Account Configuration.

    Removal

    You can run the following command to remove the deployed service:

    sls remove

    Account configuration (optional)

    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 and SecretKey in API Key Management.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help