[英]How to setup serverless.yml and webpack.config for a multiple-runtime AWS Lambda service
I want to deploy AWS Lambda functions with Node8.10 and Ruby2.5 runtimes from one serverless.yml file. 我想从一个serverless.yml文件中使用Node8.10和Ruby2.5运行时部署AWS Lambda函数。
I set up the following folder structure, with /node and /ruby holding my respective handlers. 我设置了以下文件夹结构,其中/ node和/ ruby持有各自的处理程序。
-/nodeRubyLambdas
-/node
-handler.js
-package.json, package-lock.json, /node_modules
-/ruby
-rubyRijndaelEncryption.rb
-Gemfile, Gemfile.lock, /vendor
-serverless.yml
-webpack.config.js
-package.json for serverless-webpack
Here is my serverless.yml 这是我的serverless.yml
service: nodeRubyLambdas
plugins:
- serverless-webpack
- serverless-offline
custom:
webpack:
webpackConfig: ./webpack.config.js
includeModules: true
provider:
name: aws
stage: dev
region: us-west-2
iamRoleStatements:
- Effect: Allow
Action:
- lambda:InvokeFunction
Resource: "*"
package:
individually: true
functions:
nodeMain:
handler: node/handler.main
runtime: nodejs8.10
events:
- http:
path: main
method: get
package:
individually: true
rubyEncryption:
handler: ruby/rubyRijndaelEncryption.lambda_handler
runtime: ruby2.5
environment:
RIJNDAEL_PASSWORD: 'a string'
package:
individually: true
My webpack configuration: (This is the base example, I just added the bit to ignore ruby files when I got my first error.) 我的webpack配置:(这是基本示例,我刚遇到第一个错误时就添加了忽略ruby文件的位。)
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 do 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/, /\.rb$/]
}
]
}
};
Fail #0: [Webpack Compilation error] Module parse failed 失败#0: [Webpack编译错误]模块解析失败
Fail #1: Basically, webpack assumes all functions are .js and tries to package them as such. 失败#1:基本上,webpack假定所有函数都是.js并尝试将其打包。 Based off this suggestion , I forced my entry point in webpack config to be my handler.js
基于此建议 ,我将Webpack配置中的入口点强制为我的handler.js
module.exports = {
entry: "./node/handler.js",
target: "node",
...
This packages ONLY the Node Lambda. 这仅打包节点Lambda。 An empty placeholder for the Ruby Lambda is created on AWS.
在AWS上创建Ruby Lambda的空占位符。
Fail #2: I commented out webpack from serverless.yml and added include and exclude statements in the functions package options. 错误#2:我从serverless.yml中注释掉了webpack,并在功能包选项中添加了include和exclude语句。
functions:
nodeMain:
package:
individually: true
include:
- node/**
- handler.js
exclude:
- ruby/**
- rubyLambda/**
rubyEncryption:
package:
individually: true
include:
- vendor/**
- rubyRijndaelEncryption.rb
exclude:
- Gemfile
- Gemfile.lock
- node/**
This gets an [ENOENT: no such file or directory] for node/node_modules/@babel/core/node_modules/.bin/parser. 这将为node/node_modules/@babel/core/node_modules/.bin/parser获取一个[ENOENT:没有这样的文件或目录]。 This file is not there but I don't understand why it is looking for it, since webpack is not being called.
这个文件不存在,但是我不明白为什么要查找它,因为没有调用webpack。
Sort of success?: I was able to get the Lambdas to deploy if I commented out webpack and used 是否成功?:如果我注释掉webpack并使用了Lambda,我就能部署Lambda
serverless deploy function -f <function name here>
to deploy the Ruby Lambda and then uncommented webpack and used the same thing to deploy the Node Lambda. 部署Ruby Lambda,然后取消注释webpack,并使用相同的方法部署Node Lambda。
I'm convinced that there's a better way to get them to deploy; 我相信,有更好的方法来部署它们。 Have I missed something in my setup?
我错过了设置中的某些内容吗? Is there another option I haven't tried?
我还有没有尝试过的其他选择?
PS I did see this pull request https://github.com/serverless-heaven/serverless-webpack/pull/256 , but it seems to be abandoned since 2017. PS我确实看到了这个拉取请求https://github.com/serverless-heaven/serverless-webpack/pull/256 ,但自2017年以来似乎已被放弃。
serverless-webpack
is not designed for non-JS runtimes. serverless-webpack
不适用于非JS运行时。 It hijacks serverless
packaging and deploys ONLY the webpack output. 它劫持了
serverless
打包,仅部署了webpack输出。
Here are your options: 这是您的选择:
serverless-webpack
and simply use serverless
' built-in packaging. serverless-webpack
而应该只使用serverless
的内置包装。 webpack
directly (not serverless-webpack
), and change your build process to compile using webpack
first and then let serverless
deploy the output folder. webpack
(而不是serverless-webpack
),并更改构建过程以首先使用webpack
进行编译,然后让serverless
部署输出文件夹。 PS The package.individually
property is a root-level property in your serverless.yml
. PS的
package.individually
属性是你的根级属性serverless.yml
。 It shouldn't be in provider
or in your function definitions. 它不应出现在
provider
或您的函数定义中。
For those who may be looking for options for multiple-runtimes other than serverless-webpack, I ended up switching to this plugin: https://www.npmjs.com/package/serverless-plugin-include-dependencies . 对于那些可能正在寻找除无服务器Webpack以外的多个运行时选项的人,我最终切换到此插件: https ://www.npmjs.com/package/serverless-plugin-include-dependencies。
It works with my runtimes (Ruby and Node) and lets you use package.individually with package.include/exclude at the root and function level if the plugin misses something. 它与我的运行时(Ruby和Node)一起使用,并且如果插件遗漏了某些东西,则可以在根和功能级别上单独使用package.include / exclude和package.include / exclude。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.