Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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:

Globals:

  Function:

    Runtime: nodejs8.10

MyFunction:

    Type: AWS::Serverless::Function

    Properties:

        CodeUri: a/

        Handler: index.handler

        Layers:

        - !Ref MySharedLayer

MySharedLayer:

    Type: AWS::Serverless::LayerVersion

    Properties:

        LayerName: SharedLayer

        Description: sharing code is good

        ContentUri: layer/

        CompatibleRuntimes:

            - 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

Browse Categories

...