一、操作场景
Serverless Cloud Framework SCF 组件现已支持 Web 类型函数部署,你可以通过 SCF 组件,快速创建与部署 Web 函数。
二、操作步骤
1. 执行以下命令,初始化 Serverless Web 函数模板。
scf init scf-nodejs
2. 进入示例项目,查看目录结构。示例如下:
. http-demo├── serverless.yml # 配置文件├── package.json # 依赖项文件├── scf_bootstrap # 项目启动文件└── index.js # 服务函数
其中 scf_bootstrap 为项目启动文件,具体编写规则请参见 启动文件说明。
3. 打开 serverless.yml,查看配置信息。
你只需要在 yml 里新增 type 参数,指定函数类型,即可完成 Web 类型函数部署。
示例 yml 如下:
component: scfname: httpinputs: src: src: ./ exclude: – .env# 指定 SCF 类型为 Web 类型type: webname: web-functionregion: ap-guangzhouruntime: Nodejs12.16# 对于 Node.js,可以支持打开自动安装依赖installDependency: trueevents: – apigw: parameters: protocols: – http – https environment: release endpoints: – path: / method: ANY
4. 在根目录下执行 scf deploy 命令,即可完成服务部署。示例如下:
$ scf deployserverless-cloud-framework Action: "deploy" – Stage: "dev" – App: "http" – Name: "http"type: webfunctionName: web-functiondescription: This is a function in http applicationnamespace: defaultruntime: Nodejs12.16handler: memorySize: 128lastVersion: $LATESTtraffic: 1triggers: – NeedCreate: true created: true serviceId: service-xxxxxx serviceName: serverless subDomain: service-xxxxxx.cd.apigw.tencentcs.com protocols: http&https environment: release apiList: – path: / method: ANY apiName: index created: true authType: NONE businessType: NORMAL isBase64Encoded: false apiId: api-xxxxxx internalDomain: url: https://service-xxxx.cd.apigw.tencentcs.com/release/18s › http › 执行成功
三、查看访问日志
与事件型函数相同,可直接通过 scf log 命令查看部署完成的函数最近10条日志信息。示例如下:
$ scf logserverless-cloud-framework Action: "log" – Stage: "dev" – App: "http" – Name: "http"- requestId: xxxxx retryNum: 0 startTime: 1624262955432 memoryUsage: 0.00 duration: 0 message: """- requestId: xxxxx retryNum: 0 startTime: 1624262955432 memoryUsage: 0.00 duration: 0 message: """
四、测试服务
·方案 1:在浏览器直接打开输出的路径 URL,如果可以正常访问,则说明函数创建成功。如下:
·方案 2:你可以使用其他 HTTP 测试工具,例如 CURL、POSTMAN 等工具测试你已创建成功的 Web 函数。如下示例为通过 CURL 工具测试:
curl https://service-xxx.cd.apigw.tencentcs.com/release/
五、删除服务
执行以下命令,即可移除你已部署的云上资源。
scf remove
六、Web 框架迁移
Serverless Cloud Framework 还提供了专门针对 Web 框架部署的 HTTP 组件,快速实现 Web 框架部署、创建层、静态资源分离、CDN 加速等功能,使用方式请参见 通过命令行完成框架部署。