question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't get sequelize.js to work with serverless-bundle.

See original GitHub issue

I am using a lambda function to parse a file uploaded to s3 then take the data and put it in a remote mysql database. I am using sequelize.js with the mysql2 package. I’ve deployed my code but I get the this error, {“errorType”:“Error”,“errorMessage”:“Please install mysql2 package manually”}. I googled around and found that using serverless-webpack I just need to use the following.

custom:
  webpack:
    includeModules:
      forceInclude:
        - mysql
        - mysql2

https://stackoverflow.com/questions/48554917/getting-sequelize-js-library-to-work-on-amazon-lambda I tried that but it obviously didn’t work with serverless-bundle. I saw a previous issue about using knex.js that looked like a similar problem. I tried installing the serverless-bundle@beta as was suggested but it didn’t install properly.

I’m brand new to serverless and I’m not sure what else to do.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
blaicommented, Mar 4, 2020

BTW, I found the work around in here. Basically Sequelize config accepts a dialectModule option, which would allow webpack to work around the dynamic import issue. Here’s some code fragments I extract from an working example:

// hello.js
import Sequelize from 'sequelize'
import pg from 'pg'

export const hello = async function (event, context, ...rest) {
  const sequelize = new Sequelize(
    process.env.DB_NAME,
    process.env.DB_USERNAME,
    process.env.DB_PASSWORD,
    {
      host: process.env.DB_HOST,
      dialect: process.env.DB_DIALECT,
      dialectModule: pg
    }
  )
  const result = await sequelize.query('select 1+1 as result')

  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: `Hello ${result}`,
        input: event
      },
      null,
      2
    )
  }
# serverless.yml
service: sls2

provider:
  name: aws
  runtime: nodejs12.x
  environment:
    DB_DIALECT: "postgres"
    DB_NAME: "sls2_name"
    DB_USERNAME: "sls2_username"
    DB_PASSWORD: "sls2_password"
    ...
custom:
  bundle:
    # forceInclude: # NOTE: you don't need this anymore
    #   - pg
    ignorePackages:
      - pg-native

plugins:
  - serverless-bundle

functions:
  hello:
    handler: src/hello.hello

I suggest we create an examples folder, and add these solutions for special cases into there as working (but minimal) examples.

0reactions
jayaircommented, Mar 8, 2020

For now, let me add a note to the README with your instructions. Later we can link to an example.

Edit: Added https://github.com/AnomalyInnovations/serverless-bundle/blob/master/README.md#sequelize

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serverless doesn't include `pg` dependency / Internal error ...
I'm currently struggling with the deployment of my GraphQL serverless function. Everything is working perfectly locally, the database is ...
Read more >
How to Use Sequelize (v6) ORM (Lambda With TypeScript)
1. Set up a local database. 2. Set up a serverless framework template. 3. Set up the Sequelize Models & DB connection. 4....
Read more >
Serverless + Sequelize + AWS RDS Integration
If you've just added Sequelize into your code and are getting errors like “module initialization error: Error”, it might be because you've ...
Read more >
serverless-bundle
An extension of the serverless-webpack plugin that bundles your ES6 + TypeScript Node.js Lambda functions.. Latest version: 5.5.0, ...
Read more >
Resolving Serverless Webpack Issues | by Dustin Goodman
A story about how I debugged an issue with our webpack bundles on a ... for our serverless stack began to fail due...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found