mirror of
https://github.com/serverless/serverless.git
synced 2026-01-25 15:07:39 +00:00
5.7 KiB
5.7 KiB
操作场景
该文档引导您通过 Serverless Framework Component,在腾讯云上快速创建、配置和部署一个云函数 + API 网关的服务。
前提条件
在使用之前,请确保已经 安装 Serverless Framework 1.57.0 以上版本。
操作步骤
创建服务
- 创建并进入目录:
$ mkdir my-function && cd my-function
- 在目录中创建
index.js作为云函数的入口函数:
$ touch index.js
- 在
index.js中增加如下代码:
'use strict';
exports.main_handler = async (event, context, callback) => {
console.log('%j', event);
return 'hello world';
};
配置服务
在本地创建serverless.yml文件,
$ touch serverless.yml
在serverless.yml中进行如下配置:
# serverless.yml
myFunction:
component: '@serverless/tencent-scf'
inputs:
name: myFunction
codeUri: ./ # 代码目录
handler: index.main_handler
runtime: Nodejs8.9
region: ap-guangzhou
description: My Serverless Function
memorySize: 128
events: # 触发器配置
- apigw:
name: serverless
parameters:
protocols:
- http
endpoints:
- path: /
method: GET
?您可以通过 详细配置文档,查看
serverless.yml中所有可用属性的属性列表。
部署服务
如您的账号未 登录 或 注册 腾讯云,您可以在运行该命令后,直接用微信扫描命令中弹出的二维码,对云账户进行授权登录和注册。
通过sls命令进行部署,并可以添加--debug参数查看部署过程中的信息:
?
sls是serverless命令的简写。
sls --debug
DEBUG ─ Resolving the template's static variables.
DEBUG ─ Collecting components from the template.
DEBUG ─ Downloading any NPM components found in the template.
DEBUG ─ Analyzing the template's components dependencies.
DEBUG ─ Creating the template's components graph.
DEBUG ─ Syncing template state.
DEBUG ─ Executing the template's components graph.
Please scan QR code login from wechat.
Wait login...
Login successful for TencentCloud.
DEBUG ─ Compressing function myFunction file to /Users/tina/Desktop/live/scfcomponent/my-function/.serverless/myFunction.zip.
DEBUG ─ Compressed function myFunction file successful
DEBUG ─ Uploading service package to cos[sls-cloudfunction-ap-guangzhou-code]. sls-cloudfunction-default-myFunction-1582797244.zip
DEBUG ─ Uploaded package successful /Users/tina/Desktop/live/scfcomponent/my-function/.serverless/myFunction.zip
DEBUG ─ Creating function myFunction
DEBUG ─ Created function myFunction successful
DEBUG ─ Setting tags for function myFunction
DEBUG ─ Creating trigger for function myFunction
DEBUG ─ Starting API-Gateway deployment with name myFunction.serverless in the ap-guangzhou region
DEBUG ─ Service with ID service-qs0cud0s created.
DEBUG ─ API with id api-irl0q216 created.
DEBUG ─ Deploying service with id service-qs0cud0s.
DEBUG ─ Deployment successful for the api named myFunction.serverless in the ap-guangzhou region.
DEBUG ─ Deployed function myFunction successful
myFunction:
Name: myFunction
Runtime: Nodejs8.9
Handler: index.main_handler
MemorySize: 128
Timeout: 3
Region: ap-guangzhou
Description: My Serverless Function
APIGateway:
- serverless - http://service-qs0cud0s-1300862921.gz.apigw.tencentcs.com/release
22s › myFunction › done
测试服务
在浏览器中打开输出链接,或替换如下命令中的链接地址,通过 curl 对其进行测试,该链接可以在sls命令执行后获取得到。
$ curl -X GET http://service-qs0cud0s-1300862921.gz.apigw.tencentcs.com/release
移除服务
如果您不再需要此服务,可以通过如下命令一键移除服务,该命令会清理相应函数和触发器资源。
serverless remove --debug
DEBUG ─ Flushing template state and removing all components.
DEBUG ─ Removing any previously deployed API. api-irl0q216
DEBUG ─ Removing any previously deployed service. service-qs0cud0s
DEBUG ─ Removing function
DEBUG ─ Removed function myFunction successful
9s › myFunction1 › done
配置账户信息(可选)
当前默认支持部署时扫描微信二维码登录,如您希望配置持久的环境变量/密钥信息,也可以参考 配置账号 文档。
更多应用
如果您希望通过 Serverless Framework Component 部署更多 Serverless 应用,可以参考下列最佳实践: