---
# System prepended metadata

title: Serverless Tencent Feature
tags: [functions, ' feature', ' blocked']

---

---
tags: functions, feature, blocked
author: Oliver.W
---

> Leave for future redisn and update. currently only sove the multiple func dev issue and serverless commands with components. 


// name: SCF-Components
// name: ???

# Serverless Tencent Feature

## Problem to Solve:

1. Users in China can't benefit the functional dev experience from serverless framework as the Users use serverless(aws). 
    * SCF components that we provide for users to do function development, not support define multiple funcstions in a project.
    * We are missing support for functions developers, to invoke function, and check logs.  

### Things need to consider

*  As the infrastructure difference, Tencent can't support the plugin features. 
    *  serverless-tencent no need to support plugins, bug we can nativly have the popular one build in serverless-tencent. 
    *  Based on the analytic result from global, there are the top popular plugin for serverless: serverless-offline, serverless-webpack, serverless-prune-plugin, serverless-python-requirements, serverless-domain-manager

*  All the serverless feature are build with components, and we need to rebuild a new components to implement the similiar experience of Serverless function 
    *  This is just the backend implementation way, but we don't need to let user know and use it as a compoents. (described on problem 4 above.)

## Key Result:

* User should be able to deploy multiple functions in a SCF component instance.
* User should be able to define multiple functions easily in the SCF components yaml file.
* The function should support to trigger with http, cmq, cos, kafka, timer.

### Use Scenarios
```
Feature: SCF devlopemtn with serverless-function

    Scenario: Deploy with one function only
        Given there a serverless-tencent project
        and there are only one functions defined in serverless.yml
        When deploy to tencent SCF
        Then There will have one SCF instances to the functions defined in serverless.yml
        
        Given there a serverless-tencent project
        And there are only one functions defined in serverless.yml
        And there are other components defined inthe serverless.yml
        When deploy to tencent SCF
        Then there will have one SCF instances to the functions defined in serverless.yml
        And there will have one instance of each components defined in serverless.yml
        
        
    Scenario: Deploy with multiple functions
        Given there a serverless-tencent project
        And there are multiple functions defined in serverless.yml
        When deploy to tencent SCF
        Then There will have multipl SCF instances to the functions defined in serverless.yml
        
        Given there a serverless-tencent project
        And there are multiple functions defined in serverless.yml
        And there are other components defined inthe serverless.yml
        When deploy to tencent SCF,
        Then There will have multipl SCF instances to the functions defined in serverless.yml
        And there will have one instance of each components defined in serverless.yml
        
    Scenario: Use services with components in app
        Given there a serverless app project
        And there are three components defined in three folders.
        And there are two services and each service defined two functions in two folders.
        When deploy to tencent SCF,
        Then There will have seven SCF instances.
        
    Scenario: User will serverless CLI to interact with serverless-tencent
        Given there a serverless-tencent project
        When user type sls and any command
        Then the commands from serverless should works for serverless-tencent (create, deploy, remove, etc.)
```

### Acceptance Criteria

* SCF components support do define and deploy multiple func in one time.

* SCF function support `invoke` and `log` commands with parameters below.

* Commands only show and allow to execute unser SCF commands folder.

#### Support Commands

