node package manager

marchio-lambda-post

marchio-lambda-post

REST POST to DynamoDB via Lambda

Downloads Version License

Installation

$ npm init
$ npm install marchio-lambda-post --save

Lambda Setup

References


Steps

Create Test Role

  • Browse to: https://console.aws.amazon.com/iam/
  • Click: Roles (from the left column)
  • Click: Create new role
  • Step 1: Select role type
  • For AWS Lambda, click: Select
  • Step 2 is automatically skipped
  • Step 3: Attach policy
  • Select both AmazonDynamoDB* policies
  • Click: Next Step
  • Create a name for the role (like lambda-db-test)
  • Click: Create role

Create Lambda Function

  • Browse to: https://console.aws.amazon.com/lambda
  • Click: Create a Lambda Function
  • Select: Blank Function
  • Click: Next
  • Name: marchio
  • Description: Marchio service
  • Runtime: Node.js 4.3
  • Set the Role values
  • Click: Next
  • Click: Create Function

Setup API Gateway

  • Browse to: https://console.aws.amazon.com/apigateway
  • Click: Create API
  • Select: New API
  • API name: marchio
  • Description: Marchio service
  • Click: Create API
  • Click on the slash (/)
  • Drop down: Actions
  • Select: Create Resource
  • Check: Configure as proxy resource
  • (Optionally enabled CORS)
  • Click: Create Resource
  • For Integration type select: Lambda Function Proxy
  • Lambda Region: For example: us-east-1
  • Lambda Function: marchio
  • Click: Save
  • Add Permission to Lambda Function: OK
  • Drop down: Actions
  • Select: Deploy API
  • Define a new stage (call it "test")
  • Click: Deploy
  • Save the Invoke URL

Create DynamoDB Table

  • Browse to: https://console.aws.amazon.com/dynamodb/
  • Click: Create Table
  • Table name: mldb
  • Primary key: eid
  • The type should be the default (string)
  • Click: Create
  • After some churning, click the Capacity tab
  • Set the Read / Write capacity units to 1 to save money while testing
  • Click: Save

Example and Deploy

See the deployment example located in the repo under:

  • examples/deploy

It contains a deployment script and an example lambda source file.

  • Install the dependencies by running:
$ npm install

To run the script you must first make it runnable:

$ chmod +x deploy-lambda.sh

To test:

  • Deploy the API via API Gateway
  • Create an environment variable called AWS_HOST_MARCHIO which is set to the invocation url
  • Test the deployment using curl:
$ curl -i -X POST -H "Content-Type: application/json" -d '{"email":"test@beta.com"}' $AWS_HOST_MARCHIO/test/marchio/mldb
  • The response should contain a 201 status code and a copy of the created record, along with its id (eid)
  • Browse the DynamoDB table to see the new record.

Modules

marchio-lambda-post

Module

marchio-lambda-post-factory

Factory module

marchio-lambda-post

Module

marchio-lambda-post-factory

Factory module

marchio-lambda-post-factory.create(spec) ⇒ Promise

Factory method It takes one spec parameter that must be an object with named parameters

Kind: static method of marchio-lambda-post-factory
Returns: Promise - that resolves to {module:marchio-lambda-post}

Param Type Description
spec Object Named parameters object
spec.event Object Lambda event
spec.context Object Lambda context
spec.callback function Lambda callback
spec.model Object Table model
[spec.filter] function A function that takes the original record and returns a {Promise} that resolves to a filtered record

Example (Usage example)

// Lambda root file 
"use strict";
 
var mlFactory = require('marcio-lambda-post'); 
 
var getRandomInt = function (min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
};
 
// Why not just demo hashing with bcrypt? 
// Because bcrypt requires installing on AWS Linux before packaging 
// That's beyond the scope of this example, so we fake it. 
 
function fakeHash( record ) {
   // Not a real hash function - do not used in production 
   return new Promise( (resolve, reject) => {
        if(!record) {
            return reject('record not defined');
        }
        if(!record.password) {
            return reject('record.password not defined');
        }
        // fake hashing - do not use in production 
        record.password = '$' + getRandomInt(10000, 10000000);
        resolve(record);
   });
}
 
exports.handler = function(event, context, callback) {
 
    var model = {
        name: 'mldb',   // must match DynamoDB table name 
        primary: 'eid', // primary key - cannot be reserved word (like uuid) 
        fields: {
            email:    { type: String, required: true },
            status:   { type: String, required: true, default: "NEW" },
            // Password will be (fake) hashed by filter before being saved 
            password: { type: String, select: false },  // select: false, exclude from query results 
        }
    };
 
    mlFactory.create({ 
        event: event, 
        context: context,
        callback: callback,
        model: model,
        filter: fakeHash
    })
    .catch(function(err) {
        callback(err);
    });
 };

Testing

To test:

  • Deploy the example (examples/deploy)

  • Go to the root folder and type (sans $):

    $ npm test


Repo(s)


Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.


Version History

Version 0.1.6

  • Updated documentation with filtered example

Version 0.1.5

  • Added filter support

Version 0.1.4

  • Fixed dependency issue

Version 0.1.3

  • Refactored db-post module

Version 0.1.2

  • Refactored db-post module

Version 0.1.1

  • Fixed some doc issues

Version 0.1.0

  • initial release