tencent cloud

Feedback

Quickly Deploying Flask Framework

Last updated: 2024-12-02 11:16:20
    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.
    If you only need to develop code logic and do not need to create additional resources, you can perform quick deployment through the SCF console.
    If you need to create more capabilities or resources, such as automatic creation of layer hosting dependencies, quick implementation of static resource isolation, and support for direct code repository pulling, in addition to code deployment, you can use the SLS console to create web applications.
    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.

    Template Deployment - Deploying Flask Sample Code

    1. Log in to the SLS console.
    2. Choose Create Application and select Web Application > Flask Framework.
    3. Click Next and complete basic configuration.
    4. Select Sample Code as the upload mode and click Complete. The application deployment starts.
    5. After the application deployment is completed, you can view the basic information of the sample application on the application details page. In addition, you can use access the deployed Flask project at the access path URL generated by API Gateway.

    Custom Deployment - Quickly Deploying Web Application

    Local development

    1. Confirm that Flask has been installed in your local environment.
    pip install Flask
    2. 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()
    3. 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
    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 -
    
    

    Deployment in cloud

    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 as python -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 the scf_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.

    Advanced configuration management

    In Advanced Configuration, you can perform more application management operations, such as creating layers, binding custom domains, and configuring environment variables.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support