Load module inside microservices

Hi,

After an amazing experience with Serverless Framework + AWS IoT + DynamoDB I decided to migrate an old platform with more than 50.000 users to serverless.

In this case I need to use RDS. My architecture is the following:

services
    /authorizer
    /database
        ...
        /models
        handler.js
        db.js
        serverless.yml
    /users
        handler.js
        serverless.yml

I need to load my db module inside every microservice (like users) to interact with the database but I can’t load the module doing this:

const connectToDatabase = require('../database/db');

So what is the best practice?

Thank you!

When we were doing this with NodeJS, we used symlinks a lot to put “lib” directories into each service.

Now that we’re writing our services in TypeScript, we’re using serverless-plugin-webpack, and since that packages all the dependencies for you, you can reference code outside the service directory without issue. You could do the same even without TypeScript.

Jeremy Thomerson, serverless-training.com

1 Like

It’s a perfect solution, thank you!