I'm trying to pip install a package in an AWS Lambda function.
The method recommended by Amazon is to create a zipped deployment package that includes the dependencies and python function all together (as described in AWS Lambda Deployment Package in Python). However, this results in not being able to edit the Lambda function using inline code editing within the AWS Lambda GUI.
So instead, I would like to pip install the package within the AWS Lambda function itself. In AWS Lambda, the filesystem is read-only apart from the /tmp/ directory, so I am trying to pip install to the /tmp/ directory. The function is only called once-daily, so I don't mind about the few extra seconds required to re-pip install the package every time the function is run.
My attempt
def lambda_handler(event, context):
# pip install dependencies
print('begin lambda handler')
import subprocess
import sys
subprocess.call('pip install cryptography -t /tmp/ --no-cache-dir'.split())
from cryptography.fernet import Fernet
pwd_encrypted = b'gAAAAABeTcT0OXH96ib7TD5-sTII6jMfUXPhMpwWRCF0315rWp4C0yav1XAPIn7prfkkA4tltYiWFAJ22bwuaj0z1CKaGl8vTgNd695SDl25HnLwu1xTzaQ='
key = b'fP-7YR1hUeVW4KmFmly4JdgotD6qjR52g11RQms6Llo='
cipher_suite = Fernet(key)
result = cipher_suite.decrypt(pwd_encrypted).decode('utf-8')
print(result)
print('end lambda handler')
However, this results in the error
[ERROR] ModuleNotFoundError: No module named 'cryptography'
I have also tried replacing the subprocess call with the following, as recommended in this stackoverflow answer
cmd = sys.executable+' -m pip install cryptography -t dependencies --no-cache-dir'
subprocess.check_call(cmd.split())
However, this results in the error
OSError: [Errno 30] Read-only file system: '/var/task/dependencies'
pip
inside Lambda. You could package your dependencies into an AWS Lambda Layer, which would then let you edit the remaining code in the Lambda management console. – John Rotenstein/tmp/
directory. – John Rotenstein