1.0.13 • Published 3 years ago

aws-lambda-utilities v1.0.13

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

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();

1.0.9

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago