通过快速开始,您可以通过一个简单案例试用 scf 的基础功能。
- 已安装 Python 2.7(及以上版本)或 Python 3.6(及以上版本)。
- 已安装 pip。
- 已安装 git。
- 已安装对应的开发语言(如Node开发,需要安装Node.js等)
- 已安装并启动 Docker (可选,仅针对本地调用云函数local invoke)
- 执行以下命令,安装 scf cli。
$ pip install scf
- 执行以下命令,验证 scf cli 是否安装成功。
$ scf --version
返回类似如下信息,则表示安装成功。
scf CLI, version 0.0.1
- 登录 腾讯云控制台。
- 分别获取账号的 APPID,SecretId,SecretKey,以及产品期望所属的地域。
- 执行
scf configure set
命令,将 APPID,SecretId,SecretKey,以及产品期望所属的地域配置到 scf 中。 例如,您希望在广州区使用云函数,并获取到账号 ID 为 1253970223,SecretId 和 SecretKey 分别为 AKIxxxxxxxxxx,uxxlxxxxxxxx。您可以通过执行以下命令,完成 scf cli的配置:
$ scf configure set --region ap-guangzhou --appid 1253970223 --secret-id AKIxxxxxxxxxx --secret-key uxxlxxxxxxxx
- 选择并进入到合适的代码目录,例如
cd ~
。 - 执行以下命令,创建运行环境为 Node.js 8.9,名称为 testscf 的函数。
$ scf init --runtime nodejs8.9 --name testscf
? 此命令会在当前目录下进行创建 testscf 函数。
- 执行以下命令,进入函数目录。
$ cd testscf
- 执行以下命令,查看当前函数目录下的所有文件。
ls
即可发现当前项目目录下包括了 README 说明文档及 template.yaml 配置文件。
- 执行以下命令,打包 template.yaml 配置文件。
$ scf package --template-file template.yaml
scf cli会依据 template.yaml 文件内的描述,将函数目录内的代码生成部署程序包,并生成 deploy 配置文件。
- 执行以下命令,查看当前函数目录下的所有文件。
ls
即可发现项目目录中多了一个 deploy.yaml 部署配置文件,以及类似 32b29935-1ec1-11e9-be82-9801a7af1801.zip
的部署包。
执行以下命令,将本地代码包及函数配置部署到云端。
$ scf deploy --template-file deploy.yaml
完成部署后,您可以登录 腾讯云云函数控制台,检查函数是否已经成功部署。
通过本地调用,您可以在本地使用模拟事件触发函数执行,实时调试函数代码。当前native invoke仅支持Node.js及Python语言。
在通过 scf init 初始化得到的示例项目中,均带有已准备好的代码文件及模板配置文件。以该示例项目为例,假定在环境为 Node.js 8.9下,/Users/xxx/code/scf 目录中创建了一个 hello_world 项目。函数代码 /Users/xxx/code/scf/testproject/hello_world/main.js 示例如下:
'use strict';
exports.main_handler = async (event, context, callback) => {
console.log("%j", event);
return "hello world"
};
- 通过执行 scf native generate-event cos post | scf native invoke --template template.yaml 命令,启动函数在本地运行并返回Hello World:
START RequestId: 3e3e71c9-dc56-1967-c0a3-3a454e2ce634
{"Records":[{"cos":{"cosSchemaVersion":"1.0","cosObject":{"url":"http://testpic-1253970026.cos.ap-guangzhou.myqcloud.com/testfile","meta":{"x-cos-request-id":"NWMxOWY4MGFfMjViMjU4NjRfMTUyMV8yNzhhZjM=","Content-Type":""},"vid":"","key":"/1253970026/testpic/testfile","size":1029},"cosBucket":{"region":"gz","name":"testpic","appid":"1253970026"},"cosNotificationId":"unkown"},"event":{"eventName":"cos:ObjectCreated:Post","eventVersion":"1.0","eventTime":1545205770,"eventSource":"qcs::cos","requestParameters":{"requestSourceIP":"xx.xx.xx.xxx","requestHeaders":{"Authorization":"q-sign-algorithm=sha1&q-ak=AKIDQm6iUh2NJ6jL41tVUis9KpY5Rgv49zyC&q-sign-time=1545205709;1545215769&q-key-time=1545205709;1545215769&q-header-list=host;x-cos-storage-class&q-url-param-list=&q-signature=098ac7dfe9cf21116f946c4b4c29001c2b449b14"}},"eventQueue":"qcs:0:lambda:cd:appid/1253970026:default.printevent.$LATEST","reservedInfo":"","reqid":179398952}}]}
END RequestId: 3e3e71c9-dc56-1967-c0a3-3a454e2ce634
REPORT RequestId: 3e3e71c9-dc56-1967-c0a3-3a454e2ce634 Duration: 1.91 ms
Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 20 MB
"hello world"
通过输出内容可以看到,函数在本地运行完成后,输出了函数的打印日志、及函数返回内容。
- 生成如下的 event.json 测试事件文件:
{
"key1":"value1",
"key2":"value2"
}
- 通过执行 scf native invoke --template template.yaml --event event.json 命令,启动函数在本地运行,并通过文件输出测试事件:
START RequestId: 6d06b0cf-4cc9-1f76-5f92-1f5871ff110a
{"key1":"value1","key2":"value2"}
END RequestId: 6d06b0cf-4cc9-1f76-5f92-1f5871ff110a
REPORT RequestId: 6d06b0cf-4cc9-1f76-5f92-1f5871ff110a Duration: 1.72 ms
Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 20 MB
"hello world"
通过输出内容可以看到,函数代码打印了测试事件,并返回了指定内容。
通过本地调用,您可以在本地使用模拟事件触发函数执行,实时调试函数代码。
前置依赖
在使用本地调用命令前,需要安装 docker 程序。
通过如下命令确保 docker 已安装并启动:
$ docker version
Client: Docker Engine - Community
Version: 18.09.1
API version: 1.39
Go version: go1.10.6
Git commit: 4c52b90
Built: Wed Jan 9 19:33:12 2019
OS/Arch: darwin/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.1
API version: 1.39 (minimum version 1.12)
Go version: go1.10.6
Git commit: 4c52b90
Built: Wed Jan 9 19:41:49 2019
OS/Arch: linux/amd64
Experimental: true
- 执行以下命令,使用 cos put 模拟事件触发 testscf 项目中的 hello_world 函数。
$ scf local generate-event cos post | scf local invoke -t template.yaml
read event from stdin
pull image ccr.ccs.tencentyun.com/scfrepo/scfcli:python2.7......
START RequestId: 5771eb61-dba7-4857-949a-2184c0b389d6
END RequestId: 5771eb61-dba7-4857-949a-2184c0b389d6
REPORT RequestId: 5771eb61-dba7-4857-949a-2184c0b389d6 Duration: 0 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 14 MB
"hello world"
scf 工具将会在本地使用容器方式启动函数代码,并将scf local generate-event cos post
命令生成的模拟事件发送给函数作为入参。
- 修改 testscf 项目目录中的 hello_world 文件夹下的 main.py 文件,增加对 event 的打印。
# -*- coding: utf8 -*-
'use strict';
exports.main_handler = async (event, context, callback) => {
console.log("%j", event);
return "hello world"
};
- 执行以下命令,查看打印结果。
$ scf local generate-event cos post | scf local invoke -t template.yaml
read event from stdin
pull image ccr.ccs.tencentyun.com/scfrepo/scfcli:python2.7......
START RequestId: 1271deb3-04ca-46e8-a85e-04cf016a0207
{'Records': [{'cos': {'cosSchemaVersion': '1.0', 'cosObject': {'url': 'http://testpic-1253970026.cos.ap-guangzhou.myqcloud.com/testfile', 'meta': {'x-cos-request-id': 'NWMxOWY4MGFfMjViMjU4NjRfMTUyMV8yNzhhZjM=', 'Content-Type': ''}, 'vid': '', 'key': '/1253970026/testpic/testfile', 'size': 1029}, 'cosBucket': {'region': 'gz', 'name': 'testpic', 'appid': '1253970026'}, 'cosNotificationId': 'unkown'}, 'event': {'eventVersion': '1.0', 'eventName': 'cos:ObjectCreated:Post', 'eventTime': 1545205770, 'eventSource': 'qcs::cos', 'requestParameters': {'requestSourceIP': '59.37.125.38', 'requestHeaders': {'Authorization': 'q-sign-algorithm=sha1&q-ak=AKIDQm6iUh2NJ6jL41tVUis9KpY5Rgv49zyC&q-sign-time=1545205709;1545215769&q-key-time=1545205709;1545215769&q-header-list=host;x-cos-storage-class&q-url-param-list=&q-signature=098ac7dfe9cf21116f946c4b4c29001c2b449b14'}}, 'eventQueue': 'qcs:0:lambda:cd:appid/1253970026:default.printevent.$LATEST', 'reservedInfo': '', 'reqid': 179398952}}]}
END RequestId: 1271deb3-04ca-46e8-a85e-04cf016a0207
REPORT RequestId: 1271deb3-04ca-46e8-a85e-04cf016a0207 Duration: 2 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 14 MB
"hello world"
在此快速开始篇章中,我们简单介绍了 scf cli的安装、配置过程,通过初始化模板项目,完成了打包、部署、本地测试的使用过程。
更多详细信息可见 scf cli的使用指导及项目介绍。