* invoke
    - `--function` 或 `-f` 要调用的函数名称。
    - `--stage` 或 `-s` 要调用的函数的环境名称。
    - `--region` 或 `-r` 要调用的函数的环境的地区名称。
    - `--data` 或 `-d` 要传递给调用函数的序列化事件数据(String)
    - `--path` 或 `-p` 要传递给调用函数的 json 文件所在路径(函数服务根目
* logs
    - `--function` or `-f` 要查看日志的函数名称。
    - `--stage` or `-s` 要查看日志的函数的环境名称。
    - `--region` or `-r` 要查看日志的函数的环境的地区名称。
    - `--startTime` 查看日志的开始时间戳 (如: 2019-7-12 00:00:00 ).
    - `--tail` or `-t` 查看最新的日志，并持续监听。
    - `--interval` or `-i` 查看最新日志的监听间隔。

> https://github.com/serverless/serverless/blob/ae67bf448950b199eaf2a0ec406349d63f60424c/lib/plugins/aws/logs.js#L74  
> --interval  如果无法实现，可以考虑使用dev模式支持


---
## Proposed Solution:

### Config File
Add `functions` field in SCF components and use it to help define multiple functions on Tencent Cloud.

The proposed serverless-tencent config example

> serverless example config: https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/
> 

```yml
app: appDemo
stage: dev
component: multi-scf
name: multi-scf-demo

inputs:
  src:
    src: ./ # 指定当前需要上传的包含工作流配置文件的目录
    exclude:
      - .env
  region: ap-guangzhou # 云函数所在区域
  type: event # 函数类型，event - 事件类型，web - web 类型
  runtime: Nodejs12.16
  namespace: default
  memorySize: 128
  timeout: 3  
  functions:
    index:  # $ 1, $ latest... 
        # sls invoke local --function app-index >>> app.index
        name: myscf-app-index-${stage}
        handler: app.index
    app-userList:
        name: scf-app-userlist-{stage} # {name}-{stage}-{name}-{app} : app-index-dev-multi-scf-demo-appDemo
        handler: app.userList
        memorySize: 256
        timeout: 10
  triggers:
    - type: timer
      function:
        alias: app-index
      parameters:
        name: timer1
        cronExpression: '*/5 * * * * * *' # 每5秒触发一次
        enable: true
        argument: argument # 额外的参数
    - type: cos
      function:
        alias: app-index
      parameters:
        name: cos1
        bucket: bucket-name
        filter:
          prefix: filterdir/
          suffix: .jpg
        events: 'cos:ObjectCreated:*'
        enable: true
    - type: apigw
      parameters:
        name: serverless
        id: service-xxx # 如果不配置，会自动创建, 指定已有的网关
        apis:
          - path: /
            method: GET
            function: app-index@v1
          - path: /{uid}
            method: POST
            param:
              - name: uid
                position: PATH
                required: true
                type: string
                defaultValue: 0
                desc: user id
            function:
              app-userList:
                  qualifier: xxx
    - type: cmq
      function:
        name: app-index
      parameters:
        name: test-topic-queue
        enable: true
        filterType: 1
        filterKey:
          - key1
          - key2
    - type: ckafka
      function:
        name: app-index
      parameters:
        name: ckafka-xxx
        topic: test
        maxMsgNum: 999
        retry: 10000
        offset: latest
        timeout: 60
        enable: true
    - type: cls
      function:
        name: app-index
      parameters:
        topicId: 'xxx-228b-42f5-aab5-7f740cc2fb11' # 日志主题 ID
        maxWait: 60 # 最长等待时间，单位秒
        enable: true
    - type: mps
      function:
        name: app-index
        namespace: default
        qualifier: $DEFAULT # 别名配置
      parameters:
        type: EditMediaTask # 事件类型
        enable: 

```

### Support Commands


* deploy
    - `--config` 或 `-c` 指定你的配置文件，默认为 serverless.yml|.yaml|.js|.json。
    - `--stage` 或 `-s` 想要部署函数服务的环境名称。
    - `--region` 或 `-r` 想要部署函数所在环境的地区名称。
    - `--package` 或 `-p` 打包前的路径，并跳过打包步骤。
    - `--force` 强制部署，强制部署时 trigger 也会更新。
    - `--function` 或 `-f` 调用 `deploy function`
* deploy function
    - `--stage` 或 `-s` 要查看信息的环境名称。
    - `--region` 或 `-r` 要查看信息的环境的地区名称。
* deploy list
    - `--stage` 或 `-s` 要查看信息的环境名称。
    - `--region` 或 `-r` 要查看信息的环境的地区名称。
* invoke
    - `--function` 或 `-f` 要调用的函数名称。
    - `--stage` 或 `-s` 要调用的函数的环境名称。
    - `--region` 或 `-r` 要调用的函数的环境的地区名称。
    - `--data` 或 `-d` 要传递给调用函数的序列化事件数据(String)
    - `--path` 或 `-p` 要传递给调用函数的 json 文件所在路径(函数服务根目
* logs
    - `--function` or `-f` 要查看日志的函数名称。
    - `--stage` or `-s` 要查看日志的函数的环境名称。
    - `--region` or `-r` 要查看日志的函数的环境的地区名称。
    - `--startTime` 查看日志的开始时间戳 (如: 2019-7-12 00:00:00 ).
    - `--tail` or `-t` 查看最新的日志，并持续监听。
    - `--interval` or `-i` 查看最新日志的监听间隔。
* info
    - `--stage` or `-s` 要查看函数服务的环境名称。
    - `--region` or `-r` 要查看函数服务的环境的地区名称。
* remove
    - `--stage` or `-s` 要移除服务的环境名称。
    - `--region` or `-r` 要移除服务的环境的地区名称。

## Launch Plan

### Phase 1: by 30th April

- [ ] user should can create/install, deploy, remove serverless-tencent poroject
- [ ] user should can check serverless-tenent info.
- [ ] user should get notify for unsupport config / commands

### Phase 2: by 7th May

- [ ] user should can deploy by function.
- [ ] user should can check function list.
- [ ] user should can invoke function

### Phase 3: by 14th May

- [ ] user should can check log with interval
- [ ] user should can check metrics inso

## Action items:

- [ ] Design and build the compoents. 
- [ ] Design and build the config transform. 
- [ ] Update serverless CLI
    - [ ] interact with serverless-tencent components
    - [ ] show chinese help content
- [ ] Add test cases to cover the use cases.