全部產品
Search
文件中心

Function Compute:通過Serverless Devs管理函數

更新時間:Jul 06, 2024

Serverless Devs是一個開源開放的Serverless開發人員工具,通過該工具您可以快速地開發、建立、測試和部署專案,實現專案的全生命週期管理。本文以部署運行環境為Node.js 16的函數為例,介紹如何使用Serverless Devs下載對應的Function Compute應用模板,然後快速開發部署應用。

背景資訊

本文介紹兩種方式初始化專案。當您初次使用Serverless Devs時,建議您使用方式一體驗Serverless Devs。如果您想瞭解關於Serverless Devs的更多資訊,例如本地調試、遠程調用、刪除資源等操作,建議您使用方式二體驗。

前提條件

  1. 開通Function Compute服務

  2. 快速入門

  3. 配置Serverless Devs

方式一:使用s命令管理函數

執行以下命令,進入專案初始化引導模組。

sudo s

請根據引導操作完成專案初始化。輸出樣本:

 ? No Serverless-Devs project is currently detected. Do you want to create a new project? Yes

  More applications: https://registry.serverless-devs.com
? Hello Serverless for Cloud Vendors Alibaba Cloud Serverless
? Hello, serverlesser. Which template do you like? Quick start [Deploy a Hello World function to FaaS]
? Which template do you like? Node.js

 Create application command: [s init start-fc3-nodejs]

? Please input your project name (init dir) start-fc3-nodejs
Downloading[/v3/packages/start-fc3-nodejs/zipball/0.0.9]...
Download start-fc3-nodejs successfully

  Serverless Devs Application Case

    Cloud services required:
    - FC : https://fc.console.aliyun.com/

    Tips:
    - FC Component: https://github.com/devsapp/fc3/blob/master/docs/zh/readme.md
建立應用所在的地區
? 地區 cn-hangzhou
只能包含字母、數字、底線和中劃線。不能以數字、中劃線開頭。長度在 1-128 之間。
? 函數名稱 start-nodejs-at63
建立應用所在的地區
? nodejs 運行時 nodejs16
? please select credential alias default

    * Before using, please check whether the actions command in Yaml file is available
    * Carefully reading the notes in s.yaml is helpful for the use of the tool
    * If need help in the use process, please apply to join the Dingtalk Group: 33947367


 Thanks for using Serverless-Devs
 You could [cd /Users/start-fc3-nodejs] 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
 More applications: https://registry.serverless-devs.com

方式二:使用s init命令管理函數

  1. 初始化專案。

    在目標檔案內執行以下命令,初始化專案:

    sudo s init start-fc3-nodejs
    說明

    本樣本以部署一個運行環境為Node.js 16的函數為例,介紹如何快速初始化一個專案。當需要部署其他運行環境的樣本時,您可以在初始化專案時選擇對應的運行時。

    執行輸出:

      More applications: https://registry.serverless-devs.com
    ? Please input your project name (init dir) start-fc3-nodejs
    Downloading[/v3/packages/start-fc3-nodejs/zipball/0.0.9]...
    Download start-fc3-nodejs successfully
    
      Serverless Devs Application Case
    
        Cloud services required:
        - FC : https://fc.console.aliyun.com/
    
        Tips:
        - FC Component: https://github.com/devsapp/fc3/blob/master/docs/zh/readme.md
    建立應用所在的地區
    ? 地區 cn-hangzhou
    只能包含字母、數字、底線和中劃線。不能以數字、中劃線開頭。長度在 1-128 之間。
    ? 函數名稱 start-nodejs-900k
    建立應用所在的地區
    ? nodejs 運行時 nodejs16
    ? please select credential alias default
    
        * Before using, please check whether the actions command in Yaml file is available
        * Carefully reading the notes in s.yaml is helpful for the use of the tool
        * If need help in the use process, please apply to join the Dingtalk Group: 33947367
    
    
      Thanks for using Serverless-Devs
      You could [cd /Users/start-fc3-nodejs] 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
      More applications: https://registry.serverless-devs.com

    當成功初始化專案後,會在目前的目錄中產生一個start-fc3-nodejs檔案夾,該檔案夾內包含以下檔案:

    • s.yaml:Function Compute資源,該資源是以YAML規範定義的。關於YAML規範的詳細資料,請參見YAML規範

    • s_en.yaml:同s.yaml。系統預設使用s.yaml檔案,如需使用s_en.yaml檔案,請在命令中顯式指定。例如,部署應用時,命令中增加參數-t,即s deploy -t s_en.yaml -y

    • codecode檔案夾內包含index.js檔案,可在該檔案內編輯函數代碼邏輯。

    • readme.md:依賴資訊清單檔,該檔案內定義了專案所需的各個模組和專案的配置資訊。

  2. 執行以下命令,進入專案目錄內:

    cd start-fc3-nodejs
  3. 可選:本地調試應用。

    執行以下命令進行本地調試。

    sudo s local invoke -e "{\"key\": \"value\"}"
  4. 部署應用。

    執行以下命令,將應用部署至Function Compute:

    sudo s deploy -y

    執行輸出:

      Steps for [deploy] of [hello-world-app]
    ====================
    
     [hello_world] completed (1.36s)
    
      Result for [deploy] of [hello-world-app]
    ====================
    region:         cn-hangzhou
    description:    hello world by serverless devs
    functionName:   start-nodejs-900k
    handler:        index.handler
    internetAccess: true
    memorySize:     128
    role:
    runtime:        nodejs16
    timeout:        30

    執行成功後,該命令會將應用部署至Function Compute。

  5. 可選:遠端偵錯應用。

    執行以下命令,遠端偵錯應用:

    sudo s invoke -e "{\"key\": \"value\"}"

    執行輸出:

      Steps for [invoke] of [hello-world-app]
    ====================
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: 1-65bb583a-15b24a10-58ded3270984
    load code for handler:index.handler
    FC Invoke End RequestId: 1-65bb583a-15b24a10-58ded3270984
    
    Duration: 4.43 ms, Billed Duration: 5 ms, Memory Size: 128 MB, Max Memory Used: 8.90 MB
    ========= FC invoke Logs end =========
    
    Invoke instanceId: c-65bb583a-15416274-0a3cdda6c4ee
    Code Checksum: 16688953495441179501
    Qualifier: LATEST
    RequestId: 1-65bb583a-15b24a10-58ded3270984
    
    Invoke Result:
    hello world
     [hello_world] completed (0.6s)
  6. 可選:刪除應用。

    執行以下命令刪除應用:

    sudo s remove

    執行輸出:

      Steps for [remove] of [hello-world-app]
    ====================
    Remove function: cn-hangzhou/start-nodejs-900k
    
         ? Are you sure you want to delete the resources listed above yes
     [hello_world] completed (4.09s)
    

    當成功執行該命令時,表示應用已被刪除。

常見問題