You can migrate web projects in Python by using the Custom Runtime and HTTP triggers of Function Compute. You can access a function by using a browser or HTTP client tools such as cURL.

Prerequisites

Before you begin, make sure that the following operations are complete:
  • Install Node.js 8.6.0 or later. For more information, visit Downloads.
  • Install Docker. For more information, visit Docker Hub.
Note The examples in this topic apply to scenarios where Docker is installed. If you do not want to use Docker, you must install .NET Core 3.1. For more information about how to install .NET Core 3.1, see dot.net.core3.1. For more information about the commands of .NET Core 3.1, see fc-custom-demo.

Procedure

  1. Run the following command to clone the sample project to the on-premises machine:
    git clone https://github.com/awesome-fc/fc-custom-demo          
    Note If you do not install Git, you can download the sample project from https://github.com/awesome-fc/fc-custom-demo.
  2. Optional:Modify the sample code in the python37-http-demo directory to implement your business logic.
  3. Run the following command to go to the cloned sample project:
    1. Run the following command to go to the python37-http-demo directory:
      cd python37-http-demo
  4. Run the following command to install dependencies:
    fun install -v
    The following output is returned:
    using template: template.yml
    start installing function dependencies without docker
    
    building python37-demo/fc-python37-http
    Funfile exist, Fun will use container to build forcely
    Step 1/3 : FROM registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.6
     ---> a9bade1e4f9c
    Step 2/3 : RUN fun-install pip -i https://pypi.tuna.tsinghua.edu.cn/simple install flask
     ---> Using cache
     ---> c15874870502
    Step 3/3 : RUN fun-install pip -i https://pypi.tuna.tsinghua.edu.cn/simple install gunicorn
     ---> Using cache
     ---> dc1f3f69d494
    sha256:dc1f3f69d494e914e7a6c73b64eec5d4744baca6d976a203a179d3ef2dd9691c
    Successfully built dc1f3f69d494
    Successfully tagged fun-cache-08b1a909-bc37-4a1c-bb4a-4cca273f759f:latest
    copying function artifact to C:\Users\demo\fc-custom-demo\python37-http-demo
    
    Install Success
  5. Run the following command to deploy the project to Function Compute:
    fun deploy -y
    The following output is returned:
    Waiting for service python37-demo to be deployed...
            Waiting for function fc-python37-http to be deployed...
                    Waiting for packaging function fc-python37-http code...
                    The function fc-python37-http has been packaged. A total of 368 files were compressed and the final size was 1.49 MB
                    Waiting for HTTP trigger http_t to be deployed...
                    triggerName: http_t
                    methods: [ 'GET', 'POST' ]
                    trigger http_t deploy success
            function fc-python37-http deploy success
    service python37-demo deploy success
    
    Detect 'DomainName:Auto' of custom domain 'my_domain'
    Fun will reuse the temporary domain http://********-*********.test.functioncompute.com, expired at 2020-12-20 15:18:04, limited by 1000 per day.
    
    Waiting for custom domain my_domain to be deployed...
    custom domain my_domain deploy success
  6. Run the following command to invoke the deployed function:
    curl -v http://********-*********.test.functioncompute.com
    Note
    • http://********-*********.test.functioncompute.com can be obtained from the output of Step 5.
    • If you use the Windows operating system, you must convert the format of the bootstrap file to UNIX.
    The following output is returned:
    * Connected to ********-****************.test.functioncompute.com (47.141.XX.XX) port 80 (#0)
    > GET / HTTP/1.1
    > Host: ********-****************.test.functioncompute.com
    > User-Agent: curl/7.64.1
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Access-Control-Expose-Headers: Date,x-fc-request-id,x-fc-error-type,x-fc-code-checksum,x-fc-invocation-duration,x-fc-max-memory-usage,x-fc-log-result,x-fc-invocation-code-version
    < Content-Length: 12
    < Content-Type: text/html; charset=utf-8
    < X-Fc-Code-Checksum: 6465955828********0
    < X-Fc-Invocation-Duration: 3
    < X-Fc-Invocation-Service-Version: LATEST
    < X-Fc-Max-Memory-Usage: 85.20
    < X-Fc-Request-Id: 1a26ecd3-3a6b-4b1c-9c33-********c7ec
    < Date: Thu, 10 Dec 2020 07:23:21 GMT
    <
    * Connection #0 to host ********-****************.test.functioncompute.com left intact
    Hello World!* Closing connection 0