Serverless Framework-Multi-function development example

Serverless Framework-Multi-function development example

What is serverless

The definition and understanding of Serverless will be interpreted differently in different perspectives and scenarios. AWS defines Serverless (on the AWS cloud) as "a way to describe services, practices, and strategies, enabling you to build more agile Application, which can innovate faster and respond to changes" is a service. Red Hat believes that Serverless is a development model that "allows developers to focus on building and running applications without managing servers", and further divides Serverless products into two categories: BaaS (back-end as a service, allowing developers to access A variety of third-party services and applications) and FaaS (function as a service, developers write logic, deploy it in a container fully managed by the platform, and then execute it on demand). The Serverless Framework believes that Serverless is "a movement that is promoted by developers and enterprises, allowing a single developer to complete high-traffic application development, while focusing only on the aspects that generate value."

No matter which aspect or perspective, Serverless has the following common characteristics:

  1. Rapid development, rapid deployment
  2. Pay as you go, reduce costs
  3. Automatic expansion, no maintenance

At present, it is implemented based on the FaaS services of various cloud vendors, such as: Tencent Cloud's SCF, AWS's Lambda, Azure Cloud's Azure Funcitons, etc.

What problem does Serverless solve

As computing power increases, system complexity increases, and user scale increases, software problems (also called software crises below) will also increase exponentially.

  • Unpredictable software development progress
  • Software development costs are difficult to control
  • Software product quality cannot be guaranteed
  • Software products are difficult to maintain

On the other hand, Serverless can propose a solution to the software crisis in the following ways:

  • The function of the system is divided into smaller granularities, which is more convenient for design, development, testing and maintenance.
  • Through the billing according to the amount, the overhead cost when resources are idle is greatly reduced, and the server cost is reduced.
  • Through automatic expansion and cloud platform support, the workload of operation and maintenance and software maintenance costs are greatly reduced.

At the same time, in the modern working environment where agile working methods are generally advocated, Serverless also provides the best practices of development methods for rapid verification of ideas and iterative functions, and at the same time, there is no need to worry about code changes that will affect other functions of the system, and there is no need to consider deployment. Server configuration before deployment and maintenance after deployment.

Serverless Framework

Serverless Framework is a very popular serverless application framework in the industry. Through close cooperation with many first-class cloud providers such as Tencent Cloud, AWS, etc., it provides developers with no services that can write and deploy code without caring about the underlying infrastructure. Development experience.

Serverless Framework also provides resource management, auto-scaling, statistical analysis and other capabilities, allowing developers to save operation and maintenance costs, and truly "pay by volume" without having to spend energy on tasks such as log collection and abnormal statistics.

Serverless Framework works closely with Tencent Cloud through CLI tools to provide Chinese users with a complete solution based on serverless components. Covers the entire life cycle of serviceless application coding, testing, deployment, etc., and at the same time suits the usage scenarios and habits of Chinese users.

Why choose Serverless Framework

With just a few lines of configuration files and CLI tools of Serverless Framework, developers can get additional:

  • Function development is performed locally and deployed to the cloud with one click, without additional adaptation of cloud functions, and no need to log in to the console.
  • Support the deployment of traditional development framework applications (such as Express, next.js, Flask, Laravel, etc.) as Serverless applications.
  • Debug the function code locally, or use the remote development mode to view the log output of the deployment service locally and debug it in real time.
  • All infrastructure configurations can be completed through simple configuration (such as: API gateway, COS storage, DB link, etc.)
  • Quickly switch the deployment environment (development, demonstration, production) and region of the application.
  • More detailed and easy understanding of application status, view logs, error statistics and other information.

Multi-function development example

This example uses the multi-function component (multi-scf) of the Serverless Framework and the PostgreSQL component (postgresql) to implement the following 3 API interfaces.

  • GET/todos/
    Get all todo matters
  • POST/todos/
    Create a new todo item
  • POST/todos/{id}/actions/complete
    Complete todo items

And use the invoke and logs functions provided by the Serverless Framework to debug and view real-time logs in the production environment.

The relevant code of this example can be obtained in the Git repository .

Step 1: Install Serverless Framework

Execute the following command to install Serverless Framework

$ Npm install serverless -g copy the code

