MeghP
MeghP

Reputation: 711

Install a package in AWS lambda

I need jinja2 library for my code. AWS lambda function is unable to import the module jinja2

I tried installing jinja2 locally in a directory with virtualenv, zipped it and uploaded to lambda.

I am unable to edit the code inline as the size is more than 15MB and also the test results throws the same error.

Upvotes: 1

Views: 2852

Answers (1)

Chuck LaPress
Chuck LaPress

Reputation: 278

Your best bet is to package Jinja2 as a lambda layer and attach it to your code procedure to accomplish this is outlined here: https://nordcloud.com/lambda-layers-for-python-runtime/ but for example seperate of your python lambda you'll create a directory jinja2_layer

pipenv --python 3.6
pipenv shell
pipenv install jinja2
PY_DIR='build/python/lib/python3.6/site-packages' 
mkdir -p $PY_DIR     #Create temporary build directory
pipenv lock -r > requirements.txt    #Generate requirements file
pip install -r requirements.txt --no-deps -t $PY_DIR    #Install packages 
cd build
zip -r ../jinja2_layer.zip .    #Zip files
cd ..
rm -r build    #Remove temporary directory

What you'll end up with is a jinja2_layer directory with the following files: Pipfile Pipfile.lock jinja2_layer.zip requirements.txt just upload the jinja2_layer.zip as a lambda layer and attach it any time you need it.

Upvotes: 2

Related Questions