Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 1.7.1 #193

Merged
merged 15 commits into from
Feb 1, 2020
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
npm-debug.log
.DS_Store
build/

1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: node_js
node_js:
- "6"
- "8"
- "10"
- "11"
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# ChangeLog

## 1.7.1 (2020-01/28)
* Add support for `_HANDLER`
* Update environment variables computation & consistency with context

## 1.7.0 (2020-01/17)
* Migrate to TypeScript (#191)

## 1.6.3 (2019/07/04)
* Security updates (update dependencies that had vulnerabilities: [mocha](https://github.com/mochajs/mocha/blob/master/CHANGELOG.md#614--2019-04-18)).
* Pick up AWS profile from env `AWS_PROFILE` or `AWS_DEFAULT_PROFILE` (thanks to @illusori)
Expand Down
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
install: node_modules
install: build

test: node_modules
test: install
cd test && ../node_modules/mocha/bin/mocha test.js

clean:
Expand All @@ -9,5 +9,9 @@ clean:
node_modules: package.json
npm install

.PHONY: test install clean
deadwood := node_modules package-lock.json
build: tsconfig.json node_modules
npm install --only=dev
npm run build

.PHONY: test install clean build
deadwood := node_modules package-lock.json build
154 changes: 79 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
[![Build Status](https://travis-ci.org/ashiina/lambda-local.svg?branch=develop)](https://travis-ci.org/ashiina/lambda-local)
[![Known Vulnerabilities](https://snyk.io/test/github/ashiina/lambda-local/badge.svg)](https://snyk.io/test/github/ashiina/lambda-local)

Lambda-local lets you test Amazon Lambda functions on your local machine, by providing a simplistic API and command-line tool.
Lambda-local lets you test **NodeJS Amazon Lambda functions** on your local machine, by providing a simplistic API and command-line tool.

The `context` of the Lambda function is already loaded so you do not have to worry about it.
The calls are fully customizable, as you can pass any `event` (JSON) object to any `handler` function.
It does not aim to be perfectly feature proof as projects like [serverless-offline](https://github.com/dherault/serverless-offline ) or [docker-lambda](https://github.com/lambci/docker-lambda), but rather to remain **very light** (it still provides a fully built `Context`, handles all of its parameters and functions, and everything is customizable easily).

The main target is unit tests.

## Install

Expand All @@ -18,64 +19,10 @@ npm install -g lambda-local

## Usage

### As a command line tool

You can use Lambda-local as a command line tool.

```bash
# Simple usage
lambda-local -l index.js -h handler -e examples/s3-put.js

# Input environment variables
lambda-local -l index.js -h handler -e examples/s3-put.js -E '{"key":"value","key2":"value2"}'

```

### In another node.js script


You can also use Lambda local directly in a script. For instance, it is interesting in a [MochaJS][1] test suite in order to get test coverage.
- **As an API:** You can also use Lambda local directly in a script. For instance, it is interesting in a [MochaJS][1] test suite in order to get test coverage.
- **As a command line tool:** You can use Lambda-local as a command line tool.

See [API](#about-api) for more infos

## About: Definitions

### Event data
Event sample data are placed in `examples` folder - feel free to use the files in here, or create your own event data.
Event data are just JSON objects exported:

```js
// Sample event data
module.exports = {
foo: "bar"
};
```

### Context
The `context` object has been sampled from what's visible when running an actual Lambda function on AWS, and the [available documentation](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html)
They may change the internals of this object, and Lambda-local does not guarantee that this will always be up-to-date with the actual context object.

### AWS-SDK
Since the Amazon Lambda can load the AWS-SDK npm without installation, Lambda-local has also packaged AWS-SDK in its dependencies.
If you want to use this, please use the `-p` or `-P` options (or their API counterpart) with the aws credentials file. More infos here:
http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-config-files

## About: CLI

### Available Arguments
* `-l, --lambda-path <lambda index path>` (required) Specify Lambda function file name.
* `-e, --event-path <event path>` (required) Specify event data file name.
* `-h, --handler <handler name>` (optional) Lambda function handler name. Default is "handler".
* `-t, --timeout <timeout>` (optional) Seconds until lambda function timeout. Default is 3 seconds.
* `-r, --region <aws region>` (optional) Sets the AWS region, defaults to us-east-1.
* `-P, --profile-path <aws profile name>` (optional) Read the specified AWS credentials file.
* `-p, --profile <aws profile name>` (optional) Use with **-P**: Read the AWS profile of the file.
* `-E, --environment <JSON {key:value}>` (optional) Set extra environment variables for the lambda
* `--wait-empty-event-loop` (optional) Sets callbackWaitsForEmptyEventLoop=True => will wait for an empty loop before returning. This is false by default because our implementation isn\'t perfect and only "emulates" it.
* `--envdestroy` (optional) Destroy added environment on closing. Defaults to false
* `-v, --verboselevel <3/2/1/0>` (optional) Default 3. Level 2 dismiss handler() text, level 1 dismiss lambda-local text and level 0 dismiss also the result.
* `--envfile <path/to/env/file>` (optional) Set extra environment variables from an env file
* `--inspect [[host:]port]` (optional) Starts lambda-local using the NodeJS inspector (available in nodejs > 8.0.0)
If you're unsure about some definitions, see [Definitions](#about-definitions) for terminology.

## About: API

Expand All @@ -86,24 +33,32 @@ API accessible with:
const lambdaLocal = require("lambda-local");
```

Or on TypeScript (supported on 1.7.0+):

```js
import lambdaLocal = require("lambda-local");
```

#### `lambdaLocal.execute(options)`

Executes a lambda given the `options` object, which is a dictionary where the keys may be:
- `event` - requested event as a json object
- `lambdaPath` - requested path to the lambda function
- `lambdaFunc` - pass the lambda function. You cannot use it at the same time as lambdaPath
- `profilePath` - optional, path to your AWS credentials file
- `profileName` - optional, aws profile name. Must be used with
- `lambdaHandler` - optional handler name, default to `handler`
- `region` - optional, AWS region, default to `us-east-1`
- `callbackWaitsForEmptyEventLoop` - optional, default to `false`. Setting it to True will wait for an empty loop before returning.
- `timeoutMs` - optional, timeout, default to 3000 ms
- `environment` - optional, extra environment variables for the lambda
- `envfile` - optional, load an environment file before booting
- `envdestroy` - optional, destroy added environment on closing, default to false
- `verboseLevel` - optional, default 3. Level 2 dismiss handler() text, level 1 dismiss lambda-local text and level 0 dismiss also the result.
- `callback` - optional, lambda third parameter [callback][1]. When left out a Promise is returned
- `clientContext` - optional, used to populated clientContext property of lambda second parameter (context)

| Key name | Description |
| --- | --- |
| `event`|requested event as a json object|
| `lambdaPath`|requested path to the lambda function|
| `lambdaFunc`|pass the lambda function. You cannot use it at the same time as lambdaPath|
| `profilePath`|optional, path to your AWS credentials file|
| `profileName`|optional, aws profile name. Must be used with |
| `lambdaHandler`|optional handler name, default to `handler`|
| `region`|optional, AWS region, default to `us-east-1`|| `callbackWaitsForEmptyEventLoop`|optional, default to `false`. Setting it to True will wait for an empty loop before returning.|
| `timeoutMs`|optional, timeout, default to 3000 ms|
| `environment`|optional, extra environment variables for the lambda|
| `envfile`|optional, load an environment file before booting|
| `envdestroy`|optional, destroy added environment on closing, default to false|
| `verboseLevel`|optional, default 3. Level 2 dismiss handler() text, level 1 dismiss lambda-local text and level 0 dismiss also the result.|
| `callback`|optional, lambda third parameter [callback][1]. When left out a Promise is returned|
| `clientContext`|optional, used to populated clientContext property of lambda second parameter (context)

#### `lambdaLocal.setLogger(logger)`
#### `lambdaLocal.getLogger()`
Expand Down Expand Up @@ -164,6 +119,55 @@ lambdaLocal.execute({
});
```

## About: CLI

### Available Arguments
* `-l, --lambda-path <lambda index path>` (required) Specify Lambda function file name.
* `-e, --event-path <event path>` (required) Specify event data file name.
* `-h, --handler <handler name>` (optional) Lambda function handler name. Default is "handler".
* `-t, --timeout <timeout>` (optional) Seconds until lambda function timeout. Default is 3 seconds.
* `-r, --region <aws region>` (optional) Sets the AWS region, defaults to us-east-1.
* `-P, --profile-path <aws profile name>` (optional) Read the specified AWS credentials file.
* `-p, --profile <aws profile name>` (optional) Use with **-P**: Read the AWS profile of the file.
* `-E, --environment <JSON {key:value}>` (optional) Set extra environment variables for the lambda
* `--wait-empty-event-loop` (optional) Sets callbackWaitsForEmptyEventLoop=True => will wait for an empty loop before returning. This is false by default because our implementation isn\'t perfect and only "emulates" it.
* `--envdestroy` (optional) Destroy added environment on closing. Defaults to false
* `-v, --verboselevel <3/2/1/0>` (optional) Default 3. Level 2 dismiss handler() text, level 1 dismiss lambda-local text and level 0 dismiss also the result.
* `--envfile <path/to/env/file>` (optional) Set extra environment variables from an env file
* `--inspect [[host:]port]` (optional) Starts lambda-local using the NodeJS inspector (available in nodejs > 8.0.0)

### CLI examples

```bash
# Simple usage
lambda-local -l index.js -h handler -e examples/s3-put.js

# Input environment variables
lambda-local -l index.js -h handler -e examples/s3-put.js -E '{"key":"value","key2":"value2"}'
```

## About: Definitions

### Event data
Event sample data are placed in `examples` folder - feel free to use the files in here, or create your own event data.
Event data are just JSON objects exported:

```js
// Sample event data
module.exports = {
foo: "bar"
};
```

### Context
The `context` object has been sampled from what's visible when running an actual Lambda function on AWS, and the [available documentation](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html)
They may change the internals of this object, and Lambda-local does not guarantee that this will always be up-to-date with the actual context object.

### AWS-SDK
Since the Amazon Lambda can load the AWS-SDK npm without installation, Lambda-local has also packaged AWS-SDK in its dependencies.
If you want to use this, please use the `-p` or `-P` options (or their API counterpart) with the aws credentials file. More infos here:
http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-config-files

## Other links

- If you are willing to test an app based on the ASK-SDK, have a look at https://github.com/taimos/ask-sdk-test
Expand Down
7 changes: 0 additions & 7 deletions index.js

This file was deleted.

32 changes: 21 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
{
"name": "lambda-local",
"version": "1.6.3",
"version": "1.7.1",
"description": "Commandline tool to run Lambda functions on your local machine.",
"main": "build/lambdalocal.js",
"types": "build/lambdalocal.d.ts",
"bin": {
"lambda-local": "build/cli.js"
},
"scripts": {
"test": "mocha",
"build": "tsc --declaration"
},
"files": [
"*.json",
"build",
"examples",
"Makefile"
],
"keywords": [
"lambda",
"amazon",
Expand All @@ -22,23 +37,18 @@
},
"dependencies": {
"aws-sdk": "^2.488.0",
"commander": "^2.20.0",
"commander": "^4.1.0",
"dotenv": "^8.0.0",
"mute": "^2.0.6",
"winston": "^3.2.1"
},
"devDependencies": {
"@types/node": "^13.1.7",
"chai": "^4.2.0",
"mocha": "^6.1.4",
"sinon": "^7.3.2"
},
"scripts": {
"test": "mocha"
"mocha": "^7.0.0",
"sinon": "^8.1.0",
"typescript": "^3.7.5"
},
"preferGlobal": true,
"bin": {
"lambda-local": "./bin/lambda-local"
},
"repository": {
"type": "git",
"url": "https://github.com/ashiina/lambda-local.git"
Expand Down
8 changes: 4 additions & 4 deletions bin/lambda-local → src/cli.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

'use strict';

import lambdaLocal = require('./lambdalocal');
import utils = require('./lib/utils');

/*
* Local executor for Amazon Lambda function
*/
(function() {

var lambdaLocal = require('../lib/lambdalocal.js'),
utils = require('../lib/utils.js');

var logger = lambdaLocal.getLogger();

// process opts
var program = require('commander');
program
.name('lambda-local')
.option('-l, --lambda-path <lambda index path>', '(required) Lambda function file name.')
.option('-e, --event-path <path>', '(required) Event data file name.')
.option('-h, --handler <handler name>',
Expand Down
Loading