Enterprise Framework

Software Solutions in the Enterprise

Setup a Nodejs AWS Lambda Layer with Custom Code and Setup VSCode Debugging

Steps to Success:

Last Updated - 2019-12-21

Steps Overview

  1. Create a Root Source Code Folder
  2. Add a nodejs folder to the Root Source Code Folder
  3. Initialize NPM in the nodejs folder
  4. Configure VSCode to run
  5. Add jest for unit testing
  6. Configure VSCode to Debug Jest Tests
  7. Create a custom JS code for use
  8. Create a AWS Lambda function to use the custom JS

Folder Structure

Our folder structure will end up looking like this:





Now that we know the overview, lets get started

1) Create a Root Source Code Folder called my-layer

$ mkdir my-layer

2) Create a nodejs folder under my-layer

$ cd my-layer && mkdir nodejs

3) Initial nodejs packages in the nodejs folder

$ cd nodejs
$ npm init -y

4) Install Jest as our testing framework

$ npm install --save-dev jest

5) Create a src folder for custom code to be placed

$ mkdir src && cd src

The structure of our new folders should be:





Open Visual Studio Code and add the nodejs folder to the Workspace

  1. Open Visual Studio Code
  2. Open the folder /my-layer
  3. Choose File > Add Folder to Workspace
  4. Choose File > Save Workspace As
  5. Save the Workspace as /my-layer/my-layer-workspace.code-workspace

Open /my-layer/nodejs/package.json and update the scripts and jest section so it has the following:

"name": "directory-layer",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "rm ../directory-layer.zip && npm install && zip -r ../helloworld-layer.zip ../nodejs/",
"test": "jest"
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"jest-cli": "^24.9.0",
"moment": "^2.24.0"
"devDependencies": {
"jest": "^24.9.0"
"jest": {
"testEnvironment": "node",
"verbose": true

Create a two new files under /src

  • helloworld.js 
  • helloworld.text.js

In helloworld.js, add the following text

function myFunction() {
return "hello";

module.exports = myFunction;

in helloworld.text.js, add the following text

const directory = require('./directory');

var result = directory();

test('test helloworld', () => {

Run the Jest Test to verify it works

$ npm test

Run the npm build command to generate the helloworld-layer.zip file.  We'll upload the helloworld-layer.zip file to AWS

$ cd nodejs
$ npm run build

The following file should have been created:


Upload the new layer to AWS Lambda > Layers

  • Create Layer
    • Name:  my-layer
    • Choose > Upload a .zip file
    • Click Upload
    • Choose my-layer.zip

Create a test function to use the new my-layer layer

  • Create Function
    • Choose > Author from scratch
    • Function name:  test-lambda-layer
    • Runtime:  Node.js 10.x

Update function index.js file with the following source code:

const helloworld = require('/opt/nodejs/src/helloworld');

const result = helloworld();


exports.handler = async (event) => {
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify(`Hello from Lambda! ${result}`),
return response;

Add Visual Studio Launch Config for Jest Debugging.  You can follow these directions here at https://github.com/microsoft/vscode-recipes/tree/master/debugging-jest-tests

or you can do the following:

1) Click the Debug and Run icon from the left Navigation Bar.

2) Click the Gear icon next to "Debug and Run"

3) Paste the launch.json text.  

"version": "0.2.0",
"configurations": [
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true

4) You can then press the > start debugging icon.

Comments are closed