Quick and Easy Node.JS AWS Lambda Project Organization

An easy and good way to organize an AWS Lambda project.

Project Structure

project-root-function-name
│   README.md
|   package.json
|   package-lock.json
│   .gitignore
|   .eslintrc.json
|
└───src
│   │   index.js
│   │   config.json
│   │
│   └───components
│       │   FooClass.js
│       │   BarClass.js
│       └───...
│   
└───test
    │   fooTest.js
    │   barTest.js
    └───...

Invoking and Deploying Function Locally

Add invocation npm scripts to your package.json. This will allow you to simulate an AWS invocation.

Invoking your function

npm run invoke

...
"scripts": {
	...
    "invoke": "node -e \"require('./index').handler(require('./test_event.json'),null)\"",
    ...
  },
  ...
Use test_event.json to pass arbitrary event to local invocation.

Deploying your function

npm run deploy

...
"scripts": {
	...
	"deploy": "zip -r function.zip .; aws lambda update-function-code --function-name <YOUR_FUNCTION_NAME> --zip-file fileb://function.zip;rm ./function.zip",    ...
  },
  ...
Deploying to AWS is easy.

This function will zip up your entire project folder, so you'll possibly be including a lot of files–including the entire contents of your node_modules folder–that are not actually needed. There are plenty of strategies not covered here for reducing your function deployment package size.