Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gene1wood/8db4a72a9df2618a6e9f1dfd7c943e43 to your computer and use it in GitHub Desktop.
Save gene1wood/8db4a72a9df2618a6e9f1dfd7c943e43 to your computer and use it in GitHub Desktop.
Fix for python module cryptography certificate_transparency libffi error in AWS Lambda

The problem

In a AWS Lambda function that uses the python cryptography module, possibly as a dependency of the pyOpenSSL module, you get the error

cannot import name certificate_transparency

Or alternatively you get an error like this

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "cryptography/x509/__init__.py", line 8, in <module>
    from cryptography.x509.base import (
  File "cryptography/x509/base.py", line 16, in <module>
    from cryptography.x509.extensions import Extension, ExtensionType
  File "cryptography/x509/extensions.py", line 18, in <module>
    from cryptography.hazmat.primitives import constant_time, serialization
  File "cryptography/hazmat/primitives/constant_time.py", line 9, in <module>
    from cryptography.hazmat.bindings._constant_time import lib
ImportError: libffi-45372312.so.6.0.4: cannot open shared object file: No such file or directory

Cause

One possible cause of this is that when you produced the zip file containing the cryptography module and your Lambda code to upload to AWS Lambda, you used a command like this to create the zip

zip -r ~/lambda_function.zip *

The problem is using * skips hidden files beginning with the . character. One of those files is

.libs_cffi_backend

This file, when missing causes the errors shown above

Fix

When creating the lambda_function, use . instead of * like this

zip -r ~/lambda_function.zip .

References

@guytamir87
Copy link

worked great. thanks!

@yermulnik
Copy link

Thanks!

@amcarvalho
Copy link

I've just created a package which includes the hidden files/folders, but still having the same problem. I've made sure libffi was included by unzipping the zip which was uploaded and checking. Anyone had the same problem?

@pratibhagupta2109
Copy link

pratibhagupta2109 commented Jul 8, 2018

@amcarvalho, I am also facing same issue. I am having hidden file in my zipped folder. If you found any solution, please let me know.

@johnrussell9983
Copy link

Worked great. Thanks so much!

@blakev
Copy link

blakev commented Apr 29, 2020

This worked for me, thanks! This is how I solved it in the Dockerfile:

RUN mkdir -p /mnt/artifacts/python
RUN cd /var/lang/lib/python3.8/site-packages \
    && find . -name . -o -exec sh -c 'mv -- "$@" "$0"' \
        /mnt/artifacts/python {} + -type d -prune
RUN cd /mnt/artifacts && zip -r9 layer.zip .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment