Can an AWS Lambda Layer intercept a Lambda Function Handler, without the Function / Handler invoking the layer?

Viewed 647

I'm researching the abilities of AWS Lambda Layers and trying to confirm whether the Layer can add behaviors without the Lambda Function having any knowledge / interaction with the layer.

My understanding from the docs is that Layers are effectively a .zip file that is unpacked to the Lambda instance and is primarily for delivering resources without requiring those resources to be packaged directly with the Lambda Function / handler. For example, using Layers to deploy runtimes (executables) or shared resources (such as binary dependencies) is well documented--in both cases the Function itself invokes or references the Layer content.

My question is whether a Layer, entirely on its own, can add behaviors to/around the Lambda Function. For example, let's say I want a new layer that simply echos "Hello World" -- is it possible to deploy a layer that does that to an existing Function, without the Function having any reference to the layer?

2 Answers

Check out Lambda API Extensions. These give a way to do something like what you describe, and they are deployed as layers, but it is the extension aspect that allows to interact differently than dependency code in a typical layer. Also see https://aws.amazon.com/blogs/compute/introducing-aws-lambda-extensions-in-preview/

enter image description here

Layers that aren't extensions don't work that way intrinsically. It is a common coding concept, especially in a web setting, to have a pipeline of modules that interact on an incoming request and that can modify the request or even terminate it before it reaches the core code, but that is not how layers typically function.

Instead, layers are for runtime dependencies in the core code. They only come into play because something in the core code imports/references them. Without such a reference, the code just sits there and is unused.

The name "layers" can give an incorrect impression that these are modules of code that wrap your core code or that the flow passes through these layers before reaching your code lambda function code, but it is actually the extension aspect that would give them the capability you ask about.

The AWS Lambda execution environment gives a useful visual: enter image description here

Also see

After further research I found that Lambda Layers can indeed modify the behavior of Lambda Functions without the Function having any interaction with the layer's resources.

The Layer is able to intercept the runtime invocation, and AWS-provided runtimes also provide some environment variables that can be set to provide some "hooks". These abilities are described in AWS's document titled Modifying the runtime environment.

While this is possible, the Lambda Extensions described in Shawn's answer provides much greater abilities. As of right now Extensions are in preview, but will be worth considering an exploring.

Related