How to create a multi-language serverless setup?


#1

Hi all,

I currently have a working serverless setup deploying JS lambdas. As such I’m using “serverless-webpack”.

I am now trying to deploy Golang lambdas along side my JS ones. I have read https://serverless.com/blog/building-mutliple-runtimes/, however i am running into issues with webpack.

[./src/testLambdaGo.go] 174 bytes {src/testLambdaGo} [built] [failed] [1 error]

ERROR in ./src/testLambdaGo.go
Module parse failed: The keyword 'package' is reserved (1:0)
You may need an appropriate loader to handle this file type.
| package main
| 
| import (
 
  Error --------------------------------------------------

In short, webpack is trying to bundle my golang files.

I did some digging around, and apparently in my current serverless setup, everything is automatically being bundled by webpack, which assumes everything is a js file. I’m guessing I either need a golang loader or some other way to bypass webpack for non JS lambdas. On top of all this, everything has to be compatible with the serverless framework. It seemed more like a serverless issue than a webpack issue hence I’m asking here.

Any help will be appreciated!

relevant part of my serverless.yml

  - serverless-webpack
  - serverless-offline

# serverless-webpack configuration
# Enable auto-packing of external modules
custom:
  webpack:
    webpackConfig:   ./webpack.config.js
    includeModules:  true

webpack.config.js:

const slsw = require("serverless-webpack");
const nodeExternals = require("webpack-node-externals");

module.exports = {
  entry: slsw.lib.entries,
  target: "node",
  // Generate sourcemaps for proper error messages
  devtool: 'source-map',
  // Since 'aws-sdk' is not compatible with webpack,
  // we exclude all node dependencies
  externals: [nodeExternals()],
  mode: slsw.lib.webpack.isLocal ? "development" : "production",
  optimization: {
    // We no not want to minimize our code.
    minimize: false
  },
  performance: {
    // Turn off size warnings for entry points
    hints: false
  },
  // Run babel on all .js files and skip those in node_modules
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: "babel-loader",
        include: __dirname,
        exclude: /node_modules/
      }
    ]
  }
};

#2

I’m leaning towards it being a webpack problem. The first thing I would test is changing the entry in your webpack.config to manually list the JavaScript entry points instead of using slsw.lib.entries.


#3

Hi @buggy

Thanks for the reply. I was hoping for a way to configure serverless to accept a multi-language repo. As far as I know, the golang serverless examples do not use webpack.
I’m trying to avoid deep diving into webpack internals to get webpack to play nice with other languages. I would prefer serverless to not even activate webpack on non-JS lambdas - https://github.com/serverless-heaven/serverless-webpack/issues/255


#4

@laboro18 The slsw.lib.entries is an array of function entry points that’s automatically created from the serverless.yml. I’m suggesting that you switch from the automatically generated list of entry points to a manually maintained list because I suspect the automatic list also includes the functions using the Go runtime. If I’m right then using a manual list will stop webpack from trying to compile them.


#5

I am having the same issue with a service that mixes node8.10 and ruby2.5 Lambdas.
When I tried the solution @buggy suggested, only my Node Lambda is packaged, and the Ruby Lambda has an empty namesake on AWS Console.
@laboro18, were you able to solve your problem? If so, could you post it here?


#6

I ended up switching to the following plugin: https://www.npmjs.com/package/serverless-plugin-include-dependencies.
I had to use package: individually: true at the root level and a couple package: include/exclude at the function level for my Ruby Lambda.