Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS lambda Error: Please install pg package manually

I'm trying to bundle lambda code with webpack and my code uses sequelize and postgres. This has been a very frustrating process because I've done what other questions have suggested. I've tried excluding pg-native and I've tried force including pg but I still get the same error. My webpack config looks like

const path = require("path")
const glob = require("glob")
const pg = require('pg');


module.exports = {
    target: 'node',
    entry: './app.js', 
    module: {
        rules: [
            {
                test: /\.js$/,
                use: [{
                    loader: 'babel-loader',
                    options: {
                        include: [pg] + glob.sync("../../database/migrations/*.js")
                    },
                }]
            },
        ]
    },
    devtool: 'source-map',
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: '[name].js',
        libraryTarget: 'commonjs2',
    }
}

My app.js looks like

const sdk = require('aws-sdk');
const Sequelize = require('sequelize');
const pg = require('pg');
const { Umzug, SequelizeStorage } = require('umzug');


exports.handler = async function (event, context, callback) {

let secretArn = process.env.DATABASE_SECRET_ARN;
let db_user = process.env.DATABASE_USER;
let db_endpoint = process.env.DATABASE_ENDPOINT;
let db_port = process.env.DATABASE_PORT;
let db_name = process.env.DATABASE_NAME;
console.log('here',JSON.stringify(secretArn));
try {
    console.log(secretArn);
    //Setup secret manager connection and get the json of the secret value, and its assocated properties
    let secretResponse = await new sdk.SecretsManager().getSecretValue({ SecretId: secretArn }).promise();
    console.log('got the secret')
    //Decode and parse the values to actually use them
    let secretValue;
    if (secretResponse.SecretString != null || secretResponse.secretResponse != undefined) {
        secretValue = secretResponse.SecretString
    } else {
        var buff = Buffer.from(secretResponse.SecretString, 'base64');
        secretValue = buff.toString('ascii');
    }
    let secretJson = JSON.parse(secretValue);


    //Create Connection
    console.log("Setting up connection")
    //Connection String generation
    const sequelize = new Sequelize.Sequelize(db_name, db_user, secretJson.password,
        { host: db_endpoint, port: db_port, dialect: 'postgres' });
    }
}

The code fails when the const sequelize line is ran. Has anyone else ran into this issue I have got it to work without webpack but I would rather it be bundled.

like image 691
Panda Ninja Avatar asked Feb 01 '26 06:02

Panda Ninja


1 Answers

I made comment above that I was experiencing the same issue. After further investigation, I found that I could force the Sequelize library to avoid a lookup of pg-native by passing a dialectModule parameter in addition to a dialect parameter.

const sequelize = new Sequelize.Sequelize(
    db_name,
    db_user, 
    secretJson.password,
    {
        host: db_endpoint,
        port: db_port,
        dialect: 'postgres'
        dialectModule: require('pg')
    }
);

In my case, I also needed to confirm that the host parameter was set in the right place, but that's not the case in this question.

like image 52
SulZ Avatar answered Feb 04 '26 01:02

SulZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!