Serverless Devs是一個開源開放的Serverless開發人員工具,通過該工具您可以快速地開發、建立、測試和部署專案,實現專案的全生命週期管理。本文以部署運行環境為Node.js 14的函數為例,介紹如何使用Serverless Devs下載對應的Function Compute應用模板,然後快速開發部署應用。
背景資訊
本文介紹兩種方式初始化專案。當您初次使用Serverless Devs時,建議您使用方式一體驗Serverless Devs。如果您想瞭解關於Serverless Devs的更多資訊,例如本地調試、遠程調用、刪除資源等操作,建議您使用方式二體驗。
前提條件
方式一:使用s命令管理函數
執行以下命令,初始化專案。
sudo s請根據輸出的互動提示,初始化專案。輸出樣本:
No Serverless-Devs project is currently detected. Do you want to create a new project? Yes//在專案檔內未檢測到Serverless-Devs專案。選擇是否建立一個專案。
Serverless Awesome: https://github.com/Serverless-Devs/Serverless-Devs/blob/master/docs/zh/awesome.md
Hello Serverless for Cloud Vendors Alibaba Cloud Serverless//選擇目標雲廠商,本樣本以阿里雲為例。
Hello, serverlesser. Which template do you like? Quick start [Deploy a Hello World function to FaaS]//選擇您需要建立的模板。本樣本以快速部署運行環境為Node.js 14的事件函數為例。
Which template do you like? [Event] Node.js 14
Create application command: [s init devsapp/start-fc-event-nodejs14]
Please input your project name (init dir) start-fc-event-nodejs14//Serverless Devs預設為您產生一個專案名稱,您也可以自訂該名稱。
file decompression completed
____ _ _ ___ _ _ _ _____ ____
/ _ \/ \ / \\ \/// \ /\/ \ /| / // _\
| / \|| | | | \ / | | ||| |\ || | __\| /
| |-||| |_/\| | / / | \_/|| | \|| | | | \__
\_/ \|\____/\_//_/ \____/\_/ \| \_/ \____/
please select credential alias default//選擇您在配置Serverless Devs時設定的別名。
Welcome to the Aliyun FC start application
This application requires to open these services:
FC : https://fc.console.alibabacloud.com/
* 專案初始化完成,您可以直接進入專案目錄下,並使用s deploy進行專案部署。
Thanks for using Serverless-Devs
You could [cd /test/start-fc-event-nodejs14] 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
Do you want to deploy the project immediately? Yes//選擇是否部署專案,本樣本以選擇部署專案為例。
......
helloworld:
region: cn-hangzhou
service:
name: hello-world-service
function:
name: event-nodejs14
runtime: nodejs14
handler: index.handler
memorySize: 128
timeout: 60方式二:使用s init命令管理函數
初始化專案。
在目標檔案內執行以下命令,初始化專案:
sudo s init devsapp/start-fc-http-nodejs14說明本樣本以部署一個運行環境為Node.js 14的HTTP函數為例,介紹如何快速初始化一個專案。當需要部署其他運行環境的樣本時,您可以將上述模板
start-fc-http-nodejs14名稱中http替換為其他類型的觸發方式例如event,也可以將nodejs14替換為其他類型的運行環境。執行輸出:
Serverless Awesome: https://github.com/Serverless-Devs/Serverless-Devs/blob/master/docs/zh/awesome.md Please input your project name (init dir) start-fc-http-nodejs14//Serverless Devs會預設產生一個專案名稱,您也可以自訂該名稱。 file decompression completed ____ _ _ ___ _ _ _ _____ ____ / _ \/ \ / \\ \/// \ /\/ \ /| / // _\ | / \|| | | | \ / | | ||| |\ || | __\| / | |-||| |_/\| | / / | \_/|| | \|| | | | \__ \_/ \|\____/\_//_/ \____/\_/ \| \_/ \____/ please select credential alias default//選擇您在配置Serverless Devs時設定的別名。 Welcome to the Aliyun FC start application This application requires to open these services: FC : https://fc.console.alibabacloud.com/ * 專案初始化完成,您可以直接進入專案目錄下,並使用s deploy進行專案部署。 Thanks for using Serverless-Devs You could [cd /test/start-fc-http-nodejs14] 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 Do you want to deploy the project immediately? No//選擇部署專案。當成功初始化專案後,會在目前的目錄中產生一個start-fc-http-nodejs14檔案夾,該檔案夾內包含以下檔案:
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。code:code檔案夾內包含index.js檔案,可在該檔案內編輯函數代碼邏輯。
readme.md:依賴資訊清單檔,該檔案內定義了專案所需的各個模組和專案的配置資訊。
執行以下命令,進入專案目錄內:
cd start-fc-http-nodejs14可選:構建應用。
在專案目錄內執行以下命令,構建應用:
sudo s build執行成功該步驟後,會在目前的目錄中產生一個.s檔案,存放輸出的構建產物,該檔案可用於本地調試和部署。
可選:本地調試應用。
當您在本地完成應用的開發構建後,可能需要在本地調實驗證功能是否正常。您可以按需選擇命令調試函數:
HTTP函數
sudo s local start執行輸出:
[2021-12-16 07:17:05] [INFO] [FC-LOCAL-INVOKE] - Using trigger for start: name: httpTrigger type: http config: authType: anonymous methods: - GET [2021-12-16 07:17:05] [INFO] [FC-LOCAL-INVOKE] - HttpTrigger httpTrigger of hello-world-service/http-trigger-nodejs14 was registered url: http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/ methods: GET authType: anonymous Tips:you can also use these commands to run/debug custom domain resources: Start with customDomain: * s local start auto Debug with customDomain: * s local start -d 3000 auto Tips for next step ====================== * Deploy Resources: s deploy helloworld: status: succeed function compute app listening on port 7013!事件函數
sudo s local invoke
當您執行調試命令後,由於函數類型不同返回結果也會不同:
HTTP函數
當您的函數是HTTP函數時,執行完調試命令後,將在執行輸出中返回一個URL地址,此時調試流程阻塞在這裡,您可以使用以下方式觸發函數執行:
使用cURL工具觸發函數執行
curl http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/使用瀏覽器訪問HTTP函數
http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/
事件函數
如果您的函數是事件函數,執行完調試命令後,將立即返回調試結果。
部署應用。
執行以下命令,將應用部署至Function Compute:
sudo s deploy -y執行輸出:
Checking Service, Function, Triggers (0.82s) Creating Service, Function, Triggers (0.6s) Creating custom domain (0.31s) 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 helloworld: region: cn-hangzhou service: name: hello-world-service function: name: http-trigger-nodejs14 runtime: nodejs14 handler: index.handler memorySize: 128 timeout: 60 url: custom_domain: - domain: http://http-trigger-nodejs14.hello-world-service.188077086902****.cn-hangzhou.fc.devsapp.net triggers: - type: http name: httpTrigger執行成功後,該命令會將應用部署至Function Compute。
可選:遠端偵錯應用。
執行以下命令,遠端偵錯應用:
sudo s invoke -e '{"body": "","method":"GET","headers":{"header_1":"v1"},"queries":{"key":"value"},"path":"/"}'執行輸出:
Request url: https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/ ========= FC invoke Logs begin ========= FC Invoke Start RequestId: 6a3adedb-5d5d-4ac5-aa0b-2d43f4ace65c load code for handler:index.handler 2022-03-04T08:39:14.770Z 6a3adedb-5d5d-4ac5-aa0b-2d43f4ace65c [verbose] hello world FC Invoke End RequestId: 6a3adedb-5d5d-4ac5-aa0b-2d43f4ace65c Duration: 66.23 ms, Billed Duration: 67 ms, Memory Size: 128 MB, Max Memory Used: 42.48 MB ========= FC invoke Logs end ========= FC Invoke Result[Code: 200]: { path: '//', queries: { key: 'value' }, headers: { accept: 'application/json', authorization: 'FC yourAccessKeyID:yourAccessKeySecret', date: 'Fri, 04 Mar 2022 08:39:14 GMT', header_1: 'v1', host: '188077086902****.cn-hangzhou.fc.aliyuncs.com', 'user-agent': 'Node.js(v14.17.4) OS(linux/x64) SDK(@alicloud/fc2@v2.5.0)', 'x-forwarded-proto': 'http' }, method: 'GET', requestURI: '/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14//?key=value', clientIP: '47.97.XX.XX', body: '' } End of method: invoke由於本樣本部署的函數是HTTP函數,所以在成功執行遠端偵錯命令後,執行輸出會返回一個URL,您可以使用cURL工具觸發執行線上的函數:
curl https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/可選:刪除應用。
執行以下命令刪除應用:
sudo s remove service執行輸出:
Need to delete the resource in the cn-hangzhou area, the operation service is hello-world-service: ...... Delete trigger hello-world-service/http-trigger-nodejs14/httpTrigger success. Delete function hello-world-service/http-trigger-nodejs14 success. Delete service hello-world-service success. [2022-03-04 08:40:26] [INFO] [FC] - Getting list on-demand: _FC_NAS_hello-world-service-ensure-nas-dir-exist-service [2022-03-04 08:40:26] [INFO] [FC] - Getting list provision: _FC_NAS_hello-world-service-ensure-nas-dir-exist-service [2022-03-04 08:40:26] [INFO] [FC] - Getting listAliases: _FC_NAS_hello-world-service-ensure-nas-dir-exist-service [2022-03-04 08:40:26] [INFO] [FC] - Getting list on-demand: _FC_NAS_hello-world-service [2022-03-04 08:40:26] [INFO] [FC] - Getting list provision: _FC_NAS_hello-world-service [2022-03-04 08:40:26] [INFO] [FC] - Getting listAliases: _FC_NAS_hello-world-service End of method: remove當成功執行該命令時,表示應用已被刪除。