16.1.0 • Published 2 months ago

@whook/aws-lambda v16.1.0

Weekly downloads
59
License
MIT
Repository
github
Last release
2 months ago

@whook/aws-lambda

Build and deploy to AWS Lambda with Whook.

GitHub license

This module is aimed to help you to build and deploy your Whook server to AWS Lambda.

You can find a complete setup with a Terraform deployment example in this pull request.

Quick setup

Install this module:

npm i @whook/aws-lambda;

Add the plugin to the src/index.ts main file:

  // ...

  $.register(
    constant('WHOOK_PLUGINS', [
      ...WHOOK_DEFAULT_PLUGINS,
+      '@whook/aws-lambda',
      '@whook/cors',
    ]),
  );

  // ...

Tweak the 2 build functions in your src/build.ts main file:

import {
  // (...)
-  DEFAULT_INITIALIZER_PATH_MAP,
-  runBuild as runBaseBuild,
-  prepareBuildEnvironment as prepareBaseBuildEnvironment,
  // (...)
} from '@whook/whook';
+import {
+  DEFAULT_BUILD_INITIALIZER_PATH_MAP,
+  runBuild as runBaseBuild,
+  prepareBuildEnvironment as prepareBaseBuildEnvironment,
+} from '@whook/aws-lambda';

// (...)

// The `prepareBuildEnvironment` create the build
//  environment
export async function prepareBuildEnvironment(
  $: Knifecycle = new Knifecycle(),
): Promise<Knifecycle> {
  $ = await prepareEnvironment($);

  // (...)

-  // Usually, here you call the installed build env
-  // $ = await prepareBaseBuildEnvironment($);
+  // Calling the AWS specific build env
+  $ = await prepareBaseBuildEnvironment($);

  // The build often need to know were initializers
  //  can be found to create a static build and
  //  remove the need to create an injector
  $.register(
    constant('INITIALIZER_PATH_MAP', {
      // (...)
-      ...DEFAULT_INITIALIZER_PATH_MAP,
+      ...DEFAULT_BUILD_INITIALIZER_PATH_MAP,
    }),
  );

  // (...)

}

Declare this module types in your src/whook.d.ts type definitions:

+ import type { WhookCompilerConfig } from '@whook/whook';
+ import type {
+   WhookAWSLambdaBuildConfig,
+   WhookAPIOperationAWSLambdaConfig
+ } from '@whook/aws-lambda';

declare module 'application-services' {

  // ...

  export interface AppConfig
-    extends WhookBaseConfigs {}
+    extends WhookBaseConfigs,
+      WhookAWSLambdaBuildConfig,
+      WhookCompilerConfig {}
}

// ...

declare module '@whook/whook' {
  export interface WhookAPIHandlerDefinition<
    T extends Record<string, unknown> = Record<string, unknown>,
    U extends {
      [K in keyof U]: K extends `x-${string}` ? Record<string, unknown> : never;
    } = unknown,
    V extends Record<string, unknown> = Record<string, unknown>,
  > extends WhookBaseAPIHandlerDefinition<T, U> {
    operation: U & WhookAPIOperation<
        T &
+       WhookAPIOperationAWSLambdaConfig<V> &
      WhookAPIOperationCORSConfig
    >;
  }

}

And add the AWS Lambda config (usually in src/config/common/config.js):

// ...
import type { AppConfig } from 'application-services';

// ...

const CONFIG: AppConfig = {
  // ...
+  COMPILER_OPTIONS: {
+    externalModules: [],
+    target: '20',
+  },
};

export default CONFIG;

Build

To build your functions:

# Build all functions
npm run build

# Build only one function
npm run build -- getPing

Debug

You can easily test your functions builds by adding @whook/aws-lambda to your WHOOK_PLUGINS list. It provides you some commands like the testHTTPLambda one:

npx whook testHTTPLambda --name getPing

To get more insights when some errors happens:

DEBUG=whook npm run whook-dev -- testHTTPLambda --name getPing

Deployment

We recommend using Terraform to deploy your lambda functions.

There is a complete example on how to deploy your functions in this pull request.

API

Functions

initHandler(services) ⇒ Promise.<function()>

Initialize one Whook handler

Kind: global function
Returns: Promise.<function()> - A promise of the HANDLERS hash.

ParamTypeDefaultDescription
servicesObjectThe services $autoload depends on
services.WRAPPERSArrayAn optional list of wrappers to inject
services.logObjectnoopAn optional logging service
services.HANDLERSObjectThe rest is a hash of handlers mapped by their operation id

initWrapHandlerForConsumerLambda(services) ⇒ Promise.<Object>

Wrap an handler to make it work with a consumer AWS Lambda.

Kind: global function
Returns: Promise.<Object> - A promise of an object containing the reshaped env vars.

ParamTypeDefaultDescription
servicesObjectThe services the wrapper depends on
services.ENVObjectThe process environment
services.OPERATION_APIObjectAn OpenAPI definitition for that handler
services.apmObjectAn application monitoring service
services.timeObjectAn optional time service
services.logObjectnoopAn optional logging service

initWrapHandlerForCronLambda(services) ⇒ Promise.<Object>

Wrap an handler to make it work with cron AWS Lambda.

Kind: global function
Returns: Promise.<Object> - A promise of an object containing the reshaped env vars.

ParamTypeDefaultDescription
servicesObjectThe services the wrapper depends on
services.ENVObjectThe process environment
services.OPERATION_APIObjectAn OpenAPI definitition for that handler
services.apmObjectAn application monitoring service
services.timeObjectAn optional time service
services.logObjectnoopAn optional logging service

initWrapHandlerForConsumerLambda(services) ⇒ Promise.<Object>

