0
votes

I'm fairly new to AWS and serverless, so far I've been working with lambda functions and layers.

We have two services:

lambda1.yml

service: lambda1
provider: ...
functions:
 hello: 
  handler: ...
  name: ...
  layers:
   - { Ref: GlobalLambdaLayer }

layers:
  global:
    path: global_layer

lambda2.yml

service: lambda2
provider: ...
functions:
 hello: 
  handler: ...
  name: ...
  layers:
   - { Ref: GlobalLambdaLayer }

layers:
  global:
    path: global_layer

When I deploy lambda1 using 'sls deploy --config lambda1.yml', the layer is uploaded and assigned to lambda1 function succesfully.

However, when I deploy lambda2 using 'sls deploy --config lambda2.yml', Serverless uploads and creates a new layer. It doesn't reuse the one uploaded from lambda1.

According to my research, this is correct because each lambda function is declared in a different service.

My question is. Is my research correct?

I know I can create a separate service to hold my layer and share with both lambdas, as described here:

https://forum.serverless.com/t/layers-upload-on-each-deploy/6634/3

Nonetheless, I'm curious, is there a different way to do it? I can't put both lambdas in the same service. They need to be independent and share the same layer.

Thank you!

1

1 Answers

0
votes

As I can see that you declared layers: in the both files which create two different layers, but the purpose of layers is to abstract, and resue shared code between functions. So in lambda2.yml no need to recreate a layer you just need to tell your function to use the layer which you already created in lambda1.yml so for lambda2.yml code it should be:

service: lambda2
provider: ...
functions:
hello: 
 handler: ...
 name: ...
 layers:
   - { Ref: GlobalLambdaLayer }