Help with Python AttributeError

0

I have a very simple Lambda function in Python. I deployed the Lambda with Python 3.10, but the zip file for deployment was created with Python 3.11 from my local. I am not sure if this caused the problem. My test failed with the following error message:

[ERROR] AttributeError: module 'cryptography.hazmat.bindings._rust.openssl' has no attribute 'hashes'
Traceback (most recent call last):
  File "/var/lang/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/var/task/lambda_function.py", line 2, in <module>
    import pymysql
  File "/var/task/pymysql/__init__.py", line 59, in <module>
    from . import connections  # noqa: E402
  File "/var/task/pymysql/connections.py", line 13, in <module>
    from . import _auth
  File "/var/task/pymysql/_auth.py", line 9, in <module>
    from cryptography.hazmat.primitives import serialization, hashes
  File "/var/task/cryptography/hazmat/primitives/serialization/__init__.py", line 7, in <module>
    from cryptography.hazmat.primitives._serialization import (
  File "/var/task/cryptography/hazmat/primitives/_serialization.py", line 11, in <module>
    from cryptography.hazmat.primitives.hashes import HashAlgorithm
  File "/var/task/cryptography/hazmat/primitives/hashes.py", line 88, in <module>
    Hash = rust_openssl.hashes.Hash

From the deployment zip file I can see the following:

file cryptography/hazmat/primitives/hashes.py

 line 10:  `from cryptography.hazmat.bindings._rust import openssl as rust_openssl`

 line 88:  `Hash = rust_openssl.hashes.Hash`

Content of file cryptography\hazmat\bindings\_rust\openssl\hashes.pyi

import typing

from cryptography.hazmat.primitives import hashes

class Hash(hashes.HashContext):
    def __init__(
        self, algorithm: hashes.HashAlgorithm, backend: typing.Any = None
    ) -> None: ...
    @property
    def algorithm(self) -> hashes.HashAlgorithm: ...
    def update(self, data: bytes) -> None: ...
    def finalize(self) -> bytes: ...
    def copy(self) -> Hash: ...

I am Very New with both Lambda and Python. I appreciate your advice.

Gary Y
asked a year ago2149 views
2 Answers
0
Accepted Answer

I received an answer from the other post.

The root cause is that Limbda's runtime is Linux system. The Python zip file need to be packaged from a Linux environment. After installing WSL (Amazon Linux 2) on my Windows, I packaged the Python code from WSL, the Limbda function worked as expected.

Gary Y
answered 10 months ago
profile picture
EXPERT
reviewed a month ago
0

The error you're encountering is typically due to a mismatch between the Python version used to create your deployment package and the Python runtime version specified for your AWS Lambda function.

In your case, you've created the deployment package using Python 3.11, but your Lambda function is using the Python 3.10 runtime. This discrepancy can lead to compatibility issues, as certain modules or functionalities may have changed between different Python versions.

To resolve this issue, you should ensure that the Python version used for packaging your dependencies matches the Python runtime version of your Lambda function. Here are the steps:

  1. Create a virtual environment: Use Python 3.10 to create a virtual environment on your local machine. This isolates your project and allows you to install dependencies without affecting your global Python installation. You can create a virtual environment using the venv module:
python3.10 -m venv myenv
  1. Activate the virtual environment: Before you can start installing or using packages in the virtual environment, you'll need to activate it. On Unix or MacOS, use:
source myenv/bin/activate
  1. Install your dependencies: Now that you're in your virtual environment, you can install your dependencies using pip:
pip install -r requirements.txt
  1. Create your deployment package: After installing your dependencies, you can create your deployment package. Make sure to include your function code and any dependencies:
zip -r my_lambda.zip .
  1. Upload your deployment package: Finally, upload your deployment package to AWS Lambda.

By ensuring that your deployment package is created with the same Python version as your Lambda function's runtime, you can avoid compatibility issues and ensure that your function works as expected.

profile picture
answered a year ago
profile picture
EXPERT
reviewed a year ago
  • Yusuf, Thank you so much for the detailed instructions!

    I currently only have Python 3.11 installed on my Windows 10 machine. Do I need to install Python 3.10 to create a virtual environment of 3.10?

  • Since I didn't have Python 3.10 executables to create a 3.10 virtual environment, I uninstalled Python 3.11 all together and installed Python 3.10. After that I followed the instructions above, but still received the same error messages. Any more idea and advice?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions