lambda-starter icon indicating copy to clipboard operation
lambda-starter copied to clipboard

AWS Lambda Starter Kit with latest Babel 7, Jest 24 and Webpack 4.

Lambda Starter Kit

  • to enable the latest JavaScript ES6 language features and simplification of code with the class syntax
  • Webpack to bundle only production dependencies to a single file, watch for changes and eliminate dead code
  • AWS Sam Local to emulate API Gateway locally so you can test your Lambda functions without deploying to AWS
  • Jest to test and confirm code works during development and before deploying to production

Installation

cd YourLambdaFunctionDirectory
git clone [email protected]:buildbreakdo/aws-api-lambda.git .
npm install

Create

To ensure correct assignment of IAM, role and permissions the recomendation is to initialize the lambda function manually on AWS.

  1. Create a Lambda Function using the AWS Console
  2. Update function-name in package.json with the name of the function you just created:
// package.json
...
  "scripts": {
    ...
    "update": "aws lambda update-function-code --function-name MyNewLambdaFunctionName --zip-file fileb://dist.zip --publish",
    ...
  }
...
  1. Install aws-cli
  2. npm run deploy to build, run tests and update your function to the latest code

Run Scripts

Build

Use webpack to build/index.js:

npm run build

Deploy

Build, execute tests in continuous integration mode (bail on failure) and update your Lambda function with the latest code:

npm run deploy

Test

Run jest tests:

npm run test

Run jest tests in continuous integration mode (hard exit on test failure):

npm run test:ci

Run Jest tests continuously and watch for changes:

npm run test:watch

Debug Jest tests with Chrome browser:
  1. Insert debugger on a line in your test
  2. npm run test:debug
  3. Open Chrome and navigate to about:inspect

Watch

Monitor src folder for changes and rebuild when changes occur.

npm run watch

Start

Used to be the case that we had to deploy a lambda function to test what would actually happen in a production environment. This is no longer the case, now we can emulate API Gateway and AWS Lambda locally using the AWS-SAM-CLI from Amazon. The template.yml file in the root of the repository is the configuration used by SAM Local (points to code in build/index.js).

Local Development Prerequisites. Running functions locally with SAM Local requires Docker to be installed and running.

npm install -g aws-sam-local
npm start

Starts the AWS SAM Local development server on http://127.0.0.1:3000. Open http://127.0.0.1:3000 in your browser to execute your Lambda function.

Note: You only need to restart SAM CLI if you update your AWS SAM template.

SAM Local will watch your build directory for changes and reload when changes occur. Be sure to run npm run watch to monitor your src directory (where you will be writing code) to ensure SAM Local always has the latest build/index.js. Happy coding!

License

MIT