tencent cloud

Feedback

Deploying Web Function on Command Line

Last updated: 2021-07-21 17:12:48

    Overview

    Web function is a new function capability in SCF. Compared with event function that has limits on the event format, web function focuses on optimization of web service scenarios and can directly send HTTP requests to URLs to trigger function execution. For more information, please see Function Overview.

    The Serverless Framework SCF component now supports deploying web functions; therefore, you can use it to quickly create and deploy web functions.

    Directions

    1. Run the following command to initialize the serverless web function template.

      sls init http-demo
      
    2. Enter the demo project and view the directory structure as shown below:

      . http-demo
      ├── serverless.yml  # Configuration file
      ├── package.json # Dependency file
      ├── scf_bootstrap # Project bootstrap file
      └── index.js # Service function
      

      Here, scf_bootstrap is the project bootstrap file. For the specific writing rules, please see Bootstrap File Description.

    3. Open serverless.yml to view the configuration information.
      You only need to add the type parameter in yml to specify the function type and deploy the web function.

      Note
      • For web functions, there is no need to specify the entry function.
      • If the type parameter is not entered, the function will be an event function by default.
      • If there is no scf_bootstrap bootstrap file in the local code, you can specify the entryFile parameter in yml to specify the entry function, and the component will generate a default scf_bootstrap file for you to complete the deployment based on the runtime language. After the deployment is completed, you need to modify the content of the scf_bootstrap file in the SCF console according to the actual needs of your project.
      Below is a sample yml file:
      component: scf
      name: http
      inputs:
      src:
      src: ./
      exclude:
      - .env
      # Specify web type as the function type
      type: web
      name: web-function
      region: ap-guangzhou
      runtime: Nodejs12.16
      # For Node.js, you can enable automatic dependency installation
      installDependency: true
      events:
      - apigw:
      parameters:
      protocols:
      - http
      - https
      environment: release
      endpoints:
      - path: /
      method: ANY
    4. In the root directory, run sls deploy to complete the service deployment. Below is a sample:

      $ sls deploy
      serverless ⚡components
      Action: "deploy" - Stage: "dev" - App: "http" - Name: "http"
      type:         web
      functionName: web-function
      description:  This is a function in http application
      namespace:    default
      runtime:      Nodejs12.16
      handler:      
      memorySize:   128
      lastVersion:  $LATEST
      traffic:      1
      triggers: 
      - 
      NeedCreate:  true
      created:     true
      serviceId:   service-xxxxxx
      serviceName: serverless
      subDomain:   service-xxxxxx.cd.apigw.tencentcs.com
      protocols:   http&https
      environment: release
      apiList: 
        - 
          path:            /
          method:          ANY
          apiName:         index
          created:         true
          authType:        NONE
          businessType:    NORMAL
          isBase64Encoded: false
          apiId:           api-xxxxxx
          internalDomain:  
          url:             https://service-xxxx.cd.apigw.tencentcs.com/release/
      18s › http › executed successfully
      

    Relevant Commands

    Viewing access log

    Similar to event function, you can directly run the sls log command to view the latest 10 logs of the deployed function. Below is a sample:

    $ sls log
    serverless ⚡components
    Action: "log" - Stage: "dev" - App: "http" - Name: "http"
    - 
     requestId:   xxxxx
     retryNum:    0
     startTime:   1624262955432
     memoryUsage: 0.00
     duration:    0
     message: 
       """
       """
    - 
     requestId: xxxxx
     retryNum:    0
     startTime:   1624262955432
     memoryUsage: 0.00
     duration:    0
     message: 
       """
       """
    

    Testing service

    • Scheme 1: directly open the output path URL in a browser, and if it can be accessed normally, the function is successfully created, as shown below:
    • Scheme 2: use other HTTP testing tools such as CURL and Postman to test the web function you have successfully created. Below is a sample test with CURL:
      curl https://service-xxx.cd.apigw.tencentcs.com/release/
      

    Deleting service

    Run the following command to remove your deployed cloud resources.

    sls remove
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support