Required .js file is defined on local but null on Lambda

Hi everyone,

I have been scratching my head over this for hours now as I would have thought there is a simple solution to this. I have been through endless stack overflow threads and cannot seem to get this working.

I am trying to connect to Document DB with AWS which requires you to provide the MongoClient with a certificate. I have created a certificate js file that exports the certificate as a plain string:

// rds-combined-ca-bundle.pem.js
module.exports = [`
// huge certificate file
// ...
`]

In my handler.js, I have a function which I am purely using for testing as I can’t even get the value to read on Lambda:

const ca = require('./rds-combined-ca-bundle.pem.js')

module.exports.test = (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify(ca)
  }
}

When I run this locally with serverless invoke local -f test I get the full certificate output. However when I deploy and run serverless invoke -f test I simply get “null”.

What am I missing here? I apologise if this is super basic, I think I am googling the wrong keywords to find an answer to this. Thank you so much in advance to any advice you can give.

Okay after a lot of experimentation, the answer was to add async to the handler:

const ca = require('./rds-combined-ca-bundle.pem.js')

module.exports.test = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify(ca)
  }
}

Does anyone know the underlying reason for this? We are not awaiting for any particular value to resolve and I assume the required file has already been packed in as part of the deployment?