使用Serverless Framework和SCF的小伙伴都知道,這里有一個(gè)悲傷的故事:Serverless的Yaml和SCFCLI/VSCode的Yaml長得不太一樣:

image
雖然我之前已經(jīng)出了一個(gè)工具,可以在線進(jìn)行轉(zhuǎn)換的:

image
但是實(shí)際上并不是很好用:因?yàn)闆]有人會(huì)把yaml打開一個(gè)網(wǎng)頁進(jìn)行轉(zhuǎn)換,再新建文件,復(fù)制粘貼,這流程太長太麻煩!為了解決這個(gè)問題,我也是煞費(fèi)苦心,開發(fā)了一個(gè)新的組件:tencent-prescf
是的,這個(gè)組件就是可以通過serverless直接部署原有的yaml內(nèi)容:
例如,我此時(shí)此刻新建一個(gè)scfcli的項(xiàng)目:

image
建立之后,為了模擬常用的場景:SCF+APIGW,我修改一下yaml:
Resources:
anycodes:
Type: TencentCloud::Serverless::Namespace
hello_world:
Type: TencentCloud::Serverless::Function
Properties:
CodeUri: ./
Type: Event
Description: This is a template function
Role: QCS_SCFExcuteRole
Environment:
Variables:
ENV_FIRST: env1
ENV_SECOND: env2
Handler: index.main_handler
MemorySize: 128
Runtime: Python3.6
Timeout: 3
Events:
hello_world_apigw: # ${FunctionName} + '_apigw'
Type: APIGW
Properties:
StageName: release
ServiceId:
HttpMethod: ANY
Globals:
Function:
Timeout: 10
然后我新建serverless.yaml:
TestPreScf:
component: "@gosls/tencent-prescf"
inputs:
yaml: ./template.yaml
region: ap-guangzhou

image
完成之后,我執(zhí)行
sls --debug
可以看到,函數(shù)被部署:

image
同時(shí)API網(wǎng)關(guān)也可以正常使用:

image
同理,通過
sls remove --debug
可以快速將函數(shù)等資源移除:

image
當(dāng)然了,有的小伙伴說,我原先是可以通過scfcli或者vscode單獨(dú)部署一個(gè)函數(shù),那么這個(gè)只能一次部署全部么?當(dāng)然不是:
這里面有這樣一個(gè)參數(shù):
# serverless.yml
helloWorld:
component: "@gosls/tencent-prescf"
inputs:
yaml: ./template.yaml
region: ap-guangzhou
functionName: hello_wrold
如果不寫functionName,默認(rèn)部署全部,如果寫了,就會(huì)默認(rèn)只部署老的yaml中對應(yīng)的函數(shù)。

image