Discussion Forums
Discussion Forums > Category: Compute > Forum: AWS Lambda >Thread: ImportError: /var/task/cryptography/hazmat/bindings/_constant_time.so: inva
Advanced search options
ImportError: /var/task/cryptography/hazmat/bindings/_constant_time.so: inva
Posted by: shane
Posted on: Jul 20, 2016 4:03 PM
  Click to reply to this thread Reply
This question is not answered. Answer it to earn points.
Hi,

I have a problem of running my code on Lambda. I followed the steps in this tutorial (https://aws.amazon.com/blogs/compute/scheduling-ssh-jobs-using-aws-lambda/)
I have installed pycrypto successfully.
The code is like that:
```
import boto3
import paramiko
def worker_handler(event, context):

s3_client = boto3.client('s3')
#Download private key file from secure S3 bucket
s3_client.download_file('s3-bucket-with-key','keys/devenv-key.pem', '/tmp/devenv-key.pem')

k = paramiko.RSAKey.from_private_key_file("/tmp/devenv-key.pem")
...
```
And this is the error message:
```
/var/task/cryptography/hazmat/bindings/_constant_time.so: invalid ELF header: ImportError Traceback (most recent call last): File "/var/task/worker_function.py", line 9, in worker_handler
k = paramiko.RSAKey.from_private_key_file("/tmp/devenv-key.pem")
File "/var/task/paramiko/pkey.py", line 196, in from_private_key_file key = cls(filename=filename, password=password)
File "/var/task/paramiko/rsakey.py", line 45, in __init__
self._from_private_key_file(filename, password)
File "/var/task/paramiko/rsakey.py", line 164, in _from_private_key_file
self._decode_key(data)
File "/var/task/paramiko/rsakey.py", line 173, in _decode_key
data, password=None, backend=default_backend()
File "/var/task/cryptography/hazmat/backends/__init__.py", line 35, in default_backend _default_backend = MultiBackend(_available_backends())
File "/var/task/cryptography/hazmat/backends/__init__.py", line 22, in _available_backends "cryptography.backends"
File "/var/task/pkg_resources/__init__.py", line 2235, in resolve module = __import__(self.module_name, fromlist=, level=0)
File "/var/task/cryptography/hazmat/backends/commoncrypto/__init__.py", line 7, in <module>
from cryptography.hazmat.backends.commoncrypto.backend import backend
File "/var/task/cryptography/hazmat/backends/commoncrypto/backend.py", line 11, in <module> from cryptography.hazmat.backends.commoncrypto.ciphers import ( File "/var/task/cryptography/hazmat/backends/commoncrypto/ciphers.py", line 11, in <module>
from cryptography.hazmat.primitives import ciphers, constant_time
File "/var/task/cryptography/hazmat/primitives/constant_time.py", line 9, in <module> from cryptography.hazmat.bindings._constant_time import lib
ImportError: /var/task/cryptography/hazmat/bindings/_constant_time.so: invalid ELF header
```

Thanks in advanced, hope someone can help me.
Permlink Replies: 4 | Pages: 1 - Last Post: Aug 10, 2016 12:20 AM by: stix_121
Replies
Re: ImportError: /var/task/cryptography/hazmat/bindings/_constant_time.so: inva
Posted by: stix_121
Posted on: Jul 24, 2016 4:50 AM
in response to: shane in response to: shane
  Click to reply to this thread Reply
Hi there Shane,
I seem to be having the exact same issue as you are.
I've now tried about 3 different ways of getting the key into Lambda, all result in the same exception.

Please would you let me know if you find a solution to this, it's bugging me!

Thanks
Sinjin
Re: ImportError: /var/task/cryptography/hazmat/bindings/_constant_time.so: inva
Posted by: jwd
Posted on: Aug 1, 2016 9:56 AM
in response to: shane in response to: shane
  Click to reply to this thread Reply
Helpful
I am new to python and lambda and ran into the same problem, and I finally found a solution.

My first problem was I was trying to deploy from a Windows python environment -- the directory contained _constant_time.dll instead of .so

I then switched to a aws linux ec2 instance. The problem I ran into here was i could never get the pycrypto and paramiko modules to install in the virtualenv -- despite pip install indicating success. The pip install of pycrypto and paramiko in the global system installed fine. Pip show <module> -- called from within the global environment and within the virtualenv -- clued me in to the differences in these installs.

I zip'd /usr/lib/python2.7/dist-packages and /usr/lib64/python2.7/dist-packages (admittedly overkill) and this allowed me to successfully deploy the lambda function.
Re: ImportError: /var/task/cryptography/hazmat/bindings/_constant_time.so: inva
Posted by: shane
Posted on: Aug 9, 2016 2:50 PM
in response to: jwd in response to: jwd
  Click to reply to this thread Reply
Hi jwd,

I am glad to hear that you have this running well. I also use virtualenv to install these modules but it got these error message I showed above. Do you said you install those modules globally and everything works?
Re: ImportError: /var/task/cryptography/hazmat/bindings/_constant_time.so: inva
Posted by: stix_121
Posted on: Aug 10, 2016 12:20 AM
in response to: shane in response to: shane
  Click to reply to this thread Reply
That's awesome, thanks for posting a solution!
I ended up packaging dropbear ssh client which I built on the AWS Linux AMI.
I then use subprocess to issue it command line commands.

Paramiko would be best, thank you.