Referencing AWS Lambda Layers in Serverless Framework using Cloudformation Reference

Rommel Baltazar
2 min readJul 30, 2021

Layers in Serverless help to reuse code in different functions. I have read some documentations on how a layer is used from the following links but I struggled to understand why there is a need for different configurations.

Serverless Framework — AWS Lambda Guide — Layers

How to publish and use AWS Lambda Layers with the Serverless Framework

This article aims to explain the differences between such configurations.

The simplest way to reuse a layer is to reference the layer by it’s ‘arn’ like below:

functions:
hello:
handler: handler.hello
layers:
-arn:aws:lambda:region:XXXXXX:layer:LayerName:Y

The problem with this is that once the layer gets updated, the Y variable gets incremented and thus, you would need to update all functions that use this layer ‘arn’. Else, the lambda function would remain broken.

Another method of referencing a layer is to do the following:

layers:
test:
path: layer
functions:
hello:
handler: handler.hello
layers:
-{ Ref: TestLambdaLayer }

The format of the reference name is (layer name in Title Case [first letter in Capital] + ‘LambdaLayer’). This eliminates the arn-referencing issue but this referencing only works when referencing a layer which is published in the same function. Also, this method is utilizing Cloudformation referencing as described here: Ref — AWS CloudFormation (amazon.com).

So the remaining use case is how to reference layers when it is used in another function (not on the same function where the layer was published). This article briefly describes how to do it: How to publish and use AWS Lambda Layers with the Serverless Framework

functions:
hello:
handler: handler.hello
layers:
- ${cf:layer-awscli-dev.LayerawscliExport}

The question now is ‘What is this new naming convention?’. What I have learned is that when publishing layers (or resources in general), you are able to share the resource by utilizing Output Variables (Managing Secrets and Output Variables With Serverless Framework Enterprise). Thus, the Cloudformation Reference above is an output of the layer resource as below:

service: layer-awscli
custom:
defaultStage: dev
dev:
profile: AWSAccountX
layers:
awscli:
package:
artifact: layers/awscli.zip
resources:
Outputs:
LayerawscliExport:
Value:
Ref: AwscliLambdaLayer
Export:
Name: AwscliLambdaLayer

When this layer was deployed, the Cloudformation stack name is ‘layer-aws-cli-dev’ and has an output variable named ‘LayerawscliExport’ which is referencing to the lambda layer itself (see previous referencing method usin Ref). Thus, to reference this layer in another function, the naming convention is: ‘cf : layer name’ + ‘layer output variable referencing the layer’.

In summary, we have learned to reference a layer in 3 ways which are:

  1. Via ARN — disadvantageous if layer gets updated
  2. Via Cloudformation reference Ref — can only be used within the same function
  3. Via Cloudformation reference from layer output variable — can be used in different functions

--

--