How do I deploy monorepo code to AWS Lambda using lerna?

Question:

I am attempting to make two AWS Lambda functions (written in typescript). Both of these functions share the same code for interacting with an API. In order to not have to copy the same code to two different Lambdas, I would like to move my shared code to a local module, and have both my Lambdas depend on said module.

My initial attempt at staring code between the two lambdas was to use a monorepo and lerna. My current project structure looks like this:

lerna.json:

In each of my package.json for my Lambda functions, I am able to include my local api module as such:

With this, I’ve been able to move the common code to its own module. However, I’m now not sure how to bundle my functions to deploy to AWS Lambda. Is there a way for lerna to be able to create a bundle that can be deployed?

Answer:

As cp -rL doesn’t work on the mac I had to come up with something similar.

Here is a workflow that works if all of your packages belong to one scope (@org):

In the package.json of your lerna repo:

In the package that contains your lambda function:

Now replace “packagename-version” and “@org” with the respective values of your project. Also add all of the dependent packages to “bundledDependencies”.

After running npm run deploy in the root of your lerna mono repo you end up with a folder “package” in the package that contains your lambda function. It has all the dependencies needed to run your function. Take it from there.

I had hoped that using npm pack would allow me to utilize .npmignore files but it seems that that doesn’t work. If anyone has an idea how to make it work let me know.

Leave a Reply