AnomalyInnovations / serverless-bundle

Optimized packages for ES6 and TypeScript Node.js Lambda functions without any configuration.
https://serverless-stack.com/chapters/package-lambdas-with-serverless-bundle.html
MIT License
536 stars 157 forks source link

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

Open ggrantrowberry opened 4 years ago

ggrantrowberry commented 4 years ago

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.

jayair commented 4 years ago

@ggrantrowberry What was the issue you ran into with the new beta? That should address this issue.

You can checkout the Knex.js instructions in the README - https://github.com/AnomalyInnovations/serverless-bundle/tree/beta#advanced-options

blai commented 4 years ago

I ran into the same problem. After some debugging, I found that the dynamic importing of knex.js and sequelize.js isn't the same. While both are dynamic import depending on the configuration, knex does it with a static import at dialect implementation level, while sequelize configure it at dialect level, and invokes require with a variable in a base class. This subtle difference has made a huge difference when webpack transpiles the code, in the sequelize case, webpack would replace the require() into the webpack's empty importer, which does nothing but throw exception.

blai commented 4 years ago

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.

jayair commented 4 years ago

@blai Thanks for investigating and posting this. Agreed, that we need specific examples. Do you mind contributing one for this case?

Btw, we have this note in the README and I noticed that it has the right option ignorePackages but does not mention the dialectModule. https://github.com/AnomalyInnovations/serverless-bundle#pg

jayair commented 4 years ago

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