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

aws-lambda-framework

v0.4.30

Published

A framework for simplifying writing AWS Lambda functions in typescript featuring IoC with services for input validation, sending slack notifications, and using AWS services

Downloads

54

Readme

AWS Lambda Framework

npm build coverage dependencies vulnerabilities license PRs Welcome Join the chat at https://gitter.im/aws-lambda-framework/community

A framework for simplifying writing AWS Lambda functions in typescript featuring IoC with services for input validation, sending slack notifications, connecting to databases, and using AWS services.

For release notes, see the CHANGELOG

This project is still in the early stages, any feedback is much appreciated. Please let me know of any services or features you feel could improve this framework!

Motivation

When developing microservice architectures on aws, a lot of common functionality has to be implemented into each Lambda function. This framework has been developed to provide that scaffolding as well streamline error handling and responses, such that anyone using Lambda functions through an API can always expect the same output.

Installation

npm i aws-lambda-framework --save

Usage

To utilize the framework your Lambda functions should extend the BaseLambda abstract class. This class provides scaffolding for your Lambda functions, ensuring that any results or errors are properly formatted for APIGateway and sent back to the caller. Errors are automatically logged and optionally send to a Slack channel of your choice. New Lambda functions must implement an invoke function, which ensures that the previously mentions points occur. This function also returns a LambdaResult to standardize the results for the end user, containing a userMessage and optionally a data object.

Basic example

// class TestLambda.ts

import {
  BaseLambda,
  LambdaContainer,
  APIGatewayProxyEvent,
  Context,
  APIGatewayProxyResult,
  LambdaResult,
  Mysql,
  LambdaError
} from '../../src/aws-lambda-framework'

class TestLambda extends BaseLambda {
  constructor(event: APIGatewayProxyEvent, context: Context) {
    super(event, context)
  }

  async invoke(): Promise<LambdaResult> {
    try {
      const res = await LambdaContainer.get(Mysql).execute<Country>({
        sql: process.env.MYSQL_TEST_SQL!
      })

      return {
        userMessage: 'Successfully tested Lambda!',
        data: res.rows
      }
    } catch (err) {
      throw new LambdaError(err.message, err.stack, 'Failed to Test Lambda!')
    }
  }
}

export function handler(event: APIGatewayProxyEvent, context: Context): Promise<APIGatewayProxyResult> {
  return new TestLambda(event, context).handler()
}

// interface Country.ts
interface Country {
  id: number
  name: string
  locale: string
  countryCode: number
}

Kitchensink Example

// KitchensinkLambda.ts

import {
  BaseLambda,
  APIGatewayProxyEvent,
  Context,
  LambdaResult,
  LambdaContainer,
  Property
} from '../../src/aws-lambda-framework'
import { inject } from 'inversify'
import { KitchensinkRepository } from './KitchensinkRepository'
import { UpdateKitchensinkRequest } from './UpdateKitchensinkRequest'
import { validatePermissions } from './validatePermissions'
import { validateRequest } from './validateRequest'

class KitchensinkLambda extends BaseLambda {
  @inject(KitchensinkRepository) private repo: KitchensinkRepository
  request: UpdateKitchensinkRequest

  constructor(event: APIGatewayProxyEvent, context: Context) {
    super(event, context)
    this.request = LambdaContainer.get<UpdateKitchensinkRequest>(Property.EVENT_BODY)
  }

  async invoke(): Promise<LambdaResult> {
    validatePermissions(['Superusers'])
    await validateRequest(this.request)

    const res = await this.repo.updateKitchenSink(this.request.updatedKitchensink)

    return {
      userMessage: 'Successfully updated Kitchensink!',
      data: res.metadata
    }
  }
}

export function handler(event: APIGatewayProxyEvent, context: Context) {
  return new KitchensinkLambda(event, context)
}

// Kitchensink.ts

import { IsInt, Min, Max } from 'class-validator'

export class Kitchensink {
  id: number
  @IsInt()
  @Min(40)
  @Max(200)
  height: number
  @IsInt()
  @Min(80)
  @Max(400)
  width: number
}

// KitchensinkRepository.ts

import { injectable, inject } from 'inversify'
import { Mysql, Query } from '../../src/aws-lambda-framework'
import { QueryResult } from '../../src/framework/interfaces/QueryResult'
import { Kitchensink } from './Kitchensink'

@injectable()
export class KitchensinkRepository {
  @inject(Mysql) private mysql: Mysql

  async updateKitchenSink(kitchensink: Kitchensink): Promise<QueryResult<void>> {
    const query: Query = {
      sql: `
        UPDATE
          some_table (id, height, width)
        VALUES (
          ?,?,?
        )`,
      inputs: [kitchensink.id, kitchensink.height, kitchensink.width]
    }

    return this.mysql.execute(query)
  }
}

// UpdateKitchensinkRequest.ts

import { Kitchensink } from './Kitchensink'

export class UpdateKitchensinkRequest {
  updatedKitchensink: Kitchensink
}

// validatePermissions.ts

import { Property, LambdaContainer, CognitoToken, UnauthorizedError } from '../../src/aws-lambda-framework'

export function validatePermissions(whitelistedCognitoGroups: string[]): void {
  const cognitoGroups = LambdaContainer.get<CognitoToken>(Property.COGNITO_TOKEN)['cognito:groups']
  if (cognitoGroups.some(g => whitelistedCognitoGroups.includes(g))) throw new UnauthorizedError()
}

// validateRequest.ts

import { Validator, LambdaContainer, ValidationError } from '../../src/aws-lambda-framework'

export function validateRequest(request: object): Promise<void> {
  return LambdaContainer.get(Validator)
    .validateOrReject(request)
    .catch(errors => {
      throw new ValidationError(errors)
    })
}

Services

The framework provides easy access to some of the tools that are often needed when writing Lambda functions. These are injected as singletons into the Container the first time they are called.

Validator

Mysql

Postgres

SlackNotifier

AWS Services

  • APIGateway
  • Aurora
  • Cloudwatch
  • DynamoDB
  • DynamoDC (Document Client)
  • Kinesis
  • Lambda
  • RDS
  • Redshift
  • S3
  • SES
  • SSM