通过函数计算的Custom Runtime结合HTTP触发器的方式,您可以将Python 3.7的Web项目一键迁移至云上,并可直接使用浏览器或cURL等HTTP客户端工具访问函数。您可以按需修改Python 3.7的示例代码实现二次开发。

前提条件

您已完成以下操作:

操作步骤

  1. 在目标目录中,执行以下命令,初始化项目。
    s init start-fc-custom-samples -d start-fc-custom-samples
  2. 执行以下命令,进入项目目录。
    cd start-fc-custom-samples/python37/http-demo
    说明 本文介绍的示例支持单实例多并发。
  3. 执行以下命令,安装依赖。
    s build -d
    输出示例:
    [2021-09-09T10:41:36.043] [INFO ] [S-CLI] - Start ...
    [2021-09-09T10:41:36.829] [INFO ] [FC-BUILD] - Build artifact start...
    [2021-09-09T10:41:36.848] [INFO ] [FC-BUILD] - Use docker for building.
    [2021-09-09T10:41:36.959] [INFO ] [FC-BUILD] - Build function using image: registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.19
    [2021-09-09T10:41:37.045] [INFO ] [FC-BUILD] - skip pulling image registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.19...
    [2021-09-09T10:42:05.697] [INFO ] [FC-BUILD] - Build artifact successfully.
    
    Tips for next step
    ======================
    * Invoke Event Function: s local invoke
    * Invoke Http Function: s local start
    * Deploy Resources: s deploy
    End of method: build
  4. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [2021-09-09T10:43:24.748] [INFO ] [S-CLI] - Start ...
    [2021-09-09T10:43:25.323] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-09-09T10:43:25.324] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-09T10:43:25.324] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-09-09T10:43:25.325] [INFO ] [FC-DEPLOY] - Using accessKeySecret: LTAI4G4cwJkK4Rza6xd9****
      Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    [2021-09-09T10:43:25.591] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T10:43:25.803] [INFO ] [FC-DEPLOY] - Checking Function pyHttpFunc exists
    [2021-09-09T10:43:25.880] [INFO ] [FC-DEPLOY] - Fc detects that you have run build command for function: pyHttpFunc.
    [2021-09-09T10:43:25.880] [INFO ] [FC-DEPLOY] - Using codeUri: /test/start-fc-custom-samples/python37/http-demo/.s/build/artifacts/custom-demo/pyHttpFunc
    [2021-09-09T10:43:25.881] [INFO ] [FC-DEPLOY] - Fc add/append some content to your origin environment variables for finding dependencies generated by build command.
    {
      "LD_LIBRARY_PATH": "/code/.s/root/usr/local/lib:/code/.s/root/usr/lib:/code/.s/root/usr/lib/x86_64-linux-gnu:/code/.s/root/usr/lib64:/code/.s/root/lib:/code/.s/root/lib/x86_64-linux-gnu:/code/.s/root/python/lib/python2.7/site-packages:/code/.s/root/python/lib/python3.6/site-packages:/code:/code/lib:/usr/local/lib",
      "PATH": "/code/.s/root/usr/local/bin:/code/.s/root/usr/local/sbin:/code/.s/root/usr/bin:/code/.s/root/usr/sbin:/code/.s/root/sbin:/code/.s/root/bin:/code:/code/node_modules/.bin:/code/.s/python/bin:/code/.s/node_modules/.bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin:/bin",
      "NODE_PATH": "/code/node_modules:/usr/local/lib/node_modules",
      "PYTHONUSERBASE": "/code/.s/python"
    }
    [2021-09-09T10:43:27.571] [INFO ] [FC-DEPLOY] - Checking Trigger http_t exists
    [2021-09-09T10:43:27.618] [INFO ] [FC-DEPLOY] - Checking Trigger http_t exists
      Make service custom-demo success.
      Make function custom-demo/pyHttpFunc success.
      Make trigger custom-demo/pyHttpFunc/http_t success.
    [2021-09-09T10:43:45.462] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T10:43:45.521] [INFO ] [FC-DEPLOY] - Checking Function pyHttpFunc exists
    [2021-09-09T10:43:45.593] [INFO ] [FC-DEPLOY] - Checking Trigger http_t exists
    [2021-09-09T10:43:45.717] [INFO ] [FC-DEPLOY] - Using customDomain: auto: fc will try to generate related custom domain resources automatically
      End of request
      Deployed.
      End of request
    [2021-09-09T10:43:50.418] [INFO ] [FC-DEPLOY] - Generated auto custom domain: pyhttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
    [2021-09-09T10:43:50.418] [INFO ] [FC-DEPLOY] - Creating custom domain: pyhttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
    [2021-09-09T10:43:50.581] [INFO ] [FC-DOMAIN] - Creating custom domain: pyhttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
    
    Tips for next step
    ======================
    * Display information of the deployed resource: s info
    * Display metrics: s metrics
    * Display logs: s logs
    * Invoke remote function: s invoke
    * Remove Service: s remove service
    * Remove Function: s remove function
    * Remove Trigger: s remove trigger
    * Remove CustomDomain: s remove domain
    
    
    
    fc-python37-demo:
      region: cn-hangzhou
      service:
        name: custom-demo
      function:
        name: pyHttpFunc
        runtime: custom
        handler: index.handler
        memorySize: 1536
        timeout: 30
      url:
        system_url: >-
          https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/custom-demo/pyHttpFunc/
        custom_domain:
          - domain: >-
              http://pyhttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
      triggers:
        - type: http
          name: http_t

执行结果

成功部署该项目后,您可以在执行输出中查看到函数计算生成的临时域名,通过该临时域名可以访问刚部署的应用,例如执行以下命令访问应用:

curl http://pyhttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
说明 临时域名仅用作演示以及开发,具有时效性。如需用作生产,请绑定已经在阿里云备案的域名。详细信息,请参见绑定自定义域名