Wrap an handler to make it work with a consumer AWS Lambda.

Kind: global function
Returns: Promise.<Object> - A promise of an object containing the reshaped env vars.

ParamTypeDefaultDescription
servicesObjectThe services the wrapper depends on
services.OPERATION_APIObjectAn OpenAPI definitition for that handler
services.ENVObjectThe process environment
services.DEBUG_NODE_ENVSObjectThe NODE_ENV values that trigger debugging
services.DECODERSObjectRequest body decoders available
services.ENCODERSObjectResponse body encoders available
services.PARSED_HEADERSObjectA list of headers that should be parsed as JSON
services.PARSERSObjectRequest body parsers available
services.STRINGIFYERSObjectResponse body stringifyers available
services.BUFFER_LIMITObjectThe buffer size limit
services.apmObjectAn application monitoring service
services.obfuscatorObjectA service to hide sensible values
services.errorHandlerObjectA service that changes any error to Whook response
services.timeObjectAn optional time service
services.logObjectnoopAn optional logging service

initWrapHandlerForKafkaLambda(services) ⇒ Promise.<Object>

Wrap an handler to make it work with a kafka AWS Lambda.

Kind: global function
Returns: Promise.<Object> - A promise of an object containing the reshaped env vars.

ParamTypeDefaultDescription
servicesObjectThe services the wrapper depends on
services.ENVObjectThe process environment
services.OPERATION_APIObjectAn OpenAPI definitition for that handler
services.apmObjectAn application monitoring service
services.timeObjectAn optional time service
services.logObjectnoopAn optional logging service

initWrapHandlerForLogSubscriberLambda(services) ⇒ Promise.<Object>

Wrap an handler to make it work with a log subscriber AWS Lambda.

Kind: global function
Returns: Promise.<Object> - A promise of an object containing the reshaped env vars.

ParamTypeDefaultDescription
servicesObjectThe services the wrapper depends on
services.ENVObjectThe process environment
services.OPERATION_APIObjectAn OpenAPI definitition for that handler
services.apmObjectAn application monitoring service
services.timeObjectAn optional time service
services.logObjectnoopAn optional logging service

initWrapHandlerForS3Lambda(services) ⇒ Promise.<Object>

Wrap an handler to make it work with a S3 AWS Lambda.

Kind: global function
Returns: Promise.<Object> - A promise of an object containing the reshaped env vars.

ParamTypeDefaultDescription
servicesObjectThe services the wrapper depends on
services.ENVObjectThe process environment
services.OPERATION_APIObjectAn OpenAPI definitition for that handler
services.apmObjectAn application monitoring service
services.timeObjectAn optional time service
services.logObjectnoopAn optional logging service

initWrapHandlerForConsumerLambda(services) ⇒ Promise.<Object>

Wrap an handler to make it work with a transformer AWS Lambda.

Kind: global function
Returns: Promise.<Object> - A promise of an object containing the reshaped env vars.

ParamTypeDefaultDescription
servicesObjectThe services the wrapper depends on
services.ENVObjectThe process environment
services.OPERATION_APIObjectAn OpenAPI definitition for that handler
services.apmObjectAn application monitoring service
services.timeObjectAn optional time service
services.logObjectnoopAn optional logging service

Authors

License

MIT

16.1.0

2 months ago

16.0.1

2 months ago

16.0.0

2 months ago

15.0.0

3 months ago

13.1.3

9 months ago

13.1.2

9 months ago

13.1.0

9 months ago

13.2.2

7 months ago

13.2.0

7 months ago

14.0.0

6 months ago

13.2.1

7 months ago

13.0.0

9 months ago

12.1.0

1 year ago

10.0.5

2 years ago

10.0.6

2 years ago

10.0.0

2 years ago

10.0.1

2 years ago

10.0.2

2 years ago

10.0.3

2 years ago

10.0.4

2 years ago

12.0.0

1 year ago

12.0.1

1 year ago

12.0.2

1 year ago

11.0.0

2 years ago

11.0.1

1 year ago

9.0.1

2 years ago

9.0.0

2 years ago

8.5.1

3 years ago

8.5.0

3 years ago

8.4.2

3 years ago

8.4.1

3 years ago

8.4.0

3 years ago

8.3.0

3 years ago

8.1.0

3 years ago

8.1.1

3 years ago

8.0.4

3 years ago

8.0.3

3 years ago

8.0.2

3 years ago

8.0.1

3 years ago

8.0.0

3 years ago

7.1.5

3 years ago

7.1.4

3 years ago

7.1.3

3 years ago

7.1.2

3 years ago

7.1.1

3 years ago

7.1.0

3 years ago

7.0.0

3 years ago

6.0.0

3 years ago

5.1.8

4 years ago

5.1.7

4 years ago

5.1.5

4 years ago

5.1.6

4 years ago

5.1.4

4 years ago

5.1.3

4 years ago

5.1.2

4 years ago

5.1.1

4 years ago

5.1.0

4 years ago

5.0.0

4 years ago

4.1.2

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.5

4 years ago

4.0.5-alpha.0

4 years ago

4.0.4

4 years ago

4.0.3

4 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

4.0.0-alpha.44

4 years ago

4.0.0-alpha.43

4 years ago

4.0.0-alpha.42

4 years ago

4.0.0-alpha.41

4 years ago

4.0.0-alpha.40

4 years ago

4.0.0-alpha.39

4 years ago

4.0.0-alpha.38

4 years ago

4.0.0-alpha.37

4 years ago

4.0.0-alpha.36

4 years ago

4.0.0-alpha.35

4 years ago

4.0.0-alpha.34

4 years ago

4.0.0-alpha.33

4 years ago

4.0.0-alpha.32

4 years ago