marchio-lambda-patch

0.3.2 • Public • Published

marchio-lambda-patch

REST PATCH to DynamoDB via Lambda

Continuous Integration Coverage Status Downloads Version License

Installation

$ npm init
$ npm install marchio-lambda-patch --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
  • Expand Section: AWS Service Role
  • 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-patch)
  • 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-patch
  • Description: Marchio service
  • Runtime: Node.js 4.3
  • Set the Role
  • Role: Choose an existing role
  • Existing role: (name of role you created earlier)
  • 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-patch
  • 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-patch
  • 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_PATCH which is set to the invocation url
  • Test the deployment using curl:
$ curl -i -X PATCH -H "Content-Type: application/json" \
   -d '[{"op":"replace","path":"/status","value":"PATCH"}]' \
   $AWS_HOST_MARCHIO_PATCH/test/marchio-patch/ac7e281f-33f9-4872-9c06-5703999e2d42
  • The response should contain a 204 status code.
  • Browse the DynamoDB table to see the updated record.

Modules

marchio-lambda-patch

Module

marchio-lambda-patch-factory

Factory module

marchio-lambda-patch

Module

marchio-lambda-patch-factory

Factory module

marchio-lambda-patch-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-patch-factory
Returns: Promise - that resolves to {module:marchio-lambda-patch}

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

Example (Usage example)

// Lambda root file
"use strict";

var mlFactory = require('marcio-lambda-patch'); 

exports.handler = function(event, context, callback) {

    var model = {
        name: 'mldb',   // must match DynamoDB table name
        partition: 'eid', // primary partition key - cannot be reserved word (like uuid)
        // sort: 'gid',
        fields: {
            eid:      { type: String },
               // gid:      { type: String },
            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
    })
    .catch(function(err) {
        callback(err);
    });
 };

Testing

To test, 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.3.2

  • updated test case

Version 0.3.1

  • updated deploy example with latest build

Version 0.3.0

  • removed model/table name from url
  • updated example deployment url's

Version 0.2.1

  • updated deploy example

Version 0.2.0

  • model.primary is now model.partition
  • now supports databases with a sort key

Version 0.1.1

  • added description to package.json

Version 0.1.0

  • initial release

Readme

Keywords

none

Package Sidebar

Install

npm i marchio-lambda-patch

Weekly Downloads

3

Version

0.3.2

License

MIT

Last publish

Collaborators

  • mitchallen