0.0.1 • Published 5 years ago

lambda-bin v0.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

lambda-bin

NPM Version Build Status Codacy Badge Coverage Status Known Vulnerabilities FOSSA Status

Add non-standard binaries to serverless compute.

Overview

This module reduces the footprint of the run-time only part of the bin-minify module and adds a few AWS Lambda specific functions.

Still, this module's core functionality should work well for other serverless providers as well. Please open an issue if you come across limitations using this module with other serverless providers.

Install

$ npm install --save lambda-bin

Usage

const path = require('path');
const LambdaBin = require('lambda-bin');
const MY_LAMBDA_BIN_PATH = path.join('/', 'tmp', 'my_bin');

var myLambdaBin = new LambdaBin({
  targetPath: path.resolve(__dirname, path.join('bin', 'my_bin')),
  minPack: require('MY_MIN_PACK_FILE'),
});

myLambdaBin.applyMinPack(MY_LAMBDA_BIN_PATH).then((result) => {
  if (result.accepted) {
    // Setup the PATH environment variable
    myLambdaBin.setPath(MY_LAMBDA_BIN_PATH);
    // And maybe some other environment variables
    myLambdaBin.setEnv({
      ENV_VAR1: 'some value',
      ENV_VAR2: 'another value',
    }, true);
    // Or setup the PATH and other environment variables yourself...
  }
  // Binaries are ready to use
}, error => {
  throw new Error(`Binaries could not be correctly setup: ${error}`);
});

API

constructor (options)

Object new LambdaBin( Object )

options

  • Type: Object
  • Optional

The following are supported keys in the options JSON object. Any other keys are ignored.

targetPath
  • Type: string
  • Default: ./bin/bin-minify

Location of the actual binaries.

Note: Typically the binaries under targetPath are source controlled (and should be included in the npm module or Lambda package).

useSymlinks
  • Type: boolean

Any value passed will be replaced with false, which is required for bin-minify to work on AWS Lambda

minPack
  • Type: Object
  • Default: {}

Used to load a previously created minPack.

Note: A minPack is created using bin-minify stagingBin.createMinPack() and saved to a source controlled file (which should be included in the npm module or Lambda package).

Promise runtimeBin.applyMinPack (fromBase)

fromBase

  • Type: string
  • Required

Base path where the original file structure of the binaries will be recreated.

returns Promise

Resolved Promise: { loaded: true or false }. loaded will be:

  • true if the file structure was successfully created.
  • false if the fromBase path already existed.

Rejected Promise: { error }.

String setPath (pathsToAdd)

Once the original file structure of the binaries is recreated, it is necessary to add them to the PATH before the binaries can be invoked. setPath() can be used for this purpose.

pathsToAdd

  • Type: string or Array

The path(s) for the binaries. Typically these paths are children of fromBase and/or fromBase itself.

Note: These paths will be prepended to the system PATH.

returns String

The resulting PATH.

setEnv (variablesToSet, shouldOverwrite)

Some binaries rely on environment variables to control their behavior (e.g. NODE_PATH), or require existing environment variables to be updated (e.g. LD_LIBRARY_PATH). setEnv() can be used to setup environment variables.

variablesToSet

  • Type: Object

Key-values pairs representing the environment variables to add/change, where the key is the environment variable name and the value its associated value.

shouldOverwrite

  • Type: boolean

If true, any existing value will be replaced. If false the value provided will be appended to any existing value using ':'.

Tip: If it necessary to overwrite and append values, call setEnv() twice and change the value of shouldOverwrite accordingly.

AWS Lambda Limits

Please keep these AWS lambda limits in mind when deploying lambda functions.

License

MIT © BotBitsSM