• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Mister PKI

SSL Certificates * SSL Tools * Certificate Decoder

  • Buy SSL Certificates
  • Blog
  • OpenSSL
  • Keytool
  • SSL Tools
  • Donate

AWS Lambda pyOpenSSL and cryptography

February 11, 2022 by Mister PKI Leave a Comment

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.

  1. Create a new EC2 instance using t2.micro and Amazon Linux 2.
  2. SSH into the machine with the generated private key from AWS.
  3. On the newly created EC2 instance, install python and virtualenv.
  4. Create a new python virtual environment by running virtualenv myvirtualenv
  5. Activate the virtualenv by running source myvirtualenv/bin/activate
  6. 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
  1. Create a new directory for the lambda sandbox by running mkdir lambda-sandbox
  2. 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/
  1. Create a zip archive of the sandbox: cd lambda-sandbox; zip -r ../lambda-sanbox.zip
  2. Copy the zip archive of the sandbox from the EC2 instance to the root context of your AWS Lambda function project.
  3. Extract the archive into the project and build the Lambda function to be deployed via zip, docker, etc.
  4. 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.

python

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Popular Posts

PKCS12

openssl s_client

Keytool

Keytool list

ECDSA vs RSA

OpenSSL

PKCS7

Certificate Decoder

Training Courses

Top online courses in IT & Software

Cyber Security Training

Udemy - The Complete Internet Security Privacy Course icon

Buy SSL Certificates

The SSL Store

Comodo Store

Sectigo Store

RapidSSL

Recent Posts

  • pfx password
  • pkcs12
  • Sendmail vs Postfix – Mail Transfer Agent Comparison
  • Python mock datetime now
  • Python get SSL Certificate

Footer

  • Twitter
  • YouTube

Pages

  • About Mister PKI
  • Blog
  • Compare and Buy Affordable PKI Certificates
  • Contact Us
  • Full Disclosure
  • Privacy Policy
  • SSL Tools – Certificate Decoder and Certificate Checker

Copyright © 2022