node.jsaws-lambdaamazon-auroraaws-sdk-js

"Error: Received packet in the wrong sequence." when connect to serverless aurora


I'm implementing a web application and it calls lambda function to get data from database.
I chose Serverless Aurora and wrote a code, but I get the exception "Error: Received packet in the wrong sequence." in query method.

I googled this issue but almost of all is too old.
An article said it is the problem of browisify but I don't use it.
I'm using serverless framework with typescript.

const mysql = require('serverless-mysql')({
  config: {
    host: process.env.DB_HOST,
    database: process.env.DB_NAME,
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD
  }
});

export async function query(sql: string, param?: Array<string>): Promise<any> {
  const results = await mysql.query(sql).catch(e => {
    console.log(e); // Error: Received packet in the wrong sequence
    throw new Error(e);
  });

  await mysql.end();
  return results;
}

following also not working


export async function query(sql: string, param?: Array<string>): Promise<any> {
  const connQueryPromisified = util
    .promisify(connection.query)
    .bind(connection);
  const result = await connQueryPromisified(sql, param)
    .then(row => {
      console.log(row);
      return row;
    })
    .catch(err => {
      console.log(err); // getting Error: Received packet in the wrong sequence
      throw err;
    });
  return result;
}

I also tried to use RDS DATA Service but in my region it is not available.

export async function query(sql: string, param?: Array<string>): Promise<any> {
 const params: aws.RDSDataService.Types.ExecuteSqlRequest = {
    awsSecretStoreArn: '***',
    dbClusterOrInstanceArn: '***',
    database: '***',
    schema: '***',
    sqlStatements: sql
  };
  console.log(params);
  try {
    const rdsService = new aws.RDSDataService({
      apiVersion: '2018-08-01',
      region: 'ap-northeast-1'
    });
    return rdsService
      .executeSql(params)
      .promise()
      .then(d => {
        return d;
      })
      .catch(e => {
        throw new Error(e);
      });
  } catch (err) {
    console.log(err); // nothing to say
    throw new Error(err);
  }
}

And here is my configurations:
webpack.config.js

const path = require('path');
const slsw = require('serverless-webpack');

module.exports = {
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  entry: slsw.lib.entries,
  devtool: 'source-map',
  resolve: {
    extensions: ['.js', '.jsx', '.json', '.ts', '.tsx'],
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
  },
  target: 'node',
  module: {
    rules: [
      // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
      { test: /\.tsx?$/, loader: 'ts-loader' },
    ],
  },
};

tsconfig.json

{
  "compilerOptions": {
    "lib": [
      "es2017"
    ],
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es2017",
    "outDir": "lib"
  },
  "exclude": [
    "node_modules"
  ]
}

I want to query only getting records from Serverless Aurora.
Can anybody help me?


Solution

  • The reason this is happening is because Webpack (in production mode) is putting your code through a minimiser, and the mysql module that serverless-mysql is using is not compatible with minimising.

    You can see the issue here: https://github.com/mysqljs/mysql/issues/1655. It's quite a common problem with Node modules which rely on function names to do code building, as uglyifiers/minifiers attempt to obfuscate/save space by changing the names of functions to single letters.

    The simplest fix would be to switch off minimising in your webpack config by adding:

      optimization: {
        minimize: false
      }
    

    There is some discussion in the linked issue on configuring various other minimising plugins (like terser) to not mangle names, which would allow you to get some of the benefit of minimising if you need it.