njax-aws-lambda

0.0.2 • Public • Published

njax-aws-lambda

This is a tool kit that allows

##API: ###Install:

npm install -S njax-aws-lambda

###Setting up your app:

var NJaxLambda = require('njax-aws-lambda');
var app = NJaxLambda();

###Set Up A Lambda: This will create a LambdaRouter. This acts similar to an express router for the lambda.

var eventsLambda = app.lambda('myFirstLambda');

###Set Up A APIGateway route: ####Single Default Method:

lambda.get('/some/uri', './rel/path/to/lambda');

The contents on ./rel/path/to/lambda can be found below in the Lambda Module Setup section.

####Single Custom Method:

lambda.get('/some/uri', './rel/path/to/lambda#method');

####Multiple Custom Methods:

lambda.get('/some/uri', [
    './rel/path/to/lambda#methodA',
    './rel/path/to/lambda#methodB',
    './rel/path/to/lambda#methodC'
]);

###Kinesis Usage: ####Basic Usage: The following will funnel all events to a single lambda

lambda.kinesis(
    'arn:aws:kinesis:EXAMPLE',
    [
        './rel/path/to/lambda#run'
    ]
);

####Advanced Usage: The following will filter out by event. WARNING: THIS IS REALLY EXPERIMENTAL AND UNDOCUMENTED

lambda.kinesis(
    'arn:aws:kinesis:EXAMPLE#some:event:name',
    [
        './rel/path/to/lambda#run'
    ]
);

###Lambda Module Setup: Inside of the actually Lambda Module (In these examples has lived at ./rel/path/to/lambda you will need to put the code that actually fires off.

####Basic Setup: The code is the same basically as typical lambda code.

module.exports = {
    run: function (event, context, next) {
        //Do stuff

    }
}

####APIGateway Lambda Event Structure: When setting a Lambda to be triggered by APIGateway the event has been modeled to roughly resemble the req param you see in middleware.

module.exports = {
    run: (event, context, next) => {
        //Do stuff
        console.log(event);//CONTENTS BELOW
    }
}
/*
{
    method: 'get',
    headers: {
        'Content-Type':'application/json'
    },
    query: {
        query_stirng_var:true
    },
    body: {
        post_body_vars:'true'
    },
    params: {
        stuff:'123',
        pulled:'456',
        out:'789'
        'of_the_uri_path':'bananas
    },
    route:{
        path: /* the route in api gateway
    }
}
*/

NOTE: We are trying to add as many of these as possible to make it as close to express as possible. Feel free to fork this project and add some if you like.

####Requireing Files: Since AWS Lambda don't install their own dependencies and you will need to define which dependencies you want packaged with the module: NOTE: These are relative to the root of the project

module.exports = {
    _requires: {
        '/node_modules/underscore*': true,
        '/node_modules/request*': true,
        '/lib/lambdas/some/sub/dir/*': true,
    },
    run: (event, context, next) => {
        //Do stuff

    }
}

####On Boot: Some times you want to run things only once when the Lambda is booted up in memory. Things like connecting to a service like Redis. For this you can use the Lambda Module's _onBoot property.

const Redis = require('redis');
module.exports = {
    _onBoot: (config) =>{
        this.redis = Redis.createClient(config);
    },
    _requires: {
        '/node_modules/redis*': true,
        /* ... */
    },
    run: (event, context, next) => {
        //Do stuff

    }
}

NOTE: A config parameter is passed in. See the Configuration Files and Environments section for details on that.

###Lambda Configuration Files and Environments: When you start a project for local dev or when you deploy a project you can set the Environment. Using the command line tool below that is set with the -e argument.

This will require the .js file at the following path:

{project root}/config/{environment}.js

This way you can have different configurations based on the environment.

##Command Line Tool Usage: ###Installing:

npm install njax-aws-lambda -g

###Initializing a Project: Go to the root of your project and run.

nlambda init

This will create a folder called _lambda at the root of your project. This folder contains the following:

  • Configurations specific to your project
  • The build directory of your Lambdas for when they get packaged into Zip to be uploaded to AWS.

####./_lambda/config.json: NOTE: The plan is to move as much of this manual config into the nlambda init setup. Were not there quite yet but hang with us

{
   "region": "us-east-1",
   "apigateway": "...",
    "role": "arn:aws:iam::...",//The
}
  • region - The AWS Region your lambdas will live in
  • apigateway - The APIGateway ID that will proxy HTTP/HTTPS requests to your lambda
  • role - An IAM Role that your lambda will be acting on behalf of.

It will also create a lambdas property automatically. That keeps some basic info on each of the Lambda's you created earlier.

###Running Locally: Navigate to the root of the application and run the following:

nlambda start

It should start a local express server that mimics the URL structure you have for the APIGateway

###Deploying the Lambda Module: The following will package the code for the lambdas into a zip and push the code for the lambdas to the servers

nlambda deploy -e production -l myFirstLambda

_NOTE: This does NOT setup the Triggers for the Lambdas. This only pushes the code. Use the following to push setup the Triggers. ###Deploying the Lambdas' triggers: This sets up the following:

  • Any associations between the APIGateway and your Lambdas
  • Any association between kinesis and your Lambdas
nlambda deploy-gateway -e production -l myFirstLambda

##TODO:

  • nlambda export-swagger

###Notes:

Package Sidebar

Install

npm i njax-aws-lambda

Weekly Downloads

0

Version

0.0.2

License

Apache-2.0

Last publish

Collaborators

  • schematical