3

I have a module called custom_module.py which has functions and methods that I would like to use across a few different Lambdas. The code looks like this:

def test():
    return 'Custom module'

I tried to convert this into a Lambda Layer by zipping up this file and using the form to create a layer. Everything seemed to work nicely, and I imported the module into my Lambda for use, like so:

import json
from custom_module import test

print('Loading function')

def lambda_handler(event, context):
    return test()

Unfortunately, running this returns a "Unable to import module 'lambda_function': No module named 'custom_module'" error. What exactly am I doing wrong here? I have the correct runtimes and architecture specified as well.

Edit

Going by the comment, I tried this file structure:

layer
|
+--- python
     |
     +--- custom_module
          |
          +--- __init__.py (contains the test() method)

I zipped up the layer folder as layer.zip and uploaded it. Still get the same issue unfortunately.

2
  • Did you configure the Lambda function to use the layer? Did you correctly package the layer code in a subfolder named python? Commented Dec 11, 2021 at 19:08
  • @jarmod Tried that, details in the new edit. Commented Dec 11, 2021 at 20:27

1 Answer 1

8

As per the docs, for Python runtimes, the Lambda layer should only have 1 subfolder called python so that the Lambda can access the layer content without the need to specify the path (or it needs to be a site package within python/lib/python3.9/site-packages).

You have 2 subfolders - layer and then python.

Change your file structure to have your custom_module inside the python folder only.

layer.zip
|
+--- python
     |
     +--- custom_module
          |
          +--- __init__.py (contains the test() method)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.