If you have installed Serverless Framework before, you can upgrade to the latest version with the following command:

$ Npm update serverless -g copy the code

This command will install the latest Serverless Framework to your computer. After the installation is successful, you can use

Start using Serverless Framework

Step 2: Initialize the multi-function project

$ Sls init multi-scf-nodejs --name sls-demo-msn-todo copy the code

This command will use the application template

Initialization named
Application directory. After the initialization is successful, the directory structure is

. index.js serverless.yml Copy code

The purpose of the file here is as follows:

  • index.js: function file.
  • serverless.yml: Serverless Framework configuration file.
    • app: The name of the application, which will serve as the unique identifier for the application.
    • stage: application environment, through different environments, deploy different application instances.
    • component: component name
    • name: component instance name
    • inputs: input parameters for component deployment

Step 3: Link to the database

Because Serverless is stateless (it will be destroyed after running), it is necessary to link the database to persist todo information. Adding a database requires a VPC network connection first.


Create subdirectories

And add a new one in the subdirectory
The files are as follows:

component: vpc # [Required] To use components, please see name: sls-demo-msn-vpc # [Required] Component instance name inputs: region: ap-guangzhou # The region where the instance belongs to zone: ap-guangzhou-2 # The region where the instance belongs vpcName: ${name} # The instance name, where the field name is reused as the name. subnetName: sls-demo-msn-subnet # The name of the subnet Copy code

More VPC configuration content, view the VPC private network for more details information.

In the configuration file of the child component, the app name will automatically inherit the configuration in the serverless.yml of the parent directory. At the same time, the app name of the same application needs to be consistent.

Add database

Create subdirectories

And add a new one in the subdirectory
The files are as follows:

component: postgresql #(required) refers to the name of the component, currently used is the postgresql component name: sls-demo-msn-DB # (required) the name of the instance created by the postgresql component inputs: region: ap-guangzhou # The region where the instance belongs to zone: ap-guangzhou-2 # The region where the instance belongs dBInstanceName: ${name}-${stage} # The database instance name is unique, and the same database can only have the same vpc Inside. extranetAccess: true # Whether to enable instance external network access vpcConfig: # vpc network configuration vpcId: ${output:${stage}:${app}:sls-demo-msn-vpc.vpcId} # private network Id subnetId: ${ Output: Stage} {$: $ {} App: SLS-MSN-Demo-vpc.subnetId} # subnet Id copy the code

Add the database to the vpc network in the database configuration, and use the output variable (output) to dynamically obtain the id information of the vpc.

Configure content more variables, see Serverless variable for more details information.

Configuring PostgreSQL more content to view the PostgreSQL database for more details information.

After the component is deployed, you can use it in the component catalog

sls info
View the output variables of the component, or you can go to the Tencent Cloud application console to view related information.

Initialize the application directory

  1. Create subdirectories
    And will create the generated
    (Renamed to
    ) with
    Move to the directory.
  2. in
    Execute in the directory
    npm init
    Initialize the Node.js project.
  3. in
    Execute in the directory
    npm i pg --save
    Install the database link dependency package
  4. Add a root configuration file in the project root directory
    , The file is as follows:
app: sls-demo-msn-todo-3e5a2134 # The unique identification of the application, which needs to be unique under the same account. stage: dev # Application deployment environment name, here the value of environment variable STAGE is used. Copy code

The configuration file information of the root directory will be inherited by subcomponents, and there is no need to repeat definitions in subcomponents. (Limited to app and stage).

The directory structure of the final project is as follows:

. db # database serverless.yml # Database configuration file serverless.yml src # Multi-function application node_modules package-lock.json package.json # Node.js dependency file serverless.yml # Multi-function application configuration file todos.js # todo application main file vpc # vpc serverless.yml # vpc configuration file Copy code

Modify multi-function application configuration

In the multi-function catalog

Modify the configuration file as follows:

