Steps to Success:
Last Updated - 2019-12-21
Steps Overview
- Create a Root Source Code Folder
- Add a nodejs folder to the Root Source Code Folder
- Initialize NPM in the nodejs folder
- Configure VSCode to run
- Add jest for unit testing
- Configure VSCode to Debug Jest Tests
- Create a custom JS code for use
- Create a AWS Lambda function to use the custom JS
Folder Structure
Our folder structure will end up looking like this:
/Source
/my-layer
/nodejs
/src
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:
/my-layer
/nodejs
/src
package.json
Open Visual Studio Code and add the nodejs folder to the Workspace
- Open Visual Studio Code
- Open the folder /my-layer
- Choose File > Add Folder to Workspace
- Choose File > Save Workspace As
- 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', () => {
expect(result).toBe('hello');
});
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:
/my-layer/helloworld-layer.zip
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();
console.log(result);
exports.handler = async (event) => {
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify(`Hello from Lambda! ${result}`),
};
return response;
};
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": [
"${workspaceFolder}",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"${file}",
"--config",
"jest.config.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true
}
]
}
4) You can then press the > start debugging icon.