How to share dependencies between Lambdas in a NodeJS Typescript application deployed using AWS SAM

0

I was looking for examples or guidance on how to structure a more complex CRUD serverless application written in NodeJS/Typescript. Let's say I want to build a CRUD application that has a Lambda for each operation. Let's also suppose that I have some TypeScript contracts that I want to share with my Lambdas. I am hoping someone can give me some guidance and suggestions as the SAM examples I've found online are pretty basic and don't seem to cover this case.

Let's say my project is structured like this:

|--src
|--/contracts
|--/handlers
|----/getLambda
|----/putLambda
|--package.json - root NPM package file
|--template.yml - SAM template

Initially, I was hoping that I could have a single package.json that could be shared by my Lambdas since they all have very few and similar dependencies. From my exploration, it appears SAM prefers for there to be a package.json file for each Lambda. If I create a package.json for each Lambda, I think have the problem that "getLambda" cannot reference contracts defined in the contracts directory since it is not a descendent of that Lambda's package.json file. I would love it if I could reference contracts in this higher directory. If that's not possible, I imagine I'll have to put these shared dependencies in a Lambda layer and share this layer with my Lambda. I haven't found a great example of doing this, especially with ES module imports.

I'm very excited about serverless and SAM but the lack of examples of what I would assume to be pretty common scenarios can get frustrating. Any help would be greatly appreciated.

asked 5 months ago173 views
1 Answer
2

I understand that you are trying to implement CRUD serverless application written in NodeJS/Typescript. However, you have some queries with the sam project structure. Kindly note that the folder location that you are specifying for a function in 'CodeUri' property should have package.json in case you are using any dependencies in your function code.

A template snippet with a function resource should have an individual folder structure as referred below -

  TestFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: function1/
      Runtime: nodejs18.x
      Handler: index.lambda_handler
|--function1/
    |---index.js
    |---package.json

Any dependencies you want to use along with this function, you need to add that either in package.json or ship the js files/folders within 'function1' folder so that they are included in the deployment package. For common dependencies that need to be shared across multiple functions, it is better to package the module as layer archive file. You may refer to this pattern which shows how you can package an archive file as a layer for your function. Though the example is in python, you can refer to our public documentation to follow the steps to create one for nodejs counterpart.

Should you have any additional queries or you are seeking a guidance from one of our support engineers, please do not hesitate to reach out over a support case.

profile picture
answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions