Serverless + Webpack:在ZIP中包含.pem文件 [英] Serverless+Webpack: include .pem files in ZIP
问题描述
我尝试使用无服务器将lambda函数部署到AWS.一切正常,但由于找不到两个文件而无法执行该功能(这就是 fs.readFileSync
所说的).我在serverless.yml中包括以下几行:
I try to deploy my lambda function to AWS using serverless. Everything works fine but the function cannot be executed because two files are not found (thats what fs.readFileSync
says). I include them with the following lines in the serverless.yml:
provider:
name: aws
runtime: nodejs10.x
stage: dev
region: eu-central-1
package:
exclude:
- .env
include:
- src/config/push-cert.pem
- src/config/push-key.pem
当我查看上载到S3的.zip文件时,两个.pem文件都不包括在内.我已经尝试使用 __ dirname
来获取lambda函数的完整文件路径.我的 webpack.config.js
如下所示:
When I look in the .zip file which is uploaded to S3, both .pem files are not included. I already tried using __dirname
to get the complete file path on the lambda function.
My webpack.config.js
looks as following:
const path = require("path");
const nodeExternals = require("webpack-node-externals");
const slsw = require("serverless-webpack");
module.exports = {
entry: slsw.lib.entries,
target: "node",
node: {
__dirname: true
},
mode: slsw.lib.webpack.isLocal?"development":"production",
externals: [nodeExternals()],
output: {
libraryTarget: "commonjs",
// pay attention to this
path: path.join(__dirname, ".webpack"),
filename: "[name].js"
},
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: "babel-loader",
options: {
// ... and this
presets: [["@babel/env", {targets: {node: "8.10"}}]],
plugins: [
"@babel/plugin-proposal-object-rest-spread"
]
}
}
]
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: "graphql-tag/loader"
}
]
}
};
你们中的某人可以帮忙吗?
Can someone of you help?
干杯!
推荐答案
由于 serverless-webpack
是为您而不是无服务器框架进行打包,因此您需要使用Webpack插件:
Since serverless-webpack
does the packing for you and not the serverless framework, you'll need to use a Webpack plugin:
const path = require("path");
const nodeExternals = require("webpack-node-externals");
const slsw = require("serverless-webpack");
const CopyPlugin = require('copy-webpack-plugin');
module.exports = {
entry: slsw.lib.entries,
target: "node",
node: {
__dirname: true
},
mode: slsw.lib.webpack.isLocal?"development":"production",
externals: [nodeExternals()],
plugins: [
new CopyPlugin([
{ from: 'src/config/push-cert.pem', to: 'push-cert.pem' },
{ from: 'src/config/push-key.pem', to: 'push-key.pem' },
]),
],
output: {
libraryTarget: "commonjs",
// pay attention to this
path: path.join(__dirname, ".webpack"),
filename: "[name].js"
},
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: "babel-loader",
options: {
// ... and this
presets: [["@babel/env", {targets: {node: "8.10"}}]],
plugins: [
"@babel/plugin-proposal-object-rest-spread"
]
}
}
]
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: "graphql-tag/loader"
}
]
}
};
如@hephalump所述,最好使用AWS Secrets Manager(或参数存储/环境变量).
As mentioned by @hephalump it is better to use AWS Secrets Manager (or Parameter Store/Environment variables).
这篇关于Serverless + Webpack:在ZIP中包含.pem文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!