2.0.18 • Published 2 years ago

@jetkit/cdk v2.0.18

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

JetKit/CDK

Tests npm version Open in Visual Studio Code

An anti-framework for building cloud-native serverless applications.

This module provides convenient tools for writing Lambda functions, RESTful API views, and generating cloud infrastructure with AWS CDK.

Motivation

Frameworkless web applications.

We want to build maintainable and scalable cloud-first applications, with cloud resources generated from application code.

Using AWS CDK we can automate generating API Gateway routes and Lambda functions from class and function metadata.

Each class or function view is a self-contained Lambda function that only pulls in the dependencies needed for its functioning, keeping startup times low and applications modular.

Documentation

Guides and API reference can be found at https://jetkit.dev/docs/.

Super Quickstart

Use this monorepo project template: typescript-cdk-template.

Installation

npm install @jetkit/cdk

Synopsis

API View

import { HttpMethod } from "@aws-cdk/aws-apigatewayv2"
import { badRequest, methodNotAllowed } from "@jdpnielsen/http-error"
import { ApiView, SubRoute, ApiEvent, ApiResponse, ApiViewBase, apiViewHandler } from "@jetkit/cdk"

@ApiView({
  path: "/album",
  memorySize: 512,
  environment: {
    LOG_LEVEL: "DEBUG",
  },
  bundling: { minify: true, metafile: true, sourceMap: true },
})
export class AlbumApi extends ApiViewBase {
  // define POST handler
  post = async () => "Created new album"

  // custom endpoint in the view
  // routes to the ApiView function
  @SubRoute({
    path: "/{albumId}/like", // will be /album/123/like
    methods: [HttpMethod.POST, HttpMethod.DELETE],
  })
  async like(event: ApiEvent): ApiResponse {
    const albumId = event.pathParameters?.albumId
    if (!albumId) throw badRequest("albumId is required in path")

    const method = event.requestContext.http.method

    // POST - mark album as liked
    if (method == HttpMethod.POST) return `Liked album ${albumId}`
    // DELETE - unmark album as liked
    else if (method == HttpMethod.DELETE) return `Unliked album ${albumId}`
    // should never be reached
    else return methodNotAllowed()
  }
}
export const handler = apiViewHandler(__filename, AlbumApi)

Handler Function With Route

import { HttpMethod } from "@aws-cdk/aws-apigatewayv2"
import { Lambda, ApiEvent } from "@jetkit/cdk"

// a simple standalone function with a route attached
export async function topSongsHandler(event: ApiEvent) {
  return "top songs"
}
// define route and lambda properties
Lambda({
  path: "/top-songs",
  methods: [HttpMethod.GET],
  memorySize: 384,
  environment: {
    LOG_LEVEL: "WARN",
  },
})(topSongsHandler)

// alternate, uglier way of writing the same thing
const topSongsFuncInner = Lambda({
  path: "/top-songs-inner",
  methods: [HttpMethod.GET],
  memorySize: 384,
  environment: {
    LOG_LEVEL: "WARN",
  },
  // this function name should match the exported name
  // or you must specify the exported function name in `handler`
})(async function topSongsFuncInner(event: ApiEvent) {
  return "top songs"
})
export { topSongsFuncInner }

CDK Stack

To start from scratch:

npm install -g aws-cdk
cdk init app --language typescript
npm install @jetkit/cdk @aws-cdk/core @aws-cdk/aws-apigatewayv2

See the guide for more details.

To deploy your stack:

cdk deploy

To generate API Gateway routes and Lambda function handlers from your application code:

import { CorsHttpMethod, HttpApi } from "@aws-cdk/aws-apigatewayv2"
import { Construct, Duration, Stack, StackProps, App } from "@aws-cdk/core"
import { ResourceGeneratorConstruct, AlbumApi, topSongsHandler } from "@jetkit/cdk"

export class InfraStack extends Stack {
  constructor(scope: App, id: string, props?: StackProps) {
    super(scope, id, props)

    // create API Gateway
    const httpApi = new HttpApi(this, "Api", {
      corsPreflight: {
        allowHeaders: ["Authorization"],
        allowMethods: [CorsHttpMethod.ANY],
        allowOrigins: ["*"],
        maxAge: Duration.days(10),
      },
    })

    // transmute your app code into infrastructure
    new ResourceGeneratorConstruct(this, "Generator", {
      resources: [AlbumApi, topSongsHandler], // supply your functions and view classes here
      httpApi,
    })
  }
}

