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

前提条件

您已完成以下操作:

操作步骤

  1. 执行以下命名初始化项目。
    s init devsapp/start-hapi -d start-hapi
    -d用于指定项目名称,本示例以指定项目名称start-hapi为例介绍如何迁移Hapi到函数计算。
    说明 若已存在该项目则跳过此步骤。
    输出示例:
     Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
    
     devsapp!start-hapi@0.0.12.zip file decompression completed
     please select credential alias default
    
         _   _             _
        | | | |           (_)
        | |_| | __ _ _ __  _
        |  _  |/ _` | '_ \| |
        | | | | (_| | |_) | |
        \_| |_/\__,_| .__/|_|
                    | |
                    |_|
    
    
        Welcome to the start-hapi 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 Hapi project:
             Full yaml configuration: https://github.com/devsapp/hapi#%E5%AE%8C%E6%95%B4yaml
             Hapi development docs: https://hapi.dev/api
         This application homepage: https://github.com/devsapp/start-hapi
    
    
     Thanks for using Serverless-Devs
     You could [cd /test/start-hapi] 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-hapi
  3. 执行以下命令,部署项目。
    s deploy
    输出示例:
    [2021-08-30T14:11:37.634] [INFO ] [S-CLI] - Start ...
    [2021-08-30T14:11:37.644] [INFO ] [S-CLI] - Start the pre-action
    [2021-08-30T14:11:37.644] [INFO ] [S-CLI] - Action: npm install --production
    npm WARN deprecated hapi@18.1.0: This version contains severe security issues and defects and should not be used! Please upgrade to the latest version of @hapi/hapi or consider a commercial license (https://github.com/hapijs/hapi/issues/4114)
    npm WARN deprecated boom@7.3.0: This module has moved and is now available at @hapi/boom. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated hoek@6.1.3: This module has moved and is now available at @hapi/hoek. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated call@5.0.3: This module has moved and is now available at @hapi/call. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated accept@3.1.3: This module has moved and is now available at @hapi/accept. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated catbox@10.0.6: This module has moved and is now available at @hapi/catbox. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated ammo@3.0.3: This module has moved and is now available at @hapi/ammo. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated bounce@1.2.3: This module has moved and is now available at @hapi/bounce. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated heavy@6.1.2: This module has moved and is now available at @hapi/heavy. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated mimos@4.0.2: This module has moved and is now available at @hapi/mimos. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated joi@14.3.1: This module has moved and is now available at @hapi/joi. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated podium@3.2.0: This module has moved and is now available at @hapi/podium. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated catbox-memory@4.0.1: This module has moved and is now available at @hapi/catbox-memory. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated shot@4.0.7: This module has moved and is now available at @hapi/shot. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated statehood@6.0.9: This module has moved and is now available at @hapi/statehood. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated somever@2.0.0: This module has moved and is now available at @hapi/somever. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated topo@3.0.3: This module has moved and is now available at @hapi/topo. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated subtext@6.0.12: This module has moved and is now available at @hapi/subtext. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated cryptiles@4.1.3: This module has moved and is now available at @hapi/cryptiles. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated teamwork@3.2.0: This module has moved and is now available at @hapi/teamwork. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated pez@4.0.5: This module has moved and is now available at @hapi/pez. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated iron@5.0.6: This module has moved and is now available at @hapi/iron. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated bourne@1.1.2: This module has moved and is now available at @hapi/bourne. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated content@4.0.6: This module has moved and is now available at @hapi/content. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated wreck@14.2.0: This module has moved and is now available at @hapi/wreck. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated nigel@3.0.4: This module has moved and is now available at @hapi/nigel. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm WARN deprecated vise@3.0.2: This module has moved and is now available at @hapi/vise. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
    npm notice created a lockfile as package-lock.json. You should commit this file.
    npm WARN start-hapi@0.0.1 No description
    npm WARN start-hapi@0.0.1 No repository field.
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules/fsevents):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
    
    added 55 packages from 31 contributors in 2.012s
    
    1 package is looking for funding
      run `npm fund` for details
    
    [2021-08-30T14:11:40.160] [INFO ] [S-CLI] - End the pre-action
      devsapp!hapi@0.0.8.zip file decompression completed
      core-0.0.148.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.
    [2021-08-30T14:11:58.759] [WARN ] [HAPI] - The startup file is not found, create a [start.sh] as the startup file.
     Using web framework type: nas, If you want to deploy with container, you can [s cli fc-default set web-framework container] to switch.
      devsapp!domain@0.0.21.zip file decompression completed
    (node:8893) [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)
      End of request
      Deployed.
      End of request
      devsapp!fc-deploy@0.0.45.zip file decompression completed
    [2021-08-30T14:12:08.544] [INFO ] [FC-DEPLOY] - Using region: cn-shenzhen
    [2021-08-30T14:12:08.545] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-08-30T14:12:08.545] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-08-30T14:12:08.545] [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-30T14:12:08.923] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-hapi exists
    [2021-08-30T14:12:09.148] [INFO ] [FC-DEPLOY] - Setting role: AliyunFCDefaultRole
      devsapp!ram@0.0.14.zip file decompression completed
    [2021-08-30T14:12:09.879] [INFO ] [RAM] - Checking Role AliyunFCDefaultRole exists
    [2021-08-30T14:12:10.086] [INFO ] [RAM] - Updating role: AliyunFCDefaultRole
    [2021-08-30T14:12:10.187] [INFO ] [RAM] - Checking Plicy AliyunFCDefaultRolePolicy exists
    [2021-08-30T14:12:10.245] [INFO ] [RAM] - Creating plicy: AliyunFCDefaultRolePolicy
    [2021-08-30T14:12:10.438] [INFO ] [FC-DEPLOY] - Using vpcConfig: auto: fc will try to generate related vpc resources automatically
      devsapp!vpc@0.0.10.zip file decompression completed
    [2021-08-30T14:12:12.279] [INFO ] [VPC] - Getting vpc: fc-deploy-component-generated-vpc-cn-shenzhen
    [2021-08-30T14:12:12.374] [INFO ] [VPC] - Getting vswitch: fc-deploy-component-generated-vswitch-cn-shenzhen
    [2021-08-30T14:12:12.507] [INFO ] [VPC] - Getting securityGroup: fc-deploy-component-generated-securityGroup-cn-shenzhen
    [2021-08-30T14:12:12.652] [INFO ] [FC-DEPLOY] - Generated vpcConfig:
    securityGroupId: sg-wz92ctigpi221smc****
    vSwitchId: vsw-wz9qnuult4q5g4f7n****
    vpcId: vpc-wz9x9bzs0wtvjgt6n****
    
    [2021-08-30T14:12:12.652] [INFO ] [FC-DEPLOY] - Using nasConfig: auto: fc will try to generate related nas file system automatically
     devsapp!nas@0.0.30.zip file decompression completed
    [2021-08-30T14:13:16.826] [INFO ] [FC-DEPLOY] - Generated nasConfig:
    groupId: 10003
    mountPoints:
      - fcDir: /mnt/auto
        nasDir: /serverless-devs-hapi
        serverAddr: example.com
    userId: 10003
    
    [2021-08-30T14:13:17.269] [INFO ] [FC-DEPLOY] - Checking Function hapi exists
    [2021-08-30T14:13:17.534] [WARN ] [FC-DEPLOY] - Image registry.cn-shenzhen.aliyuncs.com/web-framework/nodejs12:0.0.1 dose not exist locally.
    Maybe you need to run 's build' first if it dose not exist remotely.
    [2021-08-30T14:13:17.632] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-hapi exists
    [2021-08-30T14:13:17.878] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-hapi exists
    [2021-08-30T14:13:17.972] [INFO ] [FC-DEPLOY] - Creating service: serverless-devs-hapi
    [2021-08-30T14:13:17.972] [INFO ] [FC-DEPLOY] - Creating function: hapi
    [2021-08-30T14:13:17.972] [INFO ] [FC-DEPLOY] - Creating triggers: ["serverless-devs-hapi"]
     Make service serverless-devs-hapi success.
     Make function serverless-devs-hapi/hapi success.
     Make trigger serverless-devs-hapi/hapi/serverless-devs-hapi success.
    [2021-08-30T14:13:20.949] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-hapi exists
    [2021-08-30T14:13:21.476] [INFO ] [FC-DEPLOY] - Checking Function hapi exists
    [2021-08-30T14:13:21.750] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-hapi exists
    [2021-08-30T14:13:22.017] [INFO ] [FC-DEPLOY] - Creating custom domain: hapi.serverless-devs-hapi.1880770869023420.cn-shenzhen.fc.devsapp.net
     devsapp!fc-domain@0.0.13.zip file decompression completed
    [2021-08-30T14:13:22.593] [INFO ] [FC-DOMAIN] - Creating custom domain: hapi.serverless-devs-hapi.1880770869023420.cn-shenzhen.fc.devsapp.net
    
    There is auto config in the service: serverless-devs-hapi
    [2021-08-30T14:13:23.072] [INFO ] [WEB-FRAMEWORK] - nas component get src is: ./src
    Packing ...
    Package complete.
     Start uploading file: /mnt/2bfb748534-tpm20-cn-shenzhen/serverless-devs-hapi/hapi/src.zip
     File uploaded successfully: /mnt/2bfb748534-tpm20-cn-shenzhen/serverless-devs-hapi/hapi/src.zip
     upload done
    [2021-08-30T14:13:56.968] [INFO ] [FC-NAS] - unzipping file
    [2021-08-30T14:13:58.995] [INFO ] [FC-NAS] - unzip done
     Try container acceleration
    framework-test:
      region: cn-shenzhen
      serviceName: serverless-devs-hapi
      functionName: hapi
      customDomains:
        - >-
          http://hapi.serverless-devs-hapi.188077086902****.cn-shenzhen.fc.devsapp.net

执行结果

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

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