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
질문됨 일 년 전2281회 조회
2개 답변
0
수락된 답변

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
답변함 일 년 전
profile picture
전문가
검토됨 2달 전
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
답변함 일 년 전
profile picture
전문가
검토됨 일 년 전
  • 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?

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