aws-samples / aws-lambda-extensions

A collection of sample extensions to help you get started with AWS Lambda Extensions
MIT No Attribution
442 stars 145 forks source link

Asynchronous Initialization Extension Layer for NodeJs #53

Closed chuckbarkertech closed 3 years ago

chuckbarkertech commented 3 years ago

Asynchronous Initialization Extension Layer for NodeJs

The Problem

Asynchronous operations do not complete during the initialization phase. This is specifically a problem for provisioned concurrency. One of the major benefits is that long running operations can be initialized during provisioned concurrency, but this does not happen with AWS Lambda NodeJs operations.

A JavaScript IIFE can be used to start a promise during the initialization phase, but it does not complete until the function is actually called. Sometimes it does not complete until multiple requests later since it is running as a background task in the event loop.

The Solution

An AWS Extension layer can be used to keep the AWS Lambda container active until the AWS Lambda NodeJs function initialization completes all of its asynchronous operations. I have linked an example repository where I demonstrate this.

I would like to turn this over to the aws-lambda-extensions repository. Is it Ok to create a pull request for this? Are there any questions or modifications needed?

The example is here: https://github.com/chuckbarkertech/nodejs-example-async-init-extension