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

@erikmuir/lambda-utils

v1.0.0

Published

A collection of AWS Lambda utilities

Downloads

7

Readme

lambda-utils

AWS Lambda utilities for NodeJS

npm (scoped)   npm   install size   NPM   GitHub last commit   Snyk Vulnerabilities for npm package   CodeFactor Grade   codecov.io Code Coverage

Installation

$ npm install @erikmuir/lambda-utils --save

Usage

Utilities

LambdaLogger

LambdaLogger exposes five logging methods: trace, debug, info, warn, and error, all of which respect the current log level. So, for example, if the current log level is "info", then a call to trace() or debug() would not actually log anything.

The LambdaLogger also provides structured logging for your AWS Lambda functions, which allows you to more easily query your logs in tools like CloudWatch Log Insights, or even third-party tools like Splunk. This enables you to create insightful dashboards that give visibility into your distributed, serverless applications.

const { LambdaLogger } = require('@erikmuir/lambda-utils');

function myFunction(arg1, arg2) {
  const logger = new LambdaLogger('myFunction');
  logger.info("Hello world!", { arg1, arg2 });
}

If the above function were invoked like myFunction(42, true), it would produce the following structured log:

{
  "time": "2020-08-09T22:37:24Z",
  "level": "info",
  "logger": "myFunction",
  "message": "Hello world!",
  "data": {
    "arg1": 42,
    "arg2": true
  },
  // ...and event/context properties, if they were set on LogEnv
}

LogLevel

This is an enumeration consisting of all the available log levels. Their values are used when implementing a logging threshhold. Each log level has a corresponding method on the LambdaLogger class.

| Level | Value | |-------|:-----:| | trace | 10 | | debug | 20 | | info | 30 | | warn | 40 | | error | 50 |

LogEnv

This is a singleton that is used by the LambdaLogger. The first thing you should do in your function's handler is to initialize the Lambda environment so that the structured logs can have access to the info contained in the event and context.

const { LogEnv } = require('@erikmuir/lambda-utils');

exports.handler = async function (event, context) {
  LogEnv.initializeLambdaEnvironment({ event, context });
  // the rest of your code goes here
}

The LambdaLogger also uses the LogEnv singleton to get the current log level, however it cannot be set directly. It tries to get the value of an environment variable called LOG_LEVEL. If it doesn't exist, it will default to info.

LambdaResponse

The LambdaResponse class has the following properties: statusCode, isBase64Encoded, body, and headers. The first three properties have normal getters and setters, but headers must be added one at a time via the addHeader method. Note: Adding a header with the same key as an existing header will result in the original value being replaced with the new value.

const { Header } = require('@erikmuir/node-utils');
const { LambdaResponse } = require('@erikmuir/lambda-utils');

const response = new LambdaResponse();

response.statusCode = 200;
response.isBase64Encoded = false;
response.body = { message: 'Success!' };

var header1 = new Header('key1', 'value1');
var header2 = new Header('key2', 'value2');

response.addHeader(header1);
response.addHeader(header2);

When you're ready to return the response, you'll need to call the .build() method. This builds the response object that AWS Lambda expects to receive. Note: If isBase64Encoded is false, the body will be JSON stringified.

return response.build();

This will return the following object:

{
  "statusCode": 200,
  "headers": {
    "key1": "value1",
    "key2": "value2"
  },
  "body": "{\"message\":\"Success!\"}"
}