Quick Start

Last updated: 2020-04-12 18:44:01

Scenario

The job guides you to deploy a service on Tencent Cloud using Serverless Framework open source CLI, and complete the steps of configuration, creation, testing, deployment and so on.

  • Resources created by Serverless Framework can be viewed and managed in the console of the resource itself, such as Cloud function consoleAPI Gateway console etc.
  • , Serverless Framework is expected to provide a visual page in March 2020, allowing you to view and manage resources from the perspective of Serverless applications.

Prerequisites

Before using, make sure that the following software is installed:

If these conditions have been met, you can skip this step and directly Start deploying a service .

Install Node.js and NPM

  • Refer to Node.js installation Guide Install according to your system environment.
  • After installation, pass the node -v Command to view the installed Node.js version information:
$ node -v
vx.x.x
  • Approve npm -v Command to view the installed npm version information:
$ npm -v
x.x.x

Install Serverless Framework CLI

  • Run the following command on the command line:
npm install -g serverless
  • After installation, run the serverless -v Command to view the version information of Serverless Framework CLI.
$ serverless -v
x.x.x

Directions

After completing the above installation preparations, start deploying a Serverless service by following these steps.

Create a service from a template

  1. Using Serverless Framework tencent-nodejs The template creates a new service.
    Create it by running the following command, --path You can specify the path to the service:
# Create a serverless service
$ serverless create --template tencent-nodejs --path my-service
  1. Installation dependency.
    Enter the path where the service is located, and run the following command to install the dependency:
$ cd my-service
$ npm install

Configure a trigger
The cloud function needs to be triggered by the event call of the trigger, so it can be triggered in the serverless.yml Add the configuration of triggers in. Take API gateway triggers as an example, the configuration is as follows:

service: my-service # service name

provider: # provider information
  name: tencent
  Runtime: Nodejs6.10
  credentials: ~/credentials  #If you do not use the QR code to log in with one click, the key field needs to be the same as the path of the credentials file.

plugins:{
  - serverless-tencent-scf

"Functions": [
  hello_world:   # Function name
    Handler: index.main_handler
    Runtime: Nodejs6.10
    Events
        "apigw:*"
           > APIGW - hello_world_apigw:
           parameters:
             StageName: release
             ServiceId
             HttpMethod: ANY
  • You can use Detailed configuration document , view serverless.yml A list of properties for all available attributes in the.
  • Serverless Framework adds a prefix to the function actually deployed in the console to form the function name, and the prefix specification is service-stage-function The default stage is dev . Taking the above configuration as an example, the function name in the configuration file hello_world The function name in the console corresponds to my-service-dev-hello_world .

Deployment service

Use this command to deploy or update the functions and triggers you create, and the resource configuration will be the same as the serverless.yml Be consistent in.

serverless deploy

If your account number is not Login or Sign in Tencent Cloud, after running the command, you can directly use the WeChat Scan the QR code that pops up in the command to authorize login and registration of the cloud account.

For more information, see Security Setting of Sub-accounts/Collaborators .

Testing the service

Replace the link address in the following command and test it through curl, which can be found in the sls deploy Get it after the command is executed.

$ curl -X POST https://service-xxxx-1300000000.ap-guangzhou.apigateway.myqcloud.com/release/

Cloud call

Call the function in the cloud with the following command and get the return of the log information.

serverless invoke -f hello_world

See MFA Devices .

Get function log

Open a command line separately, enter the following command, call the function in the cloud again, and get the function in real time hello_world The cloud call log information of

serverless logs -f hello_world -t

Remove service

If you no longer need this service, you can remove it with one click of the following command, which cleans up the corresponding function and trigger resources.

serverless remove

Was this page helpful?

Was this page helpful?

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