简介

该组件是 serverless-tencent 组件库中的基础组件之一。通过云函数 SCF 组件,可以快速,方便的创建,配置和管理腾讯云的 SCF 云函数。

快速开始

通过 SCF 组件,对一个云函数进行完整的创建,配置,部署和删除等操作。支持命令如下:

1. 安装

通过 npm 安装 serverless

  1. $ npm install -g serverless

2. 创建

  1. $ mkdir my-function
  2. $ cd my-function

目录内容如下:

  1. |- code
  2. |- index.js
  3. |- serverless.yml

对于该例子可以使用一下 Demo,作为 index.js:

  1. 'use strict';
  2. exports.main_handler = async (event, context, callback) => {
  3. .log("%j", event);
  4. return "hello world"
  5. };

3. 配置

本地创建 serverless.yml 文件,在其中进行如下配置

  1. $ touch serverless.yml
  1. # serverless.yml
  2. myFunction1:
  3. component: '@serverless/tencent-scf'
  4. inputs:
  5. name: myFunction1
  6. codeUri: ./code # 代码目录
  7. handler: index.main_handler
  8. runtime: Nodejs8.9
  9. region: ap-guangzhou
  10. description: My Serverless Function
  11. memorySize: 128
  12. timeout: 20
  13. # 打包zip时希望忽略的文件或者目录配置(可选)
  14. exclude:
  15. - .gitignore
  16. - .git/**
  17. - node_modules/**
  18. - .serverless
  19. - .env
  20. include:
  21. - /Users/dfounderliu/Desktop/temp/.serverless/myFunction1.zip
  22. environment:
  23. variables:
  24. TEST: vale
  25. vpcConfig:
  26. subnetId: ''
  27. vpcId: ''
  28. myFunction2:
  29. component: '@serverless/tencent-scf'
  30. inputs:
  31. name: myFunction2
  32. codeUri: ./code

4. 部署

如您的账号未登陆注册腾讯云,您可以直接通过微信扫描命令行中的二维码进行授权登陆和注册。

通过sls命令进行部署,并可以添加—debug参数查看部署过程中的信息

  1. $ sls --debug
  2. DEBUG Resolving the template's static variables.
  3. DEBUG ─ Collecting components from the template.
  4. DEBUG ─ Downloading any NPM components found in the template.
  5. DEBUG ─ Analyzing the template's components dependencies.
  6. DEBUG Creating the template's components graph.
  7. DEBUG ─ Syncing template state.
  8. DEBUG ─ Starting Website Removal.
  9. DEBUG ─ Removing Website bucket.
  10. DEBUG ─ Removing files from the "my-bucket-1300415943" bucket.
  11. DEBUG ─ Removing "my-bucket-1300415943" bucket from the "ap-guangzhou" region.
  12. DEBUG ─ "my-bucket-1300415943" bucket was successfully removed from the "ap-guangzhou" region.
  13. DEBUG ─ Finished Website Removal.
  14. DEBUG ─ Executing the template's components graph.
  15. DEBUG Compressing function myFunction file to /Users/dfounderliu/Desktop/temp/code/.serverless/myFunction.zip.
  16. DEBUG Compressed function myFunction file successful
  17. DEBUG Uploading service package to cos[sls-cloudfunction-ap-guangzhou-code]. sls-cloudfunction-default-myFunction-1572519895.zip
  18. DEBUG Uploaded package successful /Users/dfounderliu/Desktop/temp/code/.serverless/myFunction.zip
  19. DEBUG Creating function myFunction
  20. DEBUG Created function myFunction successful
  21. myFunction:
  22. Name: myFunction
  23. Runtime: Nodejs8.9
  24. Handler: index.main_handler
  25. MemorySize: 128
  26. Timeout: 3
  27. Region: ap-guangzhou
  28. Role: QCS_SCFExcuteRole
  29. Description: This is a template function
  30. UsingCos: true
  31. 6s myFunction done

5. 移除

  1. $ sls remove --debug
  2. DEBUG Flushing template state and removing all components.
  3. DEBUG Removed function myFunction successful
  4. 1s myFunction done

账号配置(可选)

当前默认支持 CLI 扫描二维码登录,如您希望配置持久的环境变量/秘钥信息,也可以本地创建 .env 文件

  1. $ touch .env # 腾讯云的配置信息

.env 文件中配置腾讯云的 SecretId 和 SecretKey 信息并保存。

  1. # .env
  2. TENCENT_SECRET_ID=123
  3. TENCENT_SECRET_KEY=123

还支持哪些组件?

可以在 Serverless Components 中查询更多组件的信息。