npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

marchio-lambda-get

v0.3.1

Published

REST GET from DynamoDB via Lambda

Downloads

8

Readme

marchio-lambda-get

REST GET from DynamoDB via Lambda

Installation

$ npm init
$ npm install marchio-lambda-get --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 AmazonDynamoDBReadOnlyAccess policiy
  • Click: Next Step
  • Create a name for the role (like lambda-db-get-only)
  • 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-get
  • Description: Marchio service
  • Runtime: Node.js 4.3
  • Set the Role
  • Role: Choose and existing role
  • Existing role: service-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-get
  • 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-get
  • 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

If you've already setup a demo for marchio-lambda-post then you may have this table already. If not, create it and load it with a few test records.

  • 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_GET which is set to the invocation url
  • Test the deployment using curl (substitute a valid eid value):
$ curl -i -X GET -H "Accept: applications/json" \
  $AWS_HOST_MARCHIO_GET/test/marchio-get/110ec58a-a0f2-4ac4-8393-c866d813b8d1
  • The response should contain a 200 status code and a copy of the record.

Modules

marchio-lambda-get

Module

marchio-lambda-get-factory

Factory module

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

| 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-get'); 

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 },  // return eid / primary partition in GET results
            // gid:      { type: String },  // return gid / primary sort in GET results
            email:    { type: String, required: true },
            status:   { type: String, required: true, default: "NEW" },
            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.1

  • updated deploy example

Version 0.3.0

  • removed model/table name from url
  • updated deploy-build examples

Version 0.2.2

  • removed comment from demo

Version 0.2.1

  • Added support for primary sort key

Version 0.2.0

  • Change model.primary to model.partition

Version 0.1.5

  • Updated doc and demo deploy to show how to return primary key if required

Version 0.1.4

  • Integrated module documentation into readme

Version 0.1.3

  • Updated role documentation

Version 0.1.2

  • Updated service to only return record and not Item wrapper.

Version 0.1.1

  • Fixed issue with object passed to DynamoDB.getItem

Version 0.1.0

  • initial release