Code Monkey home page Code Monkey logo

aws-lambda-handler's Introduction

*nix build status Windows build status Tests coverage

aws-lambda-handler

Essential AWS Lambda handler setup

When relying directly on basic AWS interface we're immune to handler setup errors which are not transparently reported by AWS:

  • Using callback resolution we may invoke callback twice (and we're not restricted from invoking callback coming from previous invocation). All those superfluous invocations are silently ignored by AWS.
  • Using promise resolution, invocation is resolved immediately after returned promise resolves. This leaves us with no feedback of eventually orphaned async flows (which may be result of typical errors as omitted await or return) and which are eventually executed in next lambda invocation

This handler makes above errors either impossible to make or properly exposed, and additionally:

  • Enforces simple input/output function setup (no matter whether sync or promise returning)
  • Rejects any resolution attemps via context.done or it's affiliates
  • Prevents direct setting of context.callbackWaitsForEmptyEventLoop, as having it false may leak scheduled tasks between lambda invocations.
  • Exposes get-current-context module, which provides out of band access to current invocation context.

Usage

Handler configuration

module.exports = require("aws-lambda-handler")((event, context) => {
    // ...lambda logic
    return result;
});

Above module exports lambda logic on handler property

Out of band context access

some-outer-module-down-the-path.js

const getCurrentContext = require("aws-lambda-handler/get-current-context");

module.exports = (...) => {
  // Retrieve invocation context
  const invocationContext = getCurrentContext();
  if (invocationContext.getRemainingTimeInMillis() > 1000 * 60) {
    // We have time
  } else {
    throw new Error("Not enough time");
  }
}

Tests

npm test

aws-lambda-handler's People

Contributors

medikoo avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.