Explore Courses Blog Tutorials Interview Questions
0 votes
in AWS by (19.1k points)

What is the prefered way to share code between AWS Lambda functions?

I have a structure like this:

  • functions
    • a
      • node_modules
      • index.js
      • package.json
    • b
      • node_modules
      • index.js
      • package.json
    • c
      • node_modules
      • index.js
      • package.json

This lets every function keep its own node_modules and I can package the whole thing with the CLI.

But what about custom code that needs to be shared?

I can require("../mylibrary") but the package command would still not include it.

1 Answer

0 votes
by (44.4k points)

It is possible with Lambda Layers. Follow this sample SAM template code:



    Runtime: nodejs8.10


    Type: AWS::Serverless::Function


        CodeUri: a/

        Handler: index.handler


        - !Ref MySharedLayer


    Type: AWS::Serverless::LayerVersion


        LayerName: SharedLayer

        Description: sharing code is good

        ContentUri: layer/


            - nodejs8.10

        RetentionPolicy: Retain

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.4k questions

32.5k answers


108k users

Browse Categories