Serverless framework provider plugin for Tencent SCF(Serverless Cloud Function)
user
tencentyun
本插件提供 Serverless Framework 对 Tencent SCF(Serverless Cloud Function) 的支持.
npm i -g serverless
安装)Qcloud 帐号
项目文件结构类似这样:
├── index.js
├── package.json
└── serverless.yml
serverless.yml
:
service: hello-world
provider:
name: tencent
region: ap-guangzhou
runtime: Nodejs8.9
stage: dev
credentials: ~/.tencentcloud/credentials.ini # 必须提供绝对路径
plugins:
- serverless-tencent-scf
package:
exclude:
- ./**
include:
- index.js
functions:
hello:
handler: index.hello
description: hello world function
memorySize: 128
timeout: 3
events:
- timer:
name: 5m
cron: "*/5 * * * *"
更多触发器配置示例请参考example/serverless.yml
package.json
:
{
"devDependencies": {
"serverless-tencent-scf": "*"
}
}
index.js
:
"use strict";
exports.hello = (event, context, callback) => {
callback(null, "Hello world!");
};
API 密钥配置credentials.ini
:
[default]
tencent_secret_id=AKIxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
tencent_secret_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
tencent_app_id=1251******
此文件包含敏感信息,请勿放在项目目录下,建议将它放在个人目录,并以绝对路径配置到 provider.credentials 。
如果不配置provider.credentials
,将会使用tencentcli的默认配置,但因为没有 APPID,所以部分功能无法使用,具体见FAQ。
以下是各信息的获取途径
tencent_secret_id
及 tencent_secret_key
云 API 密钥,可以在 腾讯云-API 密钥管理 查询到。如果没有,请新建tencent_app_id
腾讯云账号的 APPID,可以在 腾讯云-账号中心 查询到插件初始安装:
serverless plugin install --name serverless-tencent-scf
部署或更新 scf 到腾讯云:
serverless deploy
TIP: serverless
指令可以简化为sls
,即使用sls deploy
发布
从腾讯云删除 scf:
serverless remove
远程调用
serverless invoke -f hello
日志查询
serverless logs -f hello
示例中 provider.credentials
额外配置了 tencent_app_id
,它不是必选项,只在部分功能下需要使用
以下是需要使用 APPID 的功能
如果未配置,则执行到以上功能时会提示错误
当包文件大于 10MB 时,需要通过用户自己的 cos 上传包进行发布,上传操作需要本账号有中转 bucket(scf-deployment
)读写权限。你可以用主账号给当前账号绑定相关 CAM 策略
当然你也可以进行更精确的 CAM 授权
以下是需要给 scf 角色授予相关权限的功能
通常它是由工具自动化完成的,但如果你在使用子账号密钥,而当前云账号没有进行初始角色创建与授权,并且没有权限进行相关操作时会报错。
你可以换用主账号执行一次serverless tencent initcam
,这样就会初始化角色授权,使得 scf 可以访问其它业务资源(如对象存储、API 网关等)
访问 腾讯云-访问管理-角色 可以查看 scf 角色(SCF_QcsRole
) 已绑定的策略
如需使用代理,请配置 https_proxy
环境变量
Latest commit b2f54ec on Sep 24, 2017