0 votes
1 view
in AWS by (16.5k points)

I am designing a serverless application with AWS Lambda. There's a piece of code on one of the functions that process the request in a certain way. I am going to make another function that's going to make the same processing with the request data, in the same way.

The problem is that, if I change the processing function in one of the Lambda functions, I'm going to have to copy the function and paste in the other Lambda function. Every time I make a change I will have to do this. This will be yet more cumbersome if I want to do the same processing function in more than two Lambda functions.

Is there a way to share pieces of code between Lambda functions, so I may respect DRY principles?

1 Answer

0 votes
by (39.2k points)

Layers can be used in Lambda to share libraries and code between your various functions.

Like how you create a ZIP file for the function, you have to create for the Layers. So if you have a file structure like this:

bundle.zip/

  python/

    common/

      __init__.py

      lib.py

Then in your Lambda function, you can reference those libraries like this:

from common.lib import ...

...