Working with Layers
Overview
The Layer resource allows functions to access shared libraries or custom runtimes. Adding a layer to a function is as easy as adding a Layer resource and dragging a wire to a Function resource, as you can see in this example stack:
The stack above has a layer whose source code can be found in the lambda-layers
directory, and two functions that reference that layer, both with source code in their own directories, written in Python 3.7. By referencing the layer, both functions are able to use the functions and modules in the MyLambdaLayer
layer.
Referencing a layer in the template
If you look at the Template view of the above example, you'll see that both the DeserializeDemoFunction
and ObjectPathDataDemoFunction
functions have the following property:
Layers:
- !Ref MyLambdaLayer
And below is the yaml for the layer itself:
MyLambdaLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: MyLambdaLayer
Description: !Sub
- Stack ${StackTagName} Environment ${EnvironmentTagName} Layer ${ResourceName}
- ResourceName: MyLambdaLayer
ContentUri: lambda-layers/my-lambda-layer.zip
CompatibleRuntimes:
- python3.6
- python3.7
LicenseInfo: MIT
RetentionPolicy: Retain
Of course with Stackery, there's no need to write the YAML yourself, as it's created for you when you add your resources in the visual editor.
Adding a layer
The easiest way add a layer is in the Stackery Dashboard.
- In Visual Edit Mode click Add Resource and select Layer
- Double-click on the Layer resource to configure its properties, such as the Logical ID, source path and build method
- Click Save to save the layer settings
- Drag an event subscription wire from the right side of the layer to the left side of the function(s) you want to use the layer code
Using a build method
When you add a layer resource, one of the options you can set in the Dashboard is its Build Method. This is a list of runtime systems that could be used to build the layer and when specified, adds the following metadata to the layer:
Metadata:
BuildMethod: python3.7 # selected runtime
This metadata tells the AWS SAM CLI to run its builder for this directory, which is the same code that AWS Lambda builders run when you deploy a function. For example, if you select NodeJS as a build method, npm install
and npm build
will be run, and if your directory is missing a package.json
file, you will see an error.
Select none
as the build method if your layer already has output code ready to be used by the function.
Using a layer as a runtime
While layers are often used for shared code libraries, they can also be used to provide a runtime that's not supported by Lambda, such as our PHP lambda layer. In that case, the function runtime is selected asprovided
and the layer is referenced directly in the Function resource rather than as a separate resource, as in this example:
Double-click on the Function resource above to see its settings, or click Deploy with Stackery to see it in your own Stackery account.
Working with layers
Functions with layers can be deployed like any other stack, either locally or with CodeBuild.
Locally invoking functions with layers
When locally invoking a function that uses layers, the --build
flag is necessary, or the call will result in an error.
stackery local invoke -f <function-id> -e <your-env-name> --build
Deploying stacks with layers
Stacks that include a layer resource or reference a layer ARN can be deployed with either the default AWS CodeBuild or local checkout strategy. Depending on your runtime, you may need to use the --use-container
flag when deploying locally.
Nested stacks with layers
Users who are using layers with their nested applications need to make sure to use !Ref
to reference their layer or layer ARN rather than !Sub
. This is a known AWS SAM issue and may be addressed in the future.