Reference lambda layer in the same file when using retain

How can I refer to my layer in the same stack when retain: true is used?

Without retain: true, I can simply use {Ref: FooLambdaLayer} because SLS automatically creates a Resource called FooLambdaLayer. However, when using retain: true, the created resource is called FooLambdaLayer<version hash> which is different every time, so it can’t be referenced.

I know that SLS adds the FooLambdaLayerQualifiedArn as an exported output parameter, so when using nested stacks or putting all your layers in another stack, you can reference them this way. But while in the same stack, I can’t reference output parameters.
Do I have any option other than putting all my layers in a separate module and importing the layer ARN:s for the modules hosting the functions using the layers?

I ended up making a plugin for this, If anyone finds this useful, give me feedback!

1 Like