使用Serverless Framework和SCF的小伙伴都知道,这里有一个悲伤的故事:Serverless的Yaml和SCFCLI/VSCode的Yaml长得不太一样:

虽然我之前已经出了一个工具,可以在线进行转换的:

但是实际上并不是很好用:因为没有人会把yaml打开一个网页进行转换,再新建文件,复制粘贴,这流程太长太麻烦!为了解决这个问题,我也是煞费苦心,开发了一个新的组件:tencent-prescf
是的,这个组件就是可以通过serverless直接部署原有的yaml内容:
例如,我此时此刻新建一个scfcli的项目:

建立之后,为了模拟常用的场景: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

完成之后,我执行
sls --debug
可以看到,函数被部署:

同时API网关也可以正常使用:

同理,通过
sls remove --debug
可以快速将函数等资源移除:

当然了,有的小伙伴说,我原先是可以通过scfcli或者vscode单独部署一个函数,那么这个只能一次部署全部么?当然不是:
这里面有这样一个参数:
# serverless.yml
helloWorld:
component: "@gosls/tencent-prescf"
inputs:
yaml: ./template.yaml
region: ap-guangzhou
functionName: hello_wrold
如果不写functionName
,默认部署全部,如果写了,就会默认只部署老的yaml中对应的函数。

网友评论