Serverless+Webpack:在 ZIP 中包含 .pem 文件

m__*_*__0 0 amazon-web-services webpack serverless-framework serverless aws-serverless

我尝试使用无服务器将 lambda 函数部署到 AWS。一切正常,但该函数无法执行,因为找不到两个文件(就是这么fs.readFileSync说的)。我将它们包含在 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
Run Code Online (Sandbox Code Playgroud)

当我查看上传到 S3 的 .zip 文件时,两个 .pem 文件都不包含在内。我已经尝试使用__dirnamelambda 函数获取完整的文件路径。我的webpack.config.js样子如下:

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"
            }
        ]
    }
};
Run Code Online (Sandbox Code Playgroud)

你们有人能帮忙吗?

干杯!

Ere*_*rez 7

由于serverless-webpack是为您打包而不是无服务器框架,因此您需要使用 Webpack 插件:

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"
            }
        ]
    }
};


Run Code Online (Sandbox Code Playgroud)

正如 @hephalump 所提到的,最好使用 AWS Secrets Manager(或参数存储/环境变量)。