component: multi-scf name: sls-demo-msn the Inputs: src: src: ./ the exclude: - .env - "node_modules/**" Ignore all the files in the directory node_modules # deployment to accelerate deployment Environments: # application environment variable information - Key: PG_CONNECT_STRING value: $ {the Output :${stage}:${app}:sls-demo-msn-DB.private.connectionString} region: ap-guangzhou runtime: Nodejs12.16 memorySize: 128 vpc : # vpc network configuration vpcId: ${output:${ stage}:${app}:sls-demo-msn-vpc.vpcId} # Private network Id subnetId: ${output:${stage}:${app}:sls-demo-msn-vpc.subnetId} # Sub Net Id installDependency: true # Whether online installation depends on timeout: 6 # Default timeout time (seconds) functions: # Multi-function definition allTodo: # Function alias handler: todos.all # Processing function memorySize: 256 # Customize the memory space of the sub-function addTodo: handler: todos .add timeout: 9 # customize this function timeout (in seconds) completeTodo: Handler: todos.comp timeout: 9 the triggers: # configure trigger - of the type: apigw the Parameters: name: todosAPIGW Protocols: - HTTPS - HTTP apis: # API configuration - path: /todos/ # Route path method: GET # Route method function: allTodo # Route processing function alias - path: /todos/ method: POST function: addTodo - path: /todos/{id}/actions/complete method: POST function: completeTodo param: # Dynamic routing parameter configuration - name: id position: PATH required: true type: number desc: Todo ID Copy code

The main changes here are:

  • use
    Depend on automatic installation after enabling deployment and ignore
    All files in the directory (no need to upload node_modules, speed up deployment)
  • use
    Add a vpc network and link to the same vpc network of the project.
  • use
    Add project environment variables and use output to dynamically generate database connection strings.
  • use
    To declare the functions and their aliases in the project.
  • use
    Declare the trigger of the function, and in the trigger
    Configure the path corresponding to each function and parameter information.

More help content development function, see function developed for more details information.

Step 4: develop features


And complete the development of related functions, the final code of the file is as follows:

