Giter Site home page Giter Site logo

aws-lambda-utilities's Introduction

aws-lambda-utilities

Useful utilities and functions for aws lambda projects

Using the logger

The logger class uses Winston logger.

Importing the logger:

import { Logger } from 'aws-lambda-utilities';

Create a new instance of the logger class:

const logger = new Logger();

Log a info message:

logger.info('Received event');

Log a info message with an object:

logger.info('Received event', { event });

Log an error:

logger.error('Lambda error', { error });

Always wrap objects

Using the DefaultVars class

The DefaultVars class exposes a list of standard lambda environment variables.

DefaultVars exposes environment variables through the following methods:

awsProfile(), awsRegion(), nodeEnv(), awsLocalStackEndpoint(), lambdaFunctionName(), lambdaFunctionVersion(), logLevel(), isTest(), isAWS() and isProduction()

Example DefaultVars implementation

Import the class from

import { DefaultVars } from 'aws-lambda-utilities';

Destructure the methods you require:

const { isAWS, lambdaFunctionName, awsLocalStackEndpoint } = new DefaultVars();

Use it in code:

if(!isAWS()) {
  // ...
}

Extending DefaultVars and adding your own methods

Create a new class called EnvVars (recommended naming convention)

import { DefaultVars } from 'aws-lambda-utilities';

const { env } = process;

export class EnvVars extends DefaultVars {
  constructor() {
    super();
  }

  yourEnvVar(): string | undefined {
    return env.YOUR_ENV_VAR;
  }
}

Using the EnvVars class

Then, you can simply reference the EnvVars class instead of the DefaultVars class in your microservice, as the EnvVars class will have all the properties of DefaultVars and EnvVars.

Example usage:

import { EnvVars } from 'envVars';

const { yourEnvVar, isAWS } = new EnvVars();

aws-lambda-utilities's People

Contributors

thecarlo avatar

Watchers

 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.