ChrisRTech
ChrisRTech

Reputation: 577

creating and accessing common code in AWS Lambda NodeJS

What is the directory structure I should use for:

  1. NodeJS code that I want to package as a layer?
  2. A NodeJS app that uses the common code? I want to use a SAM or cloudformation template to build the layer and another template to build a lambda that uses it. I want the layer to be used by multiple Lambdas, so that is why I am packaging it separately.

I'm still not clear on the exact directory structure I should use for the common code. Also, if my common code in turn depends on things in its package.json file, how do I structure things? Example: I want to build common code to read from the AWS parameter store. Then I will need to depend on aws-param-store in the package.json of my common code, i.e:

 "dependencies": {
    "aws-param-store": "^3.0.0",

Assume my common code lives in some package called my-common-code.

Once I have the layer built and deployed correctly, I know I can reference the ARN name in my SAM template (correct way to do it?) for the Lambda? How does my calling Lambda reference the common code in terms of: 1. directory structure? 2. What goes into the package.json file for the calling Lambda to reference the name and version of my-common-code? I'm assuming something like this?

 "dependencies": {
    "my-common-code": "^1.0.0",

In my Lambda, I want to be able to do this:

let commonModule = require('my-common-code');
commonModule.doSomething();

I've tried various structures and configurations but I'm getting errors that either my-common-code cannot be found, or something that it depends on cannot be found, e.g. aws-param-store.

Upvotes: 3

Views: 990

Answers (1)

K Mo
K Mo

Reputation: 2155

To create your layer you first need to put your common code in a zip file with the following folder structure:

nodejs/node_modules/my-common-code/my-common-code.js

Ref: Including Library Dependencies in a Layer

i.e. if you were to open the zip file, you would see something like this:

my-common-code.zip
└ nodejs
  └ node_modules
    └ my-common-code
      └ package.json
      └ my-common-code.js
      └ more-common-code.js
      └ ...

To create a layer from that zip file using CloudFormation, you'll need to store it in an s3 bucket somewhere.

The the relevant part of the CloudFormation Template could look something like this:

 "Resources": {
    "MyCommonCode": {
        "Type": "AWS::Lambda::LayerVersion",
        "Properties": {
            "CompatibleRuntimes": [
                "nodejs8.10"
            ],
            "Content": {
                "S3Bucket": "my-common-code-bucket",
                "S3Key": "my-common-code.zip"
            },
            "Description": "My common code",
        }
    }
}

This was modified from this example.

The CloudFormation for the lambda using the layer could look like this:

"MyFunction": {
    "Type": "AWS::Lambda::Function",
    "Properties": {
        "Handler": "index.handler",
        "Layers":
            - "arn:aws:lambda:us-east-2:012345678901:layer:MyCommonCode"
        "Role": "arn:aws:iam::012345678901:role/LambdaRole",
        "Code": {
            "S3Bucket": "my-lambda-functions",
            "S3Key": "myLambda.zip"
        },
        "Runtime": "nodejs8.10"
    }
}

And after all of that, your code:

let commonModule = require('my-common-code');
commonModule.doSomething();

should work

Upvotes: 2

Related Questions