lambda-local
Advanced tools
Comparing version 1.7.0 to 1.7.1
@@ -14,2 +14,3 @@ #!/usr/bin/env node | ||
program | ||
.name('lambda-local') | ||
.option('-l, --lambda-path <lambda index path>', '(required) Lambda function file name.') | ||
@@ -16,0 +17,0 @@ .option('-e, --event-path <path>', '(required) Event data file name.') |
@@ -10,2 +10,4 @@ 'use strict'; | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var os = require("os"); | ||
var utils = require("./lib/utils.js"); | ||
@@ -47,3 +49,13 @@ var Context = require("./lib/context.js"); | ||
; | ||
var _executeSync = function (opts) { | ||
function updateEnv(env) { | ||
/* | ||
* Update environment vars if not already in place | ||
*/ | ||
Object.keys(env).forEach(function (key) { | ||
if (!process.env[key]) { | ||
process.env[key] = env[key]; | ||
} | ||
}); | ||
} | ||
function _executeSync(opts) { | ||
var event = opts.event, lambdaFunc = opts.lambdaFunc, lambdaPath = opts.lambdaPath, lambdaHandler = opts.lambdaHandler || 'handler', profilePath = opts.profilePath, profileName = opts.profileName || process.env['AWS_PROFILE'] || process.env['AWS_DEFAULT_PROFILE'], region = opts.region, environment = opts.environment, envdestroy = opts.envdestroy, envfile = opts.envfile, callbackWaitsForEmptyEventLoop = opts.callbackWaitsForEmptyEventLoop || false, timeoutMs = opts.timeoutMs || 3000, verboseLevel = opts.verboseLevel, callback = opts.callback, clientContext = null; | ||
@@ -62,13 +74,26 @@ if (opts.clientContext) { | ||
} | ||
if (lambdaPath) { | ||
lambdaPath = utils.getAbsolutePath(lambdaPath); | ||
} | ||
// set environment variables before the require | ||
process.env['AWS_LAMBDA_FUNCTION_NAME'] = lambdaHandler; | ||
process.env['AWS_LAMBDA_FUNCTION_MEMORY_SIZE'] = "1024"; | ||
process.env['AWS_LAMBDA_FUNCTION_VERSION'] = "1.0"; | ||
process.env['AWS_EXECUTION_ENV'] = "AWS_Lambda_nodejs"; | ||
process.env['LAMBDA_CONSOLE_SOCKET'] = "14"; | ||
process.env['LAMBDA_CONTROL_SOCKET'] = "11"; | ||
process.env['LAMBDA_RUNTIME_DIR'] = process.cwd(); | ||
process.env['LAMBDA_TASK_ROOT'] = process.cwd(); | ||
process.env['NODE_PATH'] = utils.getAbsolutePath('node_modules'); | ||
process.env['TZ'] = "utc"; | ||
var envVars = { | ||
'AWS_LAMBDA_FUNCTION_NAME': lambdaHandler, | ||
'AWS_LAMBDA_FUNCTION_MEMORY_SIZE': Math.floor(os.freemem() / 1048576).toString(), | ||
'AWS_LAMBDA_FUNCTION_VERSION': "1.0", | ||
'AWS_EXECUTION_ENV': "AWS_Lambda_nodejs" + process.version.substr(1), | ||
'LAMBDA_CONSOLE_SOCKET': "14", | ||
'LAMBDA_CONTROL_SOCKET': "11", | ||
'LAMBDA_RUNTIME_DIR': process.cwd(), | ||
'NODE_PATH': utils.getAbsolutePath('node_modules'), | ||
'TZ': Intl.DateTimeFormat().resolvedOptions().timeZone | ||
}; | ||
if (lambdaPath) { | ||
envVars['LAMBDA_TASK_ROOT'] = path.dirname(lambdaPath); | ||
envVars['_HANDLER'] = path.basename(lambdaPath, path.extname(lambdaPath)) + "." + lambdaHandler; | ||
} | ||
else { | ||
envVars['LAMBDA_TASK_ROOT'] = process.cwd(); | ||
envVars['_HANDLER'] = "index." + lambdaHandler; | ||
} | ||
updateEnv(envVars); | ||
// custom environment variables | ||
@@ -131,4 +156,4 @@ if (environment != null) { | ||
// delete this function from the require.cache to ensure every dependency is refreshed | ||
delete require.cache[utils.getAbsolutePath(lambdaPath)]; | ||
lambdaFunc = require(utils.getAbsolutePath(lambdaPath)); | ||
delete require.cache[lambdaPath]; | ||
lambdaFunc = require(lambdaPath); | ||
} | ||
@@ -155,2 +180,3 @@ //load event | ||
} | ||
}; | ||
} | ||
; |
@@ -8,3 +8,3 @@ 'use strict'; | ||
var utils = require("./utils.js"); | ||
var mute = require("mute"); | ||
var mute = require("./mute.js"); | ||
function Context() { | ||
@@ -32,8 +32,8 @@ this.logger = null; | ||
this.functionName = ''; | ||
this.functionVersion = '1'; | ||
this.invokedFunctionArn = 'a'; | ||
this.memoryLimitInMB = 1; | ||
this.awsRequestId = ''; | ||
this.logGroupName = 'a'; | ||
this.logStreamName = null; | ||
this.functionVersion = process.env.AWS_LAMBDA_FUNCTION_VERSION; | ||
this.invokedFunctionArn = null; | ||
this.memoryLimitInMB = parseInt(process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE); | ||
this.awsRequestId = null; | ||
this.logGroupName = 'Group name'; | ||
this.logStreamName = 'Stream name'; | ||
this.identity = null; | ||
@@ -70,4 +70,4 @@ this.clientContext = null; | ||
'lambda', | ||
'region', | ||
'account-id', | ||
process.env.AWS_REGION, | ||
Math.round(Math.random() * 1000000000000).toString(), | ||
'function', | ||
@@ -74,0 +74,0 @@ this.functionName, |
# ChangeLog | ||
## 1.7.1 (2020-01/28) | ||
* Add support for `_HANDLER` | ||
* Update environment variables computation & consistency with context | ||
## 1.7.0 (2020-01/17) | ||
@@ -4,0 +8,0 @@ * Migrate to TypeScript (#191) |
{ | ||
"name": "lambda-local", | ||
"version": "1.7.0", | ||
"version": "1.7.1", | ||
"description": "Commandline tool to run Lambda functions on your local machine.", | ||
@@ -39,14 +39,13 @@ "main": "build/lambdalocal.js", | ||
"dependencies": { | ||
"@types/node": "^13.1.7", | ||
"aws-sdk": "^2.488.0", | ||
"commander": "^4.1.0", | ||
"dotenv": "^8.0.0", | ||
"mute": "^2.0.6", | ||
"typescript": "^3.7.5", | ||
"winston": "^3.2.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^13.1.7", | ||
"chai": "^4.2.0", | ||
"mocha": "^7.0.0", | ||
"sinon": "^8.1.0" | ||
"sinon": "^8.1.0", | ||
"typescript": "^3.7.5" | ||
}, | ||
@@ -53,0 +52,0 @@ "preferGlobal": true, |
154
README.md
@@ -8,7 +8,8 @@ # 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 | ||
@@ -22,61 +23,7 @@ | ||
### As a command line tool | ||
- **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. | ||
You can use Lambda-local as a command line tool. | ||
If you're unsure about some definitions, see [Definitions](#about-definitions) for terminology. | ||
```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. | ||
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) | ||
## About: API | ||
@@ -91,21 +38,29 @@ | ||
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)` | ||
@@ -170,2 +125,51 @@ #### `lambdaLocal.getLogger()` | ||
## 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 | ||
@@ -172,0 +176,0 @@ |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 10 instances in 1 package
51987
4
30
1104
186
26
0
5
- Removed@types/node@^13.1.7
- Removedmute@^2.0.6
- Removedtypescript@^3.7.5
- Removed@types/node@13.13.52(transitive)
- Removedmute@2.0.6(transitive)
- Removedtypescript@3.9.10(transitive)