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

lambda-time

v0.4.9

Published

Lightweight utilities to simplify AWS Lambda development efforts

Downloads

27

Readme

lambda-time

Build Status Dependencies node Coverage Status

NPM

Lightweight Node.js router to simplify AWS Lambda development activities.

Most of the Lambda tutorials you'll find out there on the Interweb can handle "Hello, world!" very well. Need another function? Just add it to Lambda. But when you move past the examples and attempt to create something meaningful, you might ask yourself: "Hmm, managing all these functions separately might become painful, so is it really a good idea?". Especially when they all require the same shared packages, and when many need to communicate with each other somehow.

With lambda-time, your Lambdas are as big or as small as they need to be. Common guidance is to create small, focused Lambdas. That is generally good advice, but small is as small does. The smaller the Lambda, the more likely it is that you'll need it to cooperate with other Lambdas.

Since it's pretty likely (since you're still reading this) that you're implementing your Lambdas with Node.js. It's also likely that even a relatively large Node.js API implementation will be significantly smaller and lighter than one implemented in Java. So feel free to combine multiple functions into a single lambda. Lambda-time makes it pretty easy.

There are a few things you'll need to understand to use lambda-time:

Event routing

The event router is a knock-off of other common routers out there, but with a twist. It favors configuration over code in a way similar to the popular hapi framework, although it's similarity to hapi ends with the routing model and some shared dependencies. lambda-time is far simpler, smaller, and less capable than hapi. But it works with Lambda.

With lambda-time, a route determines what happens when an event is received by Lambda. To define a route, register it when your Lambda is loaded:

var Lambda = require('lambda-time');
var Router = new Lamba();

Router.register([
 {
   path: '/greeting',
   method: 'GET',
   handler: function(event) {
     return 'Hello, world';
   }
 }
 ]);

function handler(event, context) {
  Router.route(event, context)
    .then((result) => {
        context.done(null, result);
      });
    .catch((error) => {
        context.done(error);
      })
}
module.exports = handler;

The server maintains a table of routes, and routes all events to registered event handlers.

Event handlers

An event handler responds to an event. It's about as simple as it can be. It takes an event and an AWS Lambda context as arguments, and returns a promise that resolves to a response. For now, each path/method combination will only support a single handler, and handlers must return a promise.

Promises

We prefer using bluebird. If you need another promise implementation, raise an issue.

Deploying your Lambda

Relatively simple deployments can be executed with gulp using the node-aws-lambda package. If your project is a bit more ambitious, and can use a continuous integration tool, deployment is probably better handled through the CI environment--especially if your function uses any native packages. For example, for free open source or private projects that use Travis, a Travis build file can be structured similar to this:

language: node_js
node_js:
- 4.3.2
sudo: required
dist: trusty
cache:
  directories: node_modules
before_script:
- npm install -g gulp
script: gulp test
after_success: gulp dist
deploy:
  provider: lambda
  skip_cleanup: true
  edge: true
  access_key_id:
    secure: <encrypted access key goes here>
  secret_access_key:
    secure: <encrypted secret goes here>
  function_name: <name of the Lambda function>
  handler_name: handler
  runtime: nodejs4.3
  role: <lambda execution role>
  zip_file: dist.zip

In the example above, the gulpfile.js contains directives pulled from the node-aws-lambda package to create a zip file that is uploaded to Lambda.

Setting up API Gateway

To use lambda-time, you'll need to set up resources and methods in the AWS API Gateway. Typically, lambda-time requires access to the request body, url parameters, headers, etc. Providing that access involves specification of a body mapping template in the integration request for the corresponding API. The json/params.txt file contains mapping rules that will provide your lambda, and lambda-time with that access.