"use strict" ; const {Client} = require ( "pg" ); const client = new Client({ connectionString : process.env.PG_CONNECT_STRING, }); /** * Initialize the database and table structure */ const initDB = async () => { const isConnected = client && client._connected; if (!isConnected) { await client.connect(); await client.query( ` CREATE TABLE IF NOT EXISTS todo ( ID SERIAL NOT NULL, TITLE VARCHAR NOT NULL, NOTE TEXT, IS_COMPLETE BOOLEAN DEFAULT FALSE );` ); } }; /** * Get all Todo items */ exports .all = async (event, context) => { //async needs to close the event loop waiting to avoid log record timeout or function non-return problems. context.callbackWaitsForEmptyEventLoop = false ; await initDB(); const {rows} = await client.query({ text : "SELECT * FROM todo" }); return { message : "Tencent SCF execute successful!" , data : rows, }; }; /** * Add new Todo items */ exports .add = async (event, context) => { //async needs to close the event loop waiting to avoid the log record timeout or the problem of the function not returning. context.callbackWaitsForEmptyEventLoop = false ; const {title, note} = JSON .parse(event.body); if (!title) { return { statusCode : 400 , message : "Missing Todo Title" , }; } await initDB(); const {rowCount} = await client.query({ text : "INSERT INTO todo (title, note) VALUES($1, $2)" , values : [title, note], }); return rowCount === 1 ? { statusCode : 201 , message : "Todo added success." , } : { statusCode : 400 , message : "Todo added failed." , }; }; /** * Complete designated Todo items */ exports .comp = async (event, context) => { //async needs to close the event loop waiting to avoid the log record timeout or the problem of the function not returning. context.callbackWaitsForEmptyEventLoop = false ; const todoId =; if (!todoId &&! isNaN (todoId)) { return { statusCode : 400 , message : "Missing Todo Id" , }; } await initDB(); const {rowCount} = await client.query({ text : "UPDATE todo SET is_complete = true WHERE id=$1" , values : [todoId], }); return rowCount === 1 ? { statusCode : 200 , message : "Todo Complete success." , } : { statusCode : 400 , message : "Todo Complete failed." , }; }; Copy code

Step 5: Debug function

Invoke debugging

To debug the code, in addition to using third-party development tools to debug through the configured API gateway url, you can also use the Invoke function or remote debugging function of the Serverless Framework. Here, the invoke function is used to demonstrate how to debug function functions.

Invoke and remote debugging functions need to be executed in the component's directory.

Get all Todo

Execute in the src directory

$ Serverless invoke -f allTodo copy the code

You can get the result after execution

Use authorization information in default authorization, if you need to use a temporary key, please use --login to log in again billDuration: 36 duration: 36 errMsg: functionRequestId: fe6d302d-f6db-42ad-9c7b-8d0c61ead9b3 invokeResult: 0 log : "" " START RequestId: fe6d302d-f6db-42ad-9c7b-8d0c61ead9b3 Event RequestId: fe6d302d-f6db-42ad-9c7b-8d0c61ead9b3 END RequestId: fe6d302d-f6db-42ad-9c7b-8d0c61ead9b3 Report RequestId: fe6d302d-f6db-42ad-9c7b-8d0c61ead9b3 Duration:36ms Memory:256MB MemUsage:11.3984MB " "" memUsage: 11952128 --------------------------------------------- Serverless: The call was successful { message: 'Tencent SCF execute successful!' , data: [] } Copy code

The result returned by invoke will contain meta information after the function is executed, such as running time, error, RequestId, execution log, and the result returned by the function.

Create a new Todo

Execute in the src directory

$ serverless invoke -f addTodo --data "{\"body\":\"{\\\"title\\\":\\\"Create multi-scf project demo\\\",\\\"note\\\":\\\"Todo App with postgreSQL\\\"}\"}" Copy code

You can get the result after execution

Use authorization information in default authorization, if you need to use a temporary key, please use --login to log in again billDuration: 35 duration: 35 errMsg: functionRequestId: 93f50016-064f-468d-9e98-31645fc254fd invokeResult: 0 log: """ START RequestId: 93f50016-064f-468d-9e98-31645fc254fd Event RequestId: 93f50016-064f-468d-9e98-31645fc254fd END RequestId: 93f50016-064f-468d-9e98-31645fc254fd Report RequestId: 93f50016-064f-468d-9e98-31645fc254fd Duration:35ms Memory:128MB MemUsage:11.293MB """ memUsage: 11841536 --------------------------------------------- Serverless: The call was successful { statusCode: 201, message:'Todo added success.' } Copy code

Step 6: Deployment and logging

Deploy the code to the production environment

Use the following command to quickly deploy the project to the production environment (here, the production environment is named


$ Serverless deploy --stage prod copy the code

View production environment logs instantly

In the project directory

Execute the following command to view the instant log information of the project

$ Sls logs --tail -f allTodo --stage prod copy the code

The following is the returned result:

Use authorization information in default authorization, if you need to use a temporary key, please use --login to log in again serverless components Action: "logs"-Stage: "prod"-App: "sls-demo-msn-todo-3e5a2134"-Name: "sls-demo-msn" START RequestId:6f31857109130f092c547337c073ea91 Response RequestId:dbb3a8ed63a32be8e6b7a2dd8a32bbe2 RetMsg:{"message":"Tencent SCF execute successful!","data":[{"id":1,"title":"Create multi-scf project demo","note":"Todo App with postgreSQL","is_complete":false}]} END RequestId:dbb3a8ed63a32be8e6b7a2dd8a32bbe2 Report RequestId:dbb3a8ed63a32be8e6b7a2dd8a32bbe2 Duration:4ms Memory:256MB MemUsage:12.113281MB Response RequestId:485a87cfc6ad385b7e9c84343962391b RetMsg:{"message":"Tencent SCF execute successful!","data":[{"id":1,"title":"Create multi-scf project demo","note":"Todo App with postgreSQL","is_complete":false}]} END RequestId:485a87cfc6ad385b7e9c84343962391b Report RequestId:485a87cfc6ad385b7e9c84343962391b Duration:4ms Memory:256MB MemUsage:11.886719MB START RequestId:0ede6d26dca55362a701c10ff51c9021 Serverless Monitoring... Copy code


Thanks to the vast number of developers who have supported the Serverless Framework for a long time. In the future, we will continue to iterate the product, introduce new features, and improve the product experience. Finally, we will create a set of serverless development for Chinese developers that conforms to the habits of Chinese developers. Complete solution.

Also welcome everyone to the Serverless Chinese community to share experiences and ideas, as well as feedback questions and BUG.

Serverless Chinese Community:

Finally, I hope that everyone can participate in our award-winning questionnaire: