The purpose of this article is to demonstrate AWS Lambda pyOpenssl and cryptography installation in an AWS Lambda function. AWS Lambda is a cost effective solution for running all kinds of different jobs in all kinds of different languages and programming stacks, including Python. Python code will run in AWS Lambda with a runtime provided by Lambda. Not to get off topic for the purpose of this article, but AWS is deprecating support for Python 2.x in the near future, so it is strongly recommended to proceed with Python 3.x.
One limitation or shortcoming if you will is the fact that not all python dependencies are readily available in the Lambda runtime. That point is what brings us to the purpose of this article which is to demonstrate how to install pyOpenSSL and cryptography libraries in the Python runtime in AWS Lambda.
AWS Lambda – Install pyOpenSSL
You may have received the following error message when importing your Lambda function into AWS:
"errorMessage": "Unable to import module 'lambda_function': No module named 'OpenSSL'"
This means that the pyOpenSSL module is not available in the Python runtime and must be added by you. Unfortunately this is not an easy process, but can be done nonetheless.
To install pyOpenSSL and cryptography in your AWS Lambda Python runtime, perform the following steps.
- Create a new EC2 instance using t2.micro and Amazon Linux 2.
- SSH into the machine with the generated private key from AWS.
- On the newly created EC2 instance, install
python
andvirtualenv
. - Create a new python virtual environment by running
virtualenv myvirtualenv
- Activate the virtualenv by running
source myvirtualenv/bin/activate
- Install the pyOpenSSL and cryptography modules by running:
pip3 install \
--platform manylinux2010_x86_64 \
--implementation cp \
--python 3.7 \
--only-binary=:all: \
--upgrade \
--target myvirtualenv/lib/python3.7/site-packages/ \
cryptography pyOpenSSL
- Create a new directory for the lambda sandbox by running
mkdir lambda-sandbox
- Copy the virtualenv python libraries to the sandbox.
cp -rf myvirtualenv/lib/python3.7/site-packages/* lambda-sandbox/
cp -rf myvirtualenv/lib64/python3.7/site-packages/* lambda-sandbox/
- Create a zip archive of the sandbox:
cd lambda-sandbox; zip -r ../lambda-sanbox.zip
- Copy the zip archive of the sandbox from the EC2 instance to the root context of your AWS Lambda function project.
- Extract the archive into the project and build the Lambda function to be deployed via zip, docker, etc.
- After deploying your Lambda function, test it to make sure it’s working with the newly added pyOpenSSL and cryptography libraries. If it is, go ahead and delete the EC2 instance as it will no longer be needed.
Conclusion
This article has demonstrated how to install pyOpenSSL and cryptography libraries in AWS Lambda. If you have any questions, comments, or would like to see further examples of how to install additional dependencies or libraries in your Lambda runtime then leave us a comment. Visit our other articles for more how to’s in different programming languages and stacks.
Leave a Reply