【问题标题】:serverless with azure functions and webpack具有 azure 功能和 webpack 的无服务器
【发布时间】:2018-02-19 15:21:14
【问题描述】:

我想知道是否有人使用带有 azure 函数的无服务器框架以及如何处理跨函数共享代码和捆绑?

我正在将 hapi.js 应用程序转换为 serverless + serverless-azure-functions 并尝试在部署之前捆绑我的代码,以便我可以将各种 require 用于可重用模块。

我找到了serverless-webpack,它创建了可能适用于 AWS Lambda 的捆绑包,但由于缺少 function.json 文件(例如 list-function.json),在 azure 上存在问题,因此这些函数根本不可见在 azure-portal 中,我也无法调用它们。

也找到了article 来解决这个问题,但它展示了如何使用仅支持 Windows 平台的azure-functions-cli 来处理这个问题。

最好的,JH

【问题讨论】:

    标签: azure webpack azure-functions serverless-framework serverless-architecture


    【解决方案1】:

    得到https://medium.com/a-man-with-no-server/deploying-a-serverless-application-using-webpack-and-babel-to-support-es2015-to-aws-2f61cff8bafb的提示,我用serverless-webpack修改了一个serverless azure functions启动测试项目,似乎满足了你的要求。

    我在 serverless azure functions 项目的根目录下建了一个src 文件夹,作为开发源代码文件夹。带 2 个测试文件:
    handler.js

    'use strict';
    let tool = require("./tool");
    /* eslint-disable no-param-reassign */
    
    module.exports.hello = function (context) {
      context.log('JavaScript HTTP trigger function processed a request.');
    
      context.res = {
        // status: 200, /* Defaults to 200 */
        body: tool.hello(),
      };
    
      context.done();
    };
    

    tool.js

    module.exports={
        hello:()=>{
            return "hello world";
        }
    }
    

    webpack.config.js 在根目录中:

    var nodeExternals = require('webpack-node-externals')
    
    module.exports = {
       entry: './src/handler.js',
       target: 'node',
       externals: [nodeExternals()],
       output: {
          libraryTarget: 'commonjs',
          path: __dirname,
          filename: 'handler.js', // this should match the first part of function handler in serverless.yml
       },
       module: {
          loaders: [
             {
                test: /\.jsx?$/,
                exclude: /node_modules/,
                include: __dirname,
                loaders: ["babel-loader"]
             }
          ]
       }
    };
    

    使用哪个配置文件,捆绑出来的文件将位于根目录下的service/handler.js

    所以我也修改了serverless.yml,现在部分看起来像:

    package:
      include:
        - service/handler.js
      exclude:
        - handler.js
    
    functions:
      hello:
        handler: service/handler.hello
        events:
          - http: true
            x-azure-settings:
              authLevel : anonymous
          - http: true
            x-azure-settings:
              direction: out
              name: res
    
    custom:
      webpackIncludeModules:
        packagePath: './package.json'
    

    这些修改后,使用serverless deploysrc文件夹中的文件打包,然后打包部署到azure函数中。

    希望对你有帮助。

    【讨论】:

    猜你喜欢
    • 2017-09-30
    • 2020-05-01
    • 2023-03-13
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多