Tencent Cloud SCF uses Tencent Serverless Framework. Based on serverless services (functions, triggers, etc.) in the cloud, it can implement "zero" configuration, convenient development, and rapid deployment of your first function. It supports a rich set of configuration extensions and provides the easiest-to-use, low-cost, and elastically scalable cloud-based function development, configuration, and deployment capabilities.
sls init
command for creation. You can quickly create an SCF function in Node.js as follows:sls init scf-nodejs
Note:
The
helloworld
templates currently supported by the SCF component arescf-golang
,scf-php
, andscf-python
. You only need to replacescf-nodejs
in the command with the template name listed above to quickly initialize a template in the corresponding language.
Run the following command, and a QR code will pop up. Directly scan the code to authorize for deployment:
sls deploy
Note:
If authentication fails, please authorize as instructed in Account and Permission Configuration.
Run the following command to view the information of the deployed project:
sls info
Run the following command to remove the deployed project:
sls remove
When sls deploy
is executed, function resources will be created or updated according to the configuration in the serverless.yml
file. The following is a simple serverless.yml
file:
Note:
For more information on the configuration, please see the configuration documentation.
# SCF component configuration sample
# For all configuration items, please visit https://github.com/serverless-components/tencent-scf/blob/master/docs/configure.md.
# Component information
component: scf # Name of the imported component, which is required. The `tencent-scf` component is used in this example
name: scfdemo # Name of the created instance, which is required. Replace it with the name of your instance
# Component parameters
inputs:
name: ${name}-${stage}-${app} # Function name
src: ./ # Code path
handler: index.main_handler # Entry
runtime: Nodejs10.15 # Function runtime environment
region: ap-guangzhou # Function region
events: # Trigger
- apigw: # Gateway trigger
parameters:
endpoints:
- path: /
method: GET
Information in the serverless.yml
file:
Component | Required | Description |
---|---|---|
component | Yes | Component name. You can run sls registry to query components available for import. |
name | Yes | Name of the created instance. An instance will be created when each component is deployed. |
Parameters in inputs
are component configuration parameters. The parameters of a simplest SCF component are as detailed below:
Parameter | Description |
---|---|
name | Function name. As it is also the resource ID, to ensure the uniqueness of the resource, we recommend you use the ${name}-${stage}-${app} variable as the name. |
src | Code path. |
handler | Function handler name. |
runtime | Function runtime environment. Valid values: Python2.7, Python3.6, Nodejs6.10, Nodejs8.9, Nodejs10.15, Nodejs12.16, PHP5, PHP7, Go1, Java8, CustomRuntime. |
region | Function region. |
events | Trigger. Valid values: timer, apigw, cos, cmq, ckafka. |
When deploying an instance, you need to grant the corresponding account permissions to manipulate specific Tencent Cloud resources. Currently, you can authorize by scanning the code or with a key.
SecretId
and SecretKey
of the account in advance.For more information on the configuration, please see Account and Permission Configuration.
You can run sls dev
in the directory of the serverless.yml
file to output cloud logs in real time. After each deployment, you can access the project to output invocation logs in real time on the command line, which makes it easy for you to view business conditions and troubleshoot issues. Node.js allows you to enable the development debugging feature, which can detect and automatically upload changes in the local code. For more information, please see Development Mode and In-cloud Debugging.
Deployment of a component instance in Serverless Framework is actually deployment of a single-component instance application.
During the development of an application project, there may be multiple component instances under the same application. For detailed directions on how to manage component instances for application project development, please see Application Management.
The SCF component provides component-level commands. A function successfully deployed by running the sls deploy
command can be manipulated with the following commands.
Note:
The command must be executed in the same directory as
serverless.yml
.
Publish the $LATEST
version of the my-function
function as a fixed version:
sls publish-ver --inputs function=my-function
Create the routing-alias
alias for the my-function
function, with the routing rule of 50% traffic for version 1 and 50% traffic for version 2:
sls create-alias --inputs name=routing-alias function=my-function version=1
config='{"weights":{"2":0.5}}'
Update the flow rule of the routing-alias
alias of the my-function
function to 10% for version 1 and 90% for version 2:
sls update-alias --inputs name=routing-alias function=my-function version=1 config='{"weights":{"2":0.9}}'
List the routing-alias
alias of the my-function
function:
sls list-alias --inputs function=my-function
Delete the routing-alias
alias of the my-function
function:
sls delete-alias --inputs name=routing-alias function=my-function
Invoke the functionName
function and pass the JSON parameter {"weights":{"2":0.1}}
:
sls invoke --inputs function=functionName clientContext='{"weights":{"2":0.1}}'
Was this page helpful?