Search code examples
node.jsaws-lambdaaws-lambda-layers

Can a Node.js AWS Lambda Layers Run Code?


I'm reading about Lambda layers -- so far my understanding of a layer is it's a ZIP archive of extra files that AWS will dump in your Lambda function's /opt folder such that the function will be able to include extra modules/libraries/etc that are distributed in the ZIP archive.

Is that all a layer is? Or is it possible to create a layer that will run code prior to the the Lambda function's execution?


Solution

  • Your conclusion about Lambda Layers is correct!

    Including one or more layers in a function, during initialization, the contents of each layer are extracted in order to the /opt directory in the function execution environment. Each runtime then looks for libraries in a different location under /opt, depending on the language. You can include up to five layers per function, which count towards the unzipped deployment package size limit of 250 MB. Layers are automatically set as private, but they can be shared with other AWS accounts, or shared publicly.

    Or is it possible to create a layer that will run code prior to the the Lambda function's execution?

    What you are looking for can be achieved via Lambda Extensions:

    https://docs.aws.amazon.com/lambda/latest/dg/using-extensions.html

    https://aws.amazon.com/blogs/compute/introducing-aws-lambda-extensions-in-preview/

    Extensions can run in either of two modes – internal and external.

    • Internal extensions: run as part of the runtime process, in-process with your code. They allow you to modify the startup of the runtime process using language-specific environment variables and wrapper scripts. Internal extensions enable use cases such as automatically instrumenting code.

    • External extensions: allow you to run separate processes from the runtime but still within the same execution environment as the Lambda function. External extensions can start before the runtime process, and can continue after the runtime shuts down. External extensions enable use cases such as fetching secrets before the invocation, or sending telemetry to a custom destination outside of the function invocation. These extensions run as companion processes to Lambda functions.

    You can use Lambda extensions to augment your Lambda functions. For example, use Lambda extensions to integrate functions with your preferred monitoring, observability, security, and governance tools.

    The lifecycle of a Lambda execution environment will work as:

    • Init: In this phase, Lambda creates or unfreezes an execution environment with the configured resources, downloads the code for the function and all layers, initializes any extensions, initializes the runtime, and then runs the function’s initialization code (the code outside the main handler). The Init phase happens either during the first invocation, or in advance of function invocations if you have enabled provisioned concurrency.

    The Init phase is split into three sub-phases:

    • Extension init
    • Runtime init
    • Function init

    These sub-phases ensure that all extensions and the runtime complete their setup tasks before the function code runs.

    • Invoke: In this phase, Lambda invokes the function handler. After the function runs to completion, Lambda prepares to handle another function invocation.

    • Shutdown: This phase is triggered if the Lambda function does not receive any invocations for a period of time. In the Shutdown phase, Lambda shuts down the runtime, alerts the extensions to let them stop cleanly, and then removes the environment. Lambda sends a Shutdown event to each extension, which tells the extension that the environment is about to be shut down.