我想用Weback來構建一個簡單的lambda nodejs函數hello world。webpack&aws lambda
exports.handler = (event, context, callback) => {
callback(null, 'Hello from Lambda');
};
該函數在lambda中工作,處理器「index.handler」在aws lambda配置頁面中配置。
上述的Webpack生成的代碼不起作用。該函數在模塊'index'上拋出錯誤「Handler'handler''。它看起來像模塊成爲反義詞。
它可以通過更新生成的代碼來工作,如下所示。
global.handler = (event, context, callback) => {
//async.map(['file1','file2','file3'], console.log, function(err, results){
// results is now an array of stats for each file
callback(null, 'Hello from Lambda');
//});
//add the following at the end.
exports.handler = global.handler;
webpack.config.js如下。
var path = require('path');
module.exports = {
// Specify the entry point for our app.
entry: [
path.join(__dirname, '/src/autotag.js')
],
// Specify the output file containing our bundled code
output: {
path: path.join(__dirname, "dist"),
filename: "autotag.js"
},
//target: "node",
module: {
/**
* Tell webpack how to load 'json' files.
* When webpack encounters a 'require()' statement
* where a 'json' file is being imported, it will use
* the json-loader.
*/
loaders: [{
test: /\.json$/,
loaders:
}]
}
}
任何使用webpack構建lambda nodejs函數的人?
任何幫助表示讚賞。
爲什麼你的處理程序名爲「index.handler」,但你的webpack入口點是「autotag.js」?你能否包含你的目錄結構來顯示文件的相對位置? – dashmug
一個簡單的解決方案是簡單地編寫一個附加'exports.handler = global.handler;'的腳本到最後一個bundle的末尾。我通常編寫'npm run bundle'來首先運行webpack,然後運行一個腳本將它附加到bundle的末尾。 – kevin628
感謝您的關注。 Webpack在dist文件夾中生成autotag.js文件。然後將代碼複製到AWS Lambda以創建該文件。對於任何內聯代碼,處理程序是「index.handler」。我也嘗試使用包含autotag.js的zip文件以及node_modules,但是存在相同的錯誤。在這種情況下,處理程序是autotag.hander。 – CalmCloud