【问题标题】:Serverless+Webpack: include .pem files in ZIPServerless+Webpack:在 ZIP 中包含 .pem 文件
【发布时间】:2019-12-26 02:00:26
【问题描述】:

我尝试使用无服务器将我的 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

当我查看上传到 S3 的 .zip 文件时,两个 .pem 文件都不包括在内。我已经尝试使用__dirname 来获取 lambda 函数的完整文件路径。 我的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"
            }
        ]
    }
};

你们有人可以帮忙吗?

干杯!

【问题讨论】:

    标签: amazon-web-services webpack serverless-framework serverless aws-serverless


    【解决方案1】:

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

    正如@hephalump 所述,最好使用 AWS Secrets Manager(或 Parameter Store/Environment 变量)。

    【讨论】:

      【解决方案2】:

      虽然您绝对可以将您的证书文件作为部署包的一部分包含在内,而且如果没有更多信息,我不确定为什么不包含它们,但更安全的方法是将您的证书/密钥存储在 AWS Secrets 中Manager,然后在您的 Lambda 中访问该密钥。

      您可以了解有关 AWS Secrets Manager here 的更多信息,并且有一个存储和检索密钥 here 的教程。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-08
        • 1970-01-01
        • 1970-01-01
        • 2021-07-03
        • 2019-06-14
        • 2018-12-23
        • 2013-08-19
        • 1970-01-01
        相关资源
        最近更新 更多