本文介绍如何使用FC组件的相关命令创建部署一个Hello World函数的应用。

前提条件

您已完成以下操作:

操作步骤

  1. 初始化项目。
    在目标文件内执行以下命令,初始化项目:
    s init node.js12-http -d fc-hello-world-demo
    参数说明:
    • node.js12-http表示函数为HTTP函数且函数的运行环境为node.js12。
    • -d指定项目名称,该示例中项目名称为fc-hello-world-demo
    执行输出:
    √ File decompression completed
    ? Do you want to install dependencies? No//在执行的过程中选择不安装依赖。
    ? please select credential alias default//选择密钥别名。
    
     Thanks for using Serverless-Devs
     You could [cd C:\Users\ty\Desktop\ddd\jy\fc-hello-world-demo] and enjoy your serverless journey!
     If you need help for this example, you can use [s -h] after you enter folder.
     Document Star:https://github.com/Serverless-Devs/Serverless-Devs
    当成功初始化项目后,会在当前目录中生成一个fc-hello-world-demo文件,该文件内包含以下文件:
    • s.yaml:函数计算资源,该资源是以YAML规范定义的。关于YAML规范的详细信息,请参见YAML规范
    • index.js:函数代码逻辑。
    • package.json:依赖清单文件,该文件内定义了项目所需的各个模块和项目的配置信息。
  2. 执行以下命令,进入项目目录内:
    cd fc-hello-world-demo
  3. 构建应用。
    在项目目录内执行以下命令,构建应用:
    s build
    执行输出:
    [2021-09-26T17:38:11.710] [INFO ] [S-CLI] - Start ...
    [2021-09-26T17:38:12.363] [INFO ] [FC-BUILD] - Build artifact start...
    builder begin to build, runtime is: nodejs10, sourceDir:  /test/jjy/fc-hello-world-demo/
    running task: flow NpmTaskFlow
    running task: CopySource
    running task: NpmInstall
    added 1 package in 447ms
    [2021-09-26T17:38:13.480] [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
    执行成功该步骤后,会在当前目录中生成一个.s文件,存放输出的构建产物,该文件可用于本地调试和部署。.s文件内生成的node_modules文件用于存放依赖文件。
  4. 可选:本地调试应用。
    当您在本地完成应用的开发构建后,可能需要在本地调试验证功能是否正常。您可以按需选择命令调试函数:
    • HTTP函数
      s local start
      执行输出:
      [2021-09-26T17:38:47.101] [INFO ] [S-CLI] - Start ...
      []
      [2021-09-26T17:38:49.008] [INFO ] [FC-LOCAL-INVOKE] - Using build codeUri: /test/jjy/fc-hello-world-demo/.s/build/artifacts/fc-deploy-service/http-trigger-function
      [2021-09-26T17:38:49.022] [INFO ] [FC-LOCAL-INVOKE] - Using trigger for start: name: httpTrigger
      type: http
      config:
        authType: anonymous
        methods:
          - GET
      
      [2021-09-26T17:38:49.043] [INFO ] [FC-LOCAL-INVOKE] - HttpTrigger httpTrigger of fc-deploy-service/http-trigger-function was registered
              url: http://localhost:7275/2016-08-15/proxy/fc-deploy-service/http-trigger-function/
              methods: GET
              authType: anonymous
      
      Tips for next step
      ======================
      * Deploy Resources: s deploy
      fc-deploy-test:
        status: succeed
      function compute app listening on port 7275!
    • 事件函数
      s local invoke
    当您执行调试命令后,由于函数类型不同返回结果也会不同:
    • HTTP函数
      当您的函数是HTTP函数时,执行完调试命令后,将在执行输出中返回一个URL地址,此时调试流程阻塞在这里,您可以使用以下方式触发函数执行:
      • 使用cURL工具触发函数执行
        curl http://localhost:8000/2016-08-15/proxy/fc-deploy-service/http-trigger-function/
      • 使用浏览器访问HTTP函数
        http://localhost:8000/2016-08-15/proxy/fc-deploy-service/http-trigger-function/
    • 事件函数

      如果您的函数是事件函数,执行完调试命令后,将立即返回调试结果。

  5. 部署应用。
    执行以下命令,将应用部署至函数计算:
    s deploy
    执行输出:
    [2021-09-26T17:41:02.559] [INFO ] [S-CLI] - Start ...
    [2021-09-26T17:41:03.518] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-09-26T17:41:03.519] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-26T17:41:03.519] [INFO ] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9****
    [2021-09-26T17:41:03.519] [INFO ] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN****
      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-26T17:41:04.047] [INFO ] [FC-DEPLOY] - Checking Service fc-deploy-service exists
    [2021-09-26T17:41:04.264] [INFO ] [FC-DEPLOY] - Checking Function http-trigger-function exists
    [2021-09-26T17:41:04.419] [INFO ] [FC-DEPLOY] - Fc detects that you have run build command for function: http-trigger-function.
    [2021-09-26T17:41:04.420] [INFO ] [FC-DEPLOY] - Using codeUri: /test/jjy/fc-hello-world-demo/.s/build/artifacts/fc-deploy-service/http-trigger-function
    
    
    Detail:
    
    added:
      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
    deleted: {}
    updated: {}
    
    //在部署的过程中,为了可以正确找到构建时安装的依赖,FC组件给您的环境变量增加了一些内容,请按需设置。您也可以在执行时指定--assume参数为yes,默认添加。
    ? Fc want to add/append some content to your origin environment variables for finding dependencies generated by build command.
    Do you agree with the behavior? yes
    [2021-09-26T17:41:23.662] [INFO ] [FC-DEPLOY] - Checking Trigger httpTrigger exists
    [2021-09-26T17:41:23.722] [INFO ] [FC-DEPLOY] - Checking Trigger httpTrigger exists
     Make service fc-deploy-service success.
     Make function fc-deploy-service/http-trigger-function success.
     Make trigger fc-deploy-service/http-trigger-function/httpTrigger success.
    [2021-09-26T17:41:24.902] [INFO ] [FC-DEPLOY] - Checking Service fc-deploy-service exists
    [2021-09-26T17:41:25.113] [INFO ] [FC-DEPLOY] - Checking Function http-trigger-function exists
    [2021-09-26T17:41:25.423] [INFO ] [FC-DEPLOY] - Checking Trigger httpTrigger exists
    
    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-deploy-test:
      region:   cn-hangzhou
      service:
        name: fc-deploy-service
      function:
        name:       http-trigger-function
        runtime:    nodejs10
        handler:    index.handler
        memorySize: 128
        timeout:    60
      url:
        system_url: https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/fc-deploy-service/http-trigger-function/
      triggers:
        -
          type: http
          name: httpTrigger
    执行成功后,该命令会将应用部署至函数计算。
  6. 可选:远程调试应用。
    执行以下命令,远程调试应用:
    s invoke
    执行输出:
    [2021-09-26T17:47:25.026] [INFO ] [S-CLI] - Start ...
    https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/fc-deploy-service/http-trigger-function/
    ========= FC invoke Logs begin =========
    FC Initialize Start RequestId: cfa09ba1-acc8-4eeb-a4a0-c34b152639bf
    load code for handler:index.initializer
    2021-09-26T09:47:26.649Z cfa09ba1-acc8-4eeb-a4a0-c34b152639bf [verbose] i am initializing
    FC Initialize End RequestId: cfa09ba1-acc8-4eeb-a4a0-c34b152639bf
    FC Invoke Start RequestId: cfa09ba1-acc8-4eeb-a4a0-c34b152639bf
    load code for handler:index.handler
    2021-09-26T09:47:26.664Z cfa09ba1-acc8-4eeb-a4a0-c34b152639bf [verbose] hello world
    FC Invoke End RequestId: cfa09ba1-acc8-4eeb-a4a0-c34b152639bf
    
    Duration: 7.53 ms, Billed Duration: 8 ms, Memory Size: 128 MB, Max Memory Used: 51.47 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    {
        "message": "Hello World",
        "path": "/",
        "queries": {},
        "headers": {
            "accept": "application/json",
            "authorization": "FC LTAI4G4cwJkK4Rza6xd9****:N5VaTmhWbNRR9kLqx0dCse8xXBq6C/2ok7vaQUCc4pI=",
            "date": "Sun, 26 Sep 2021 09:47:26 GMT",
            "host": "188077086902****.cn-hangzhou.fc.aliyuncs.com",
            "user-agent": "Node.js(v14.17.4) OS(linux/x64) SDK(@alicloud/fc2@v2.2.2)",
            "x-forwarded-proto": "http"
        },
        "method": "GET",
        "requestURI": "/2016-08-15/proxy/fc-deploy-service/http-trigger-function/",
        "clientIP": "47.97.212.57",
        "uuid": "080a2216-8ac8-4194-96db-9ddbb14711f5",
        "body": ""
    }
    
    
    End of method: invoke
    由于本示例部署的函数是HTTP函数,所以在成功执行远程调试命令后,执行输出会返回一个URL,您可以使用cURL工具触发执行线上的函数:
    curl https://xxx.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/fc-deploy-service/http-trigger-function/
  7. 可选:删除应用。
    执行以下命令删除应用:
    s remove service
    执行输出:
    [2021-09-26T17:49:33.530] [INFO ] [S-CLI] - Start ...
    [2021-09-26T17:49:33.751] [INFO ] [FC] - Getting list on-demand: fc-deploy-service
    [2021-09-26T17:49:33.891] [INFO ] [FC] - Getting list provision: fc-deploy-service
    [2021-09-26T17:49:33.908] [INFO ] [FC] - Getting listAliases: fc-deploy-service
    [2021-09-26T17:49:33.925] [INFO ] [FC] - Getting listVersions: fc-deploy-service
    [2021-09-26T17:49:35.091] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-09-26T17:49:35.092] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-26T17:49:35.092] [INFO ] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9****
    [2021-09-26T17:49:35.093] [INFO ] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN****
    [2021-09-26T17:49:35.211] [INFO ] [FC-DEPLOY] - Checking Service fc-deploy-service exists
    [2021-09-26T17:49:35.410] [INFO ] [FC-DEPLOY] - Service: fc-deploy-service already exists online.
    [2021-09-26T17:49:35.412] [INFO ] [FC-DEPLOY] - Checking Function http-trigger-function exists
    [2021-09-26T17:49:35.562] [INFO ] [FC-DEPLOY] - Function: http-trigger-function already exists online.
    [2021-09-26T17:49:35.565] [INFO ] [FC-DEPLOY] - Checking Trigger httpTrigger exists
    [2021-09-26T17:49:35.776] [INFO ] [FC-DEPLOY] - Trigger: httpTrigger already exists online.
     Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
     Delete trigger fc-deploy-service/http-trigger-function/httpTrigger success.
     Delete function fc-deploy-service/http-trigger-function success.
     Delete service fc-deploy-service success.
    fc-deploy-test:
      triggers:
        - httpTrigger
      functions:
        - http-trigger-function
      service:   fc-deploy-service                        
    当成功执行该命令时,表示应用已被删除。