Why am I unable to install any dependencies to Lambda Via Layers?

0

I have a simple package I would like to use in my python lambda function: bcrypt. I have tried many ways of installing this dependency, mainly following YT tutorials, but the way that makes the most sense to me is by utilizing layers. I have gone through the workflow 3 times: make a layer, make a zip folder with python/lib/python3.13 etc and installing the packages to the site folder, zipping the python folder and uploading it to the layer. Still my lambda function does not find the correct folder. What am I doing wrong? Do I just not use any external libraries? Really stuck here, any contact or help would be huge.

2 Answers
1
Accepted Answer

Hello.

You can add a layer to the Lambda function by executing the following command in CloudShell.

mkdir python
pip3 install bcrypt -t ./python
zip -r ./python.zip .
aws lambda publish-layer-version --layer-name python-layer --zip-file fileb://python.zip --compatible-runtimes python3.13
aws lambda update-function-configuration --function-name "Your Lambda Function Name" --layers arn:aws:lambda:<Your Region Code>:<Your AWS Account ID>:layer:python-layer:1

CloudShell can be used by following the steps in the following document:
https://docs.aws.amazon.com/cloudshell/latest/userguide/getting-started.html#launch-region-shell

profile picture
EXPERT
answered 2 months ago
profile picture
EXPERT
reviewed 2 months ago
  • I'm not sure who you are or where you came from, but I am incredibly grateful for the response. Thank you.

0

It sounds like you're encountering some common issues with Lambda layers. Let's go through a few potential reasons why your Lambda function might not be finding the correct folder for your dependencies:

  1. Incorrect folder structure: When creating a layer for Python dependencies, the correct structure is crucial. The packages should be in a folder named 'python' at the root of your zip file, and within that, they should be in the appropriate site-packages directory. For example: python/lib/python3.x/site-packages/.

  2. Python version mismatch: Ensure that the Python version you're using to install the packages matches the runtime of your Lambda function. If you're using Python 3.13 in your layer structure, make sure your Lambda function is also set to use Python 3.13.

  3. Layer size limits: Lambda has a maximum size limit for all layers combined when extracted. If your layer is too large, it might not be applied correctly.

  4. Layer attachment: Make sure you've actually attached the layer to your Lambda function after creating it.

  5. Permissions: Ensure your Lambda function has the necessary permissions to access the layer.

To troubleshoot:

  1. Double-check your folder structure in the zip file.
  2. Verify that the Python version in your layer matches your Lambda runtime.
  3. Check the size of your layer - if it's very large, consider optimizing it.
  4. Confirm that the layer is attached to your function.
  5. Review the function's execution role to ensure it has permissions to use the layer.

If you're still having issues, you might consider alternative approaches:

  1. Include the dependencies directly in your Lambda deployment package instead of using a layer.
  2. For larger dependencies, consider using Lambda container images, which have a much higher size limit (up to 10GB).

Remember, while layers can be useful for managing dependencies, they're not always necessary. For simpler use cases, including dependencies directly in your deployment package can be more straightforward.
Sources
Unable to import module 'lambda_function': No module named 'requests | AWS re:Post
Lambda Package Exceeds 60MB: Solutions for Large Dependencies? | AWS re:Post
Packaging your layer content - AWS Lambda
Lambda function package not found error | AWS re:Post

profile picture
answered 2 months ago
profile picture
EXPERT
reviewed 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions