SCF Component

Last updated: 2020-02-25 14:26:08

Scenario

Cloud function SCF component is one of the basic components in serverless-tencent component library. Through the cloud function SCF component, you can quickly and easily create, configure and manage Tencent Cloud's cloud function SCF.

Directions

Through the SCF component, you can complete the creation, configuration, deployment and deletion of a cloud function. The following commands are supported:

Installation

Install Serverless: through npm
Console

 $npm install-g serverless

Creating


$mkdir my-function 
 $cd my-function

The contents of the directory are as follows:


## Code

|- index.js
|- serverless.yml

For this example, you can use the following Demo, as the index.js:

'use strict';
exports.main_handler = (event, context, callback) => {
    console.log(event)
    return "hello world";
};

Configuration

Create locally serverless.yml File:

Console
$touch serverless.yml

Configure the following in `serverless.yml`:
```yml
# serverless.yml
myFunction1:
  component: "@serverless/tencent-scf"
  #### inputs
    name: myFunction1
    codeUri: ./code       # Code directory
    Handler: index.main_handler
    Runtime: Nodejs6.10
    region: ap-guangzhou
    description: My Serverless Function
    "MemorySize": 128,
    "Timeout": 3,
    # File or directory configuration that you want to ignore when packaging zip (optional)
    exclude:
      - .gitignore
      - .git/**
      - node_modules/**
      ### **Serverless**
      env:
    include
          - /Users/dfounderliu/Desktop/temp/.serverless/myFunction1.zip
    ## Environment
      "Variables": []
        TEST: vale
    ## VpcConfig
      "SubnetId": "",
      vpcId

myFunction2:
  component: "@serverless/tencent-scf"
  #### inputs
    name: myFunction2
    codeUri: ./code

You can use Detailed configuration document , view serverless.yml A list of properties for all available attributes in the.

Automatic Deployment

If your account number is not Login or Sign in Tencent Cloud, you can go directly through WeChat Scan the QR code on the command line for authorized login and registration.

Approve sls Command to deploy, and you can add --debug Parameter to view information during deployment:

Console

 $sls-debug

DEBUG Resolving the template's static variables. 
 DEBUG Collecting components from the template. 
 DEBUG Downloading any NPM components found in the template. 
 DEBUG Analyzing the template's components dependencies. 
 DEBUG Creating the template's components graph. 
 DEBUG Syncing template state. 
 DEBUG Starting Website Removal. 
 DEBUG Removing Website bucket. 
 DEBUG Removing files from the "my-bucket-1300415943" bucket. 
 DEBUG Removing "my-bucket-1300415943" bucket from the "ap-guangzhou" region. 
 DEBUG "my-bucket-1300415943" bucket was successfully removed from the "ap-guangzhou" region. 
 DEBUG Finished Website Removal. 
 DEBUG Executing the template's components graph. 
 DEBUG Compressing function myFunction file to / Users/dfounderliu/Desktop/temp/code/.serverless/myFunction.zip. 
 DEBUG Compressed function myFunction file successful 
 DEBUG Uploading service package to cos [ Sls-cloudfunction-ap-guangzhou-code]. Sls-cloudfunction-default-myFunction-1572519895.zip 
 DEBUG Uploaded package successful / Users/dfounderliu/Desktop/temp/code/.serverless/myFunction.zip 
 DEBUG Creating function myFunction 
 DEBUG Created function myFunction successful

MyFunction: 
 Name: myFunction 
 Runtime: Nodejs6.10 
 Handler: index.main_handler 
 "MemorySize": 128, 
 "Timeout": 3, 
 & Region=ap-guangzhou 
 Role: QCS_SCFExcuteRole 
 Description: This is a template function 
 UsingCos: true

6s > myFunction > done

Remove

Console

$ sls remove --debug

  DEBUG ─ Flushing template state and removing all components.
  DEBUG ─ Removed function myFunction successful

  1s › myFunction › done

Account configuration (optional)

Currently, CLI scanning QR code login is supported by default. If you want to configure persistent environment variable / key information, you can also create it locally. .env File:

Console

 $touch .env # configuration information of Tencent Cloud

Configure Tencent Cloud's SecretId and SecretKey information in the env file and save it.


env: 
 TENCENT_SECRET_ID=123 
 TENCENT_SECRET_KEY=123

Was this page helpful?

Was this page helpful?

  • Not at all
  • Not very helpful
  • Somewhat helpful
  • Very helpful
  • Extremely helpful
Send Feedback
Help