Deploying Egg.js Framework

Last updated: 2020-07-21 18:32:34

    Operation Scenarios

    Tencent Cloud Egg.js Serverless Component supports deploying RESTful API services.

    Prerequisites

    Note:

    You are recommended to use Node.js 10.0 or above; otherwise, Component v2 may report errors during deployment.

    Initialize Egg.js project

    $ mkdir serverless-egg && cd serverless-egg
    $ npm init egg src --type=simple
    $ cd src && npm install

    Modify Egg.js configuration

    When a function is executed, only /tmp is readable/writable; therefore, you need to write the logs of Egg.js framework running attempts into this directory by modifying the configuration in config/config.default.js to the following:

    const config = (exports = {
      rundir: '/tmp',
      logger: {
        dir: '/tmp'
      }
    })

    Directions

    1. Install

    Use npm to install Serverless CLI globally:

    $ npm install -g serverless

    2. Configure

    Create a serverless.yml file in the project root directory:

    $ touch serverless.yml

    Configure the serverless.yml file as follows:

    # serverless.yml
    
    org: orgDemo
    app: appDemo
    stage: dev
    component: egg@0.0.0-dev
    name: eggDemo
    
    inputs:
      src: ./src
      region: ap-guangzhou
      functionName: eggDemo
      runtime: Nodejs10.15
      apigatewayConf:
        protocols:
          - http
          - https
        environment: release

    Detailed Configuration >>

    3. Deploy

    Deploy by running the sls deploy command, and you can add the --debug parameter to view the information during the deployment process:

    Note:

    sls is short for the serverless command.

    $ sls deploy
    
      MyComponent: 
        region:              ap-beijing
        functionName:        egg-function
        apiGatewayServiceId: service-n5m5e8x3
        url:                 https://service-n5m5e8x3-1251971143.bj.apigw.tencentcs.com/release/
    
      32s › MyComponent › done

    4. Remove

    You can run the following commands to remove the deployed API Gateway and SCF services:

    $ sls remove --debug
    
      DEBUG ─ Flushing template state and removing all components.
      DEBUG ─ Removing function
      DEBUG ─ Request id
      DEBUG ─ Removed function egg-function successful
      DEBUG ─ Removing any previously deployed API. api-cmkhknda
      DEBUG ─ Removing any previously deployed service. service-n5m5e8x3
    
      8s › MyComponent › done
    

    Account configuration (optional)

    Currently, you can scan a QR code to log in to the CLI by default. If you want to configure persistent environment variables/key information, you can also create a local .env file:

    $ touch .env # Tencent Cloud configuration information

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

    # .env
    TENCENT_SECRET_ID=123
    TENCENT_SECRET_KEY=123

    Note:

    • If you don't have a Tencent Cloud account yet, please sign up first.
    • If you already have a Tencent Cloud account, you can get SecretId and SecretKey in API Key Management.

    More Resources

    FAQs

    1. What should I do if the app/public deployment directory does not exist?

      Generally, when you initialize an Egg.js project, the app/public directory will be created automatically. If this directory is empty during packaging and compressing, it will not exist after the deployment; instead, it will automatically be created when the Egg.js project is started; however, SCF will not have the permission to manipulate it. Therefore, you are recommended to create an empty folder .gitkeep in the app/public directory to solve this problem.

    2. What should I do if a failure to find the dependent module is reported after deployment with Layer?

      For more information, please see here.

    More components

    You can view more component information in the repository of Serverless Components.

    Was this page helpful?

    Was this page helpful?

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