Reputation: 343
I installed ujson package in virtualenv
(mylambdaenv) C:\Users\xyz\lambda_code\mylambdaenv>pip install ujson
Collecting ujson
Using cached ujson-4.0.2-cp36-cp36m-win_amd64.whl (43 kB)
Installing collected packages: ujson
Successfully installed ujson-4.0.2
But While running the package it is failing with error no module ujson. I have one file in my package as ujson.cp36-win_amd64 & one folder ujson-4.0.2.dist-info. Is it not readable by AWS Lambda? Please help to resolve Error
import ujson
ModuleNotFoundError: No module named 'ujson'
Upvotes: 2
Views: 1062
Reputation: 238647
The best way, in my experience, to add dependencies to lambda functions is through lambda layers and the use of docker tool described in the recent AWS blog:
Thus you can add ujson
to your function as follows:
Create empty folder, e.g. mylayer
.
Go to the folder and create requirements.txt
file with the content of
echo ujson > ./requirements.txt
The command will create layer for python3.8:
docker run -v "$PWD":/var/task "lambci/lambda:build-python3.8" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.8/site-packages/; exit"
zip -9 -r mylayer.zip python
Create lambda layer based on mylayer.zip
in the AWS Console. Don't forget to specify Compatible runtime
to python3.8
.
Add the the layer created in step 6 to your function.
Test the layer in lambda using the following lambda function:
import ujson
def lambda_handler(event, context):
print(dir(ujson))
I tested this and it the function and layer work correctly:
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', 'decode', 'dump', 'dumps', 'encode', 'load', 'loads']
p.s.
The above steps were executed on linux. If you don't have one, you can create EC2 linux instance and do the steps there if you wish.
Upvotes: 1