本文介绍如何将Egg.js应用部署到函数计算。与传统的部署方法相比,您可以跳过购买机器等步骤,将传统的Egg.js应用一键部署至远端直接用于生产,并且拥有弹性伸缩、按量付费和免运维等特性。

前提条件

您已完成以下操作:

背景信息

Egg.js为企业级框架和应用而生,为开发人员提供了Web开发的核心功能和一套具有高扩展性的插件机制。开发人员按照统一的约定开发应用,从而降低开发和维护成本。关于Egg.js的更多信息,请参见Egg.js

操作步骤

  1. 在目标目录中执行以下命令,初始化项目。
    s init devsapp/start-egg -d start-egg
    -d用于指定项名称,本示例以项目名称start-egg为例,介绍如何将Egg.js迁移到函数计算。
    说明 若已存在该项目则跳过此步骤。
    输出示例:
     Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
    
     devsapp!start-egg@0.1.11.zip file decompression completed
     Do you want to install dependencies? No
     please select credential alias default
    
         _____
        |  ___|
        | |__  __ _  __ _
        |  __|/ _` |/ _` |
        | |__| (_| | (_| |
        \____/\__, |\__, |
               __/ | __/ |
              |___/ |___/
    
    
        Welcome to the start-egg application
         This application requires to open these services:
             FC : https://fc.console.aliyun.com/
             ACR: https://cr.console.aliyun.com/
         This application can help you quickly deploy the Egg project:
             Full yaml configuration: https://github.com/devsapp/egg#%E5%AE%8C%E6%95%B4yaml
             Egg.js development docs: https://start-egg.org/zh-cn/tutorials/index.html
         This application homepage: https://github.com/devsapp/start-egg
    
    
    ‍ Thanks for using Serverless-Devs
     You could [cd /test/start-egg] and enjoy your serverless journey!
    ️ If you need help for this example, you can use [s -h] after you enter folder.
     Document Star:https://github.com/Serverless-Devs/Serverless-Devs
  2. 执行以下命令,进入项目目录:
    cd start-egg
  3. 执行以下命令部署项目:
    s deploy
    输出示例:
    [2021-08-17T14:31:03.383] [INFO ] [S-CLI] - Start ...
    [2021-08-17T14:31:03.392] [INFO ] [S-CLI] - Start the pre-action
    [2021-08-17T14:31:03.392] [INFO ] [S-CLI] - Action: npm install --production
    npm WARN deprecated babel-eslint@8.2.6: babel-eslint is now @babel/eslint-parser. This package will no longer receive updates.
    npm WARN deprecated debug@3.2.6: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (https://github.com/visionmedia/debug/issues/797)
    npm WARN deprecated mkdirp@0.5.4: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
    npm WARN deprecated core-js@2.6.12: core-js@<3.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Please, upgrade your dependencies to the actual version of core-js.
    npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
    npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
    [            ......] - extract:component-emitter: sill extract component-emitter@^1.2.1 extracted to /test/start-egg/node_modules/.staging/component-emitter-009e97c5 (1358ms)
    [            ......] - extract:component-emitter: sill extract component-emitter@^1.2.1 extracted to /test/start-egg/node_modules/.staging/component-emitter-009e97c5 (1358ms)
    npm notice created a lockfile as package-lock.json. You should commit this file.
    npm WARN co-mocha@1.2.2 requires a peer of mocha@>=1.18 <6 but none is installed. You must install peer dependencies yourself.
    npm WARN acorn-jsx@5.3.2 requires a peer of acorn@^6.0.0 || ^7.0.0 || ^8.0.0 but none is installed. You must install peer dependencies yourself.
    
    added 552 packages from 387 contributors in 50.881s
    
    14 packages are looking for funding
      run `npm fund` for details
    
    [2021-08-17T14:31:55.200] [INFO ] [S-CLI] - End the pre-action
     devsapp!egg@0.1.7.zip file decompression completed
     Using web framework type: nas, If you want to deploy with container, you can [s cli fc-default set web-framework container] to switch.
    [2021-08-17T14:32:10.242] [WARN ] [EGG] - The startup file is not found, create a [start.sh] as the startup file.
     core-0.0.146.tgz file decompression completed
     Using web framework type: nas, If you want to deploy with container, you can [s cli fc-default set web-framework container] to switch.
     End of request
     Deployed.
     End of request
    (node:18629) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
    (Use `node --trace-deprecation ...` to show where the warning was created)
    [2021-08-17T14:32:22.474] [INFO ] [FC-DEPLOY] - Using region: cn-shenzhen
    [2021-08-17T14:32:22.474] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-08-17T14:32:22.475] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-08-17T14:32:22.475] [INFO ] [FC-DEPLOY] - Using accessKeySecret: ldHUiJLoGto****
     Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    [2021-08-17T14:32:23.193] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-egg exists
    [2021-08-17T14:32:23.637] [INFO ] [FC-DEPLOY] - Setting role: fcDeployDefaultRole-serverless-devs-egg
    [2021-08-17T14:32:24.423] [INFO ] [RAM] - Checking Role fcDeployDefaultRole-serverless-devs-egg exists
    [2021-08-17T14:32:24.585] [INFO ] [RAM] - Creating role: fcDeployDefaultRole-serverless-devs-egg
    [2021-08-17T14:32:24.685] [INFO ] [RAM] - Checking Plicy AliyunECSNetworkInterfaceManagementAccess exists
    [2021-08-17T14:32:24.755] [INFO ] [RAM] - Checking Plicy AliyunContainerRegistryReadOnlyAccess exists
    [2021-08-17T14:32:25.056] [INFO ] [FC-DEPLOY] - Using vpcConfig: auto: fc will try to generate related vpc resources automatically
    [2021-08-17T14:32:26.375] [INFO ] [VPC] - Getting vpc: fc-deploy-component-generated-vpc-cn-shenzhen
    [2021-08-17T14:32:26.477] [INFO ] [VPC] - Getting vswitch: fc-deploy-component-generated-vswitch-cn-shenzhen
    [2021-08-17T14:32:26.636] [INFO ] [VPC] - Getting securityGroup: fc-deploy-component-generated-securityGroup-cn-shenzhen
    [2021-08-17T14:32:26.842] [INFO ] [FC-DEPLOY] - Generated vpcConfig:
    securityGroupId: sg-wz92ctigpi221smc****
    vSwitchId: vsw-wz9qnuult4q5g4f7n****
    vpcId: vpc-wz9x9bzs0wtvjgt6n****
    
    [2021-08-17T14:32:26.843] [INFO ] [FC-DEPLOY] - Using nasConfig: auto: fc will try to generate related nas file system automatically
     upload done
    [2021-08-17T14:32:53.524] [INFO ] [FC-DEPLOY] - Generated nasConfig:
    groupId: 10003
    mountPoints:
      - fcDir: /mnt/auto
        nasDir: /serverless-devs-egg
        serverAddr: example.com
    userId: 10003
    
    [2021-08-17T14:32:53.721] [INFO ] [FC-DEPLOY] - Checking Function egg exists
    [2021-08-17T14:32:54.014] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-egg exists
    [2021-08-17T14:32:54.233] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-egg exists
    [2021-08-17T14:32:54.408] [INFO ] [FC-DEPLOY] - Creating service: serverless-devs-egg
    [2021-08-17T14:32:54.409] [INFO ] [FC-DEPLOY] - Creating function: egg
    [2021-08-17T14:32:54.409] [INFO ] [FC-DEPLOY] - Creating triggers: ["serverless-devs-egg"]
     Make service serverless-devs-egg success.
     Make function serverless-devs-egg/egg success.
     Make trigger serverless-devs-egg/egg/serverless-devs-egg success.
    [2021-08-17T14:32:58.095] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-egg exists
    [2021-08-17T14:32:58.617] [INFO ] [FC-DEPLOY] - Checking Function egg exists
    [2021-08-17T14:32:59.122] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-egg exists
    [2021-08-17T14:32:59.336] [INFO ] [FC-DEPLOY] - Creating custom domain: egg.serverless-devs-egg.188077086902****.cn-shenzhen.fc.devsapp.net
    [2021-08-17T14:33:00.983] [INFO ] [FC-DOMAIN] - Creating custom domain: egg.serverless-devs-egg.188077086902****.cn-shenzhen.fc.devsapp.net
    
    There is auto config in the service: serverless-devs-egg
    [2021-08-17T14:33:01.494] [INFO ] [WEB-FRAMEWORK] - nas component get src is: .
     upload done
    Packing ...
    file /test/start-egg/.s/zip is ignored.
    Package complete.
     Start uploading file: /mnt/2bfb748534-tpm20-cn-shenzhen/serverless-devs-egg/egg/start-egg.zip
     File uploaded successfully: /mnt/2bfb748534-tpm20-cn-shenzhen/serverless-devs-egg/egg/start-egg.zip
     upload done
    [2021-08-17T14:33:58.326] [INFO ] [FC-NAS] - unzipping file
    [2021-08-17T14:34:10.528] [INFO ] [FC-NAS] - unzip done
     Try container acceleration
    function-test:
      region: cn-shenzhen
      serviceName: serverless-devs-egg
      functionName: egg
      customDomains:
        - http://egg.serverless-devs-egg.188077086902****.cn-shenzhen.fc.devsapp.net

执行结果

成功部署该项目后,您可以在执行输出中查看到函数计算生成的临时域名,通过该临时域名可以访问刚部署的应用,例如执行以下命令访问应用:

curl http://egg.serverless-devs-egg.188077086902****.cn-shenzhen.fc.devsapp.net
说明 临时域名仅用作演示以及开发,具有时效性。如需用作生产,请绑定已经在阿里云备案的域名。详细信息,请参见绑定自定义域名