项目作者: ErikMuir

项目描述 :
AWS Lambda utilities for NodeJS
高级语言: JavaScript
项目地址: git://github.com/ErikMuir/lambda-utils.git
创建时间: 2020-08-08T04:03:54Z
项目社区:https://github.com/ErikMuir/lambda-utils

开源协议:MIT License

下载


lambda-utils

AWS Lambda utilities for NodeJS

@erikmuir/lambda-utils" alt="npm (scoped)">

@erikmuir/lambda-utils" alt="npm">

install size

@erikmuir/lambda-utils" alt="NPM">

GitHub last commit

@erikmuir/lambda-utils" alt="Snyk Vulnerabilities for npm package">

CodeFactor Grade

codecov.io Code Coverage

Installation

  1. $ 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.

  1. const { LambdaLogger } = require('@erikmuir/lambda-utils');
  2. function myFunction(arg1, arg2) {
  3. const logger = new LambdaLogger('myFunction');
  4. logger.info("Hello world!", { arg1, arg2 });
  5. }

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

  1. {
  2. "time": "2020-08-09T22:37:24Z",
  3. "level": "info",
  4. "logger": "myFunction",
  5. "message": "Hello world!",
  6. "data": {
  7. "arg1": 42,
  8. "arg2": true
  9. },
  10. // ...and event/context properties, if they were set on LogEnv
  11. }

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.

  1. const { LogEnv } = require('@erikmuir/lambda-utils');
  2. exports.handler = async function (event, context) {
  3. LogEnv.initializeLambdaEnvironment({ event, context });
  4. // the rest of your code goes here
  5. }

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.

  1. const { Header } = require('@erikmuir/node-utils');
  2. const { LambdaResponse } = require('@erikmuir/lambda-utils');
  3. const response = new LambdaResponse();
  4. response.statusCode = 200;
  5. response.isBase64Encoded = false;
  6. response.body = { message: 'Success!' };
  7. var header1 = new Header('key1', 'value1');
  8. var header2 = new Header('key2', 'value2');
  9. response.addHeader(header1);
  10. 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.

  1. return response.build();

This will return the following object:

  1. {
  2. "statusCode": 200,
  3. "headers": {
  4. "key1": "value1",
  5. "key2": "value2"
  6. },
  7. "body": "{\"message\":\"Success!\"}"
  8. }