This is the 16th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Many developers have heard of the Serverless Framework at some point. Both with less service architecture, even no server, to achieve the development of the entire project. Developers can deploy complete Serverless applications without having to worry about underlying resources, performance, etc. The scenario of this article is to build and deploy an egg.js application through the Serverless Framework provided by Tencent Cloud.

Application scenarios of Serverless

Development based on cloud functions

The Serverless Framework provided by Tencent Cloud enables developers to develop, deploy, and debug applications through the command line or console. The Serverless Framework combines the cloud function, API gateway, database and other resources of Tencent Cloud to develop and deploy full-stack applications. There are two types of cloud functions of Tencent Cloud, event-based and Web. The former supports multiple triggering modes, such as gateway triggering, timing triggering, COS resource triggering and message middleware, while the latter only supports API gateway triggering at present.

Rapid migration of traditional projects

If you need to quickly migrate an existing project to the Serverless Framework, such as Egg, Koa, etc., you can quickly migrate with just a few lines of code.

Quickly set up and deploy

You need to have a Tencent Cloud account and complete real-name authentication before setting up and deploying the software using the command line. You are advised to read related product documents carefully before setting up and deploying the software.

Confirm that the environment

To install the Serverless Framework provided by Tencent Cloud, you need to make sure that your development environment has a Node10 or higher version and Npm.

$node -v v12.18.0 $NPM -v 7.0.10Copy the code

Install the serverless

npm install -g serverless
Copy the code

Build Egg application

Initialize a project locally according to the official document egg.js, as shown in the following example:

npm init egg --type=simple

npm install
Copy the code

Start the command at the project root to run the project

npm run dev

open http://localhost:7001
Copy the code

Write a serverless. Yml

The Serverless Framework uses the project configuration file serverless.yml to identify application types and configure resources. After the project is developed locally, you must configure the YML file before running the SLS deploy command. The configuration file in serverless. Yml and the specified parameters or code directory in inputs will be passed to the Serverless Components deployment engine to complete the cloud deployment. When starting the deployment command, Serverless automatically creates the related services and deploys them based on the configuration. A basic YML file is as follows:

#serverless.yml
app: expressDemoApp # Application name, default with component instance name
stage: ${env:STAGE} # for isolation of the development environment. Defaults to dev

component: express # (required) The name of the reference Component, currently used for express-Tencent
name: expressDemo # (required) The name of the instance created by the componentInputs: SRC: SRC:./ exclude: -. Env region: AP-Guangzhou Runtime: Nodejs10.15Copy the code

However, only one Serverless application is automatically deployed. You need to configure event-based cloud functions and gateway triggering as follows: Configure cloud functions

functionName: xxx

functionConf:
    eip: false # Fixed egress IP
    timeout: 10 The maximum execution time of the function, in seconds. Optional values range from 1 to 900 seconds. Default is 3 seconds
    memorySize: 128 < span style = "font-size: 14px; font-size: 14px;
Copy the code

Configure the API gateway. The authentication mode is configured as application authentication and the authentication mode is associated with the corresponding app. The APP needs to be created and obtained on the Tencent Cloud API Gateway console.

serviceName: xxx
 
apigatewayConf:
    isDisabled: false Disable automatic API gateway creation. If the default value is no, the API gateway is automatically created by default.
    serviceTimeout: 60 # Api timeout, in seconds
    protocols: # Type of front-end request, such as HTTP, HTTPS, HTTP and HTTPS
      - http
      - https
    environment: release There are three release environments: Test, prepub and Release.
    enableCORS: false. # Enable cross-domain. The default value is no
    autoCreateDns: false
    endpoints:
      - path: /
        method: ANY
        authType: APP
        responseType: JSON
        function:
          isIntegratedResponse: true
          functionQualifier: $LATEST
          functionName: xxx
        app:
          name: xxx
          id: app-xxx
          description: xxx
Copy the code

Command line deployment

Run the following command on the console to deploy the cloud. The system automatically configutes the cloud function and API gateway. After the deployment, you can view the application details and gateway entry on the console.

sls deploy
Copy the code

If Secretid and Secretkey are not configured in.env, then scan authorization is required during deployment. After authorization, temporary Secretid and Secretkey are generated. You can also view specific log information during deployment

sls deploy --debug
Copy the code

Welcome to other articles

  • Internationalizing your website with Vite2+Vue3 | More challenges in August
  • Actual: Front-end interface request parameter confusion
  • Actual: Implement a Message component with Vue3
  • Actual: Vue3 to implement the Image component, by the way, support lazy loading
  • One Piece, Vue.js 3.0 brings what updates
  • This article digests the major new features of ES7, ES8, and ES9
  • Technical team’s common problems and solutions
  • 10 new features commonly used in ES6
  • Vue3’s Script setup grammar sugar is really cool