Using Python 3.12 and AWS Lambda, I’m trying to run Redis, but it gives an error for an unrecognized library.
It’s hard to believe that Redis is not a native Lambda library. Do I need to upload the Redis library manually, or is it under another name?
import json
import redis
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Error message:
Response
{
"errorMessage": "Unable to import module 'lambda_function': No module named 'redis'",
"errorType": "Runtime.ImportModuleError",
"requestId": "xxxx-xxx-xxxx-xxxx-xxx",
"stackTrace": []
}
2
Answers
That’s correct. AWS Lambda Runtime goes with a minimal set of pre-installed libraries.
However, you can include any libraries you’re using.
Please take into account that depending on your set up these steps may differ. For example if you’re using
serverless
to manage Lambda functions you may use built-in support for packaging python projects with preferred dependency manager, e.g pipenv, poetry.I hope it helps, please let me know if you have further questions.
Links:
Correct, you can create redis layer and attach with your lambda function to resolve the issue.
Next, return to Lambda and navigate to Layers. Create a new layer and name it ‘redis-layer.’ Upload the ZIP file.
Once you have created the layer, add it to your Lambda function.
Or else you can directly install dependencies into your project folder and you can upload a zip
open your project directory and create requirements.txt file and add all dependencies that you required for your function.
run:
Upload a zip to your lambda function.