使用Serverless Framework和SCF的小夥伴都知道,這裡有一個悲傷的故事:Serverless的Yaml和SCFCLI/VSCode的Yaml長得不太一樣:
雖然我之前已經出了一個工具,可以在線進行轉換的:
但是實際上並不是很好用:因為沒有人會把yaml打開一個網頁進行轉換,再新建文件,複製粘貼,這流程太長太麻煩!為了解決這個問題,我也是煞費苦心,開發了一個新的組件:tencent-prescf
是的,這個組件就是可以通過serverless直接部署原有的yaml內容:
例如,我此時此刻新建一個scfcli的項目:
建立之後,為了模擬常用的場景:SCF+APIGW,我修改一下yaml:
<code>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: ANYGlobals: Function: Timeout: 10/<code>
然後我新建serverless.yaml:
<code>TestPreScf: component: "@gosls/tencent-prescf" inputs: yaml: ./template.yaml region: ap-guangzhou/<code>
完成之後,我執行
<code>sls --debug/<code>
可以看到,函數被部署:
同時API網關也可以正常使用:
同理,通過
<code>sls remove --debug/<code>
可以快速將函數等資源移除:
當然了,有的小夥伴說,我原先是可以通過scfcli或者vscode單獨部署一個函數,那麼這個只能一次部署全部麼?當然不是:
這裡面有這樣一個參數:
<code># serverless.ymlhelloWorld: component: "@gosls/tencent-prescf" inputs: yaml: ./template.yaml region: ap-guangzhou functionName: hello_wrold/<code>
如果不寫functionName,默認部署全部,如果寫了,就會默認只部署老的yaml中對應的函數。
閱讀更多 GoServerless 的文章