faas-cf是天翼云函數計算的組件,使用該組件,您可以快速部署Serverless項目,以及管理Serverless函數的生命周期。本文介紹如何使用Serverless Devs和faas-cf進行部署、調用、刪除函數。
安裝 Serverless Devs 工具
您可以通過如下任一方法進行安裝:
通過安裝
npm install @serverless-devs/s -g通過安裝
yarn global add @serverless-devs/s手動下載二進制安裝
1. 進入Serverless Devs 頁面,選擇最新的發布版本,下載對應操作系統的壓縮包。
2. 解壓壓縮包。
3. 將解壓后的二進制文件進行重命名。對于Windows平臺,將s-xxx-win.exe重命名為s.exe;對于Linux/macOS平臺,將s-xxx-linux/s-xxx-macos重命名為s。
4. 將二進制文件移動到系統PATH目錄。比如,對于Windows平臺,將s.exe移動到C:\Windows\System32;對于Linux/macOS平臺,將s移動到/usr/local/bin。
通過腳本安裝
腳本安裝方式僅適用于Linux/macOS:
curl -o- -L //cli.serverless-devs.com/install.sh | bash驗證安裝
打開命令終端,輸入 s -v 命令,如果正確安裝和配置,將會顯示如下輸出:
@serverless-devs/s: 3.xx配置密鑰
1. 登錄天翼云官網,創建并獲取。
2. 您可以執行以下命令一鍵配置 Serverless Devs 工具(請將${AK}、${SK}分別換成您的 AccessKey ID、SecurityKey):
s config add -a default --kl AK,SK --il ${AK},${SK} -f您也可以使用交互式命令 s config add 進行配置,provider選擇“Custom”,然后配置AK、SK項。以下為示例:
$ s config add
? Please select a provider: Custom (others)
? Please select a type: Add key-value pairs
? Please enter key: AK
? Please enter value: Input Your AccessKey ID
? Please select a type: Add key-value pairs
? Please enter key: SK
? Please enter value: Input Your SecurityKey
? Please select a type: End of adding key-value pairs
? Please create alias for key pair. If not, please enter to skip default
Alias: default
Credential:
__provider: Custom
AK: ************************
SK: ************************管理函數
新建 Serverless Devs 項目
創建一個空文件夾,在該文件夾下創建 s.yaml 文件、code 目錄,然后在 code 目錄下 創建 index.py 文件。文件目錄結構如下:
index.py 文件為函數代碼,文件內容如下:
def handler(event, context):
return 'hello world'Serverless Devs 工具默認使用 s.yaml 文件描述部署資源的元信息,其內容如下:
edition: 3.0.0
name: hello-devsapp
access: default
vars:
region: "bb9fdb42056f11eda1610242ac110002"
resources:
my-func:
component: faas-cf # 組件,提供具體的業務能力。faas-cf 組件提供了對天翼云Serverless函數的管理能力
props:
region: ${vars.region} # 部署的資源池,天翼云華東1資源池為 bb9fdb42056f11eda1610242ac110002
functionName: hello-world # 函數名稱
code: ./code # 函數代碼目錄
createType: 1 # 函數類型:標準函數(1)、自定義函數(2)、容器函數(3)
runtime: # 函數運行配置
handleType: event # 處理事件請求(event)、處理HTTP請求(http)
handler: index.handler # 請求處理程序
runtime: python3.10 # 運行環境
executeTimeout: 64 # 執行超時時間
instanceConcurrency: 1 # 實例并發度
container: # 運行容器配置
memorySize: 512 # 內存規格,單位:MB
cpu: 0.5 # vCPU規格,單位vCPU
diskSize: 512 # 臨時硬盤大小,單位:MB
timeZone: UTC # 時區,例如:UTC, Asia/Shanghai部署函數
進入到 s.yaml 文件所在的路徑,執行如下命令進行部署函數:
s deploy如果部署函數成功,將會顯示如下輸出:
functionId: 39061695070521915
code:
ossBucketName:
ossObjectName: function/******
snapshotOssObjectName:
zipFile:
container:
image: faas-******/python3.10-runtime:v1.3.1
timeZone: UTC
memorySize: 512
cpu: 0.5
diskSize: 512
runCommand: Li9ib290c3RyYXAuc2g=
listenPort: 8080
codeDirPath: /code
logLevel:
sysLogLevel: INFO
maxScale: null
minScale: null
fastStart: 0
functionName: hello-world
...調用函數
進入到上述 s.yaml 文件所在的路徑,執行如下命令進行調用函數:
s invoke如果調用函數成功,將會顯示如下輸出:
status: 200
headers:
content-length: 11
content-type: text/plain; charset=utf-8
date: Tue, 27 May 2025 08:06:01 GMT
x-fc-request-id: cfece192-0449-4de4-bf5f-180700006fa8
x-fc-status-code: 200
x-fc-upstream-service-time: 2781
x-fc-req-cost-time: 2782
x-fc-req-arrive-time: 1748333160021
x-fc-resp-start-time: 1748333162803
connection: close
body: hello world
__component: faas-cf刪除函數
進入到上述 s.yaml 文件所在的路徑,執行如下命令進行刪除函數:
s remove根據提示選擇確認,以刪除函數,如下為示例輸出:
? 確認刪除函數'hello-world'嗎? 確認