1

I want package a lambda layer using codebuild.

My codebuild buildspec is as follows:

version: 0.2

phases:
  install:
    runtime-versions:
      nodejs: 10  
    commands:
      - npm init -y
      - npm install --save middy
artifacts:
  files:
    - 'node_modules/**/*'
    - 'package-lock.json'
    - 'package.json'

This saves a nodejs.zip folder to my s3 bucket

the resulting zip file looks like this:

folder dir

middy is here

package.json is as follows:

{
  "name": "src",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "middy": "^0.30.4"
  }
}

however when I add this layer to my lambda (node10.x)

and import my mods:

'use strict';
var AWS = require('aws-sdk');
const middy = require('middy')
const { cors } = require('middy/middlewares')

Returns the following error:

{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'middy'\nRequire stack:\n- /var/task/function_code/verify_zipcode.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module 'middy'",
    "Require stack:",
    "- /var/task/function_code/verify_zipcode.js",
    "- /var/runtime/UserFunction.js",
    "- /var/runtime/index.js",
    "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:956:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)",
    "    at Module.load (internal/modules/cjs/loader.js:812:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:724:14)",
    "    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)",
    "    at internal/main/run_main_module.js:17:11"
  ]
}

Adding the envoronment variable: NODE_PATH : ./:/opt/node_modules gave my lambda access to my layers, but lost the context of aws-sdk

After adding the env var I get the following error:

{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'aws-sdk'\nRequire stack:\n- /var/task/function_code/verify_zipcode.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module 'aws-sdk'",
    "Require stack:",
    "- /var/task/function_code/verify_zipcode.js",
    "- /var/runtime/UserFunction.js",
    "- /var/runtime/index.js",
    "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:956:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)",
    "    at Module.load (internal/modules/cjs/loader.js:812:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:724:14)",
    "    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)",
    "    at internal/main/run_main_module.js:17:11"
  ]
}

Is there a way to use both the native aws-sdk and my layers? Or do I need to use an aws-sdk layer anytime I use other custom layers?

1 Answer 1

2

The directory structure of a node.js layer (nodejs.zip in your example) should be:

├── nodejs
  └── package.json
  └── node_modules
          └── middy(version z.z.z)

Update your buildspec file to add a parent nodejs folder:

version: 0.2

phases:
  install:
    runtime-versions:
      nodejs: 10  
    commands:
      - mkdir nodejs           # NEW LINE
      - cd nodejs              # NEW LINE
      - npm init -y
      - npm install bcrypt

artifacts:
  files:
    - 'nodejs/**/*'            # CHANGE LINE
    - 'package.json'

and remove the environment variable: NODE_PATH

Sign up to request clarification or add additional context in comments.

7 Comments

When the nodejs.zip unzips it is inside a nodejs folder - does it need to be nodejs/nodejs/...
I didn't see any nodejs folder when you wrote this the resulting zip file looks like this:
That's because I was browsing the zip file in windows 10, I hadn't unzipped it. When you unzip a folder it automatically puts the contents inside a folder with the same name as the zip folder
Let's call your zip nodejs_test, when you unzip it you should see nodejs->node_modules, when you browsing the zip file in windows 10 first dir should be nodejs
if I call is nodejs_test.zip when I unzip it I'll see nodejs_test/node_modules
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.