How It Works

This library provides decorators that can be attached to view classes, methods, and functions. The decorator attaches metadata in the form of options for constructing the Lambda function and optionally API Gateway routes.

It also includes some convenient CDK L3 constructs to generate the Lambda functions and API Gateway routes from your decorated application code.

Local Development

AWS SAM supports running CDK applications locally (in beta).

2.0.17

2 years ago

2.0.18

2 years ago

2.0.15

3 years ago

2.0.16

3 years ago

2.0.14

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.9

3 years ago

2.0.8

3 years ago

2.0.11

3 years ago

2.0.10

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

1.122.20

3 years ago

1.122.21

3 years ago

1.122.19

3 years ago

1.122.18

3 years ago

1.122.15

3 years ago

1.122.16

3 years ago

1.122.14

3 years ago

1.122.17

3 years ago

1.122.12

3 years ago

1.122.13

3 years ago

1.122.11

3 years ago

1.122.10

3 years ago

1.122.1

3 years ago

1.122.6

3 years ago

1.122.9

3 years ago

1.122.8

3 years ago

1.122.2

3 years ago

1.122.5

3 years ago

1.122.4

3 years ago

1.119.17

3 years ago

1.119.16

3 years ago

1.119.15

3 years ago

1.119.9

3 years ago

1.119.14

3 years ago

1.119.13

3 years ago

1.119.10

3 years ago

1.119.8

3 years ago

1.119.6

3 years ago

1.119.7

3 years ago

1.119.4

3 years ago

1.119.5

3 years ago

1.119.2

3 years ago

1.119.3

3 years ago

1.119.1

3 years ago

1.115.33

3 years ago

1.115.31

3 years ago

1.115.30

3 years ago

1.115.29

3 years ago

1.115.28

3 years ago

1.115.27

3 years ago

1.115.26

3 years ago

1.115.25

3 years ago

1.115.24

3 years ago

1.115.23

3 years ago

1.115.22

3 years ago

1.115.21

3 years ago

1.115.19

3 years ago

1.115.18

3 years ago

1.115.17

3 years ago

1.115.16

3 years ago

1.115.15

3 years ago

1.115.14

3 years ago

1.115.13

3 years ago

1.115.12

3 years ago

1.115.11

3 years ago

1.115.8

3 years ago

1.115.6

3 years ago

1.115.7

3 years ago

1.115.5

3 years ago

1.115.4

3 years ago

1.115.2

3 years ago

1.115.3

3 years ago

1.115.1

3 years ago

1.110.10

3 years ago

1.110.13

3 years ago

1.110.12

3 years ago

1.110.9

3 years ago

1.110.7

3 years ago

1.110.8

3 years ago

1.110.6

3 years ago

1.110.4

3 years ago

0.4.10

3 years ago

1.110.1

3 years ago

1.110.2

3 years ago

1.110.3

3 years ago

0.4.9

3 years ago

0.4.8

3 years ago

0.4.5

3 years ago

0.4.6

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.0.43

3 years ago

0.0.44

3 years ago

0.3.0

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.0.42

3 years ago

0.0.41

3 years ago

0.0.40

3 years ago

0.0.39

3 years ago

0.0.38

3 years ago

0.0.37

3 years ago

0.0.36

3 years ago

0.0.35

3 years ago

0.0.34

3 years ago

0.0.33

3 years ago

0.0.32

3 years ago

0.0.31

3 years ago

0.0.30

3 years ago

0.0.29

3 years ago

0.0.28

3 years ago

0.0.27

3 years ago

0.0.26

3 years ago

0.0.25

3 years ago

0.0.24

3 years ago

0.0.23

3 years ago

0.0.22

3 years ago

0.0.21

3 years ago

0.0.20

3 years ago

0.0.19

3 years ago

0.0.18

3 years ago

0.0.17

3 years ago

0.0.16

3 years ago

0.0.14

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.1

3 years ago