Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 [BUG] - Unable to import module 'lambda_handler': No module named 'openai' #191

Closed
marcelfolaron opened this issue Dec 13, 2023 · 7 comments
Labels
bug Something isn't working dependencies Pull requests that update a dependency file good first issue Good for newcomers python Pull requests that update Python code terraform Pull requests that update Terraform code

Comments

@marcelfolaron
Copy link

Description

Hi,
I was following the instructions for the REST API and for the most part everything worked great but the vast majority of endpoints on postman return the following error message:

"errorMessage": "Unable to import module 'lambda_handler': No module named 'openai'",
"errorType": "Runtime.ImportModuleError",

I tried verifying that I am using python 3.11 and that everything gets installed correctly but can't really figure out what the issue is.

Thank you

Reproduction URL

https://github.com/FullStackWithLawrence/aws-openai

Reproduction steps

1. Follow instructions on https://github.com/FullStackWithLawrence/aws-openai/tree/main/api
2. Try running passthrough.

Screenshots

![DESCRIPTION](LINK.png)

Logs

No response

Browsers

No response

OS

Mac

@marcelfolaron marcelfolaron added the bug Something isn't working label Dec 13, 2023
@lpm0073
Copy link
Member

lpm0073 commented Dec 13, 2023

Hi @marcelfolaron,

This is the exact line of code to which the import error is referring. This is a symptom indicating that your Lambda Layer is either not present or is corrupted (i'm guessing the former). More specifically, the package 'openai' is included in the contents of the AWS Lambda Layer that Terraform creates, here.

Creation of the Lambda Layer is by far the most technically complex operation that Terraform performs during this build. The layer itself essentially contains a run-time copy of the Python virtual environment that both Lambdas use. This helps to minimize the Lambda package size, which in turn has some nice side benefits like for example, the source code of the Lambdas becomes viewable in the AWS console once the package dependencies have been moved to a Layer. This also ensures that both Lambdas are using the same Python package dependencies, which is nice, and helps with trouble shooting. Lastly, the Layer code and Lambda function source code change for quite different reasons, and at different frequencies, so it's nice to have these separated so that these can be independently managed.

Of note:

you need Docker and Docker Compose, which are used in order to avoid strange platform incompatibility issues that can surface with some of the larger PyPi packages that are included in the build such as NumPy and SciPy. You can refer here and here to see how Docker Compose is being used to create the Layer.

If the Layer exists and is configured correctly then you'll be able to match up what you see in the AWS console to these two screen shots:
Screenshot 2023-12-13 at 11 34 54鈥痑 m
Screenshot 2023-12-13 at 11 35 30鈥痑 m

@lpm0073 lpm0073 added good first issue Good for newcomers dependencies Pull requests that update a dependency file terraform Pull requests that update Terraform code python Pull requests that update Python code labels Dec 13, 2023
@lpm0073 lpm0073 pinned this issue Dec 13, 2023
@marcelfolaron
Copy link
Author

Thank you for your response!
But yeah this is rather puzzling to me. The screenshots align and yet I am still getting this error. I'll take another look at the docker set up. I read somewhere else that this could be a version alignment issue. Which openAI python package versions are supported?

image image

@marcelfolaron
Copy link
Author

Maybe related: Have you seen any issues running everything on a Macbook M1. I did have to go through some additional steps getting terraform running on my mac due to Arm incompatibility. In particular I had to compile the template 2.2.0 module using https://kreuzwerker.de/en/post/use-m1-terraform-provider-helper-to-compile-terraform-providers-for-mac-m1

@lpm0073
Copy link
Member

lpm0073 commented Dec 13, 2023

Yes. the terraform run-time issues are resolved at this point, so nothing to discuss on that front. but the platform-specific issues i mention above regarding the build for the Layer is something specific to Python that is permanent. at issue is that certain, large PyPi packages include Cython as well as really old source files such as C and C header files which have to be compiled for the platform on which they'll run (ie Amazon Linux). Hence, the Dockerfile

@marcelfolaron
Copy link
Author

Thank you for your help! I think I am almost there. Was able to fix my python configuration so that the openAI import works. I am now running into a problem with pydantic_core. It appears to be a platform issue as well since it's pulling the arm version on my mac when I need the x86 version. Is that part of the docker build as well?

@lpm0073
Copy link
Member

lpm0073 commented Dec 14, 2023

That sounds familiar. you might want to review this thread -- pydantic/pydantic#4699.
More generally, the terraform variable compatible_architecture should be set to "x86_64" here , and you can confirm from this window in the AWS console

Screenshot 2023-12-14 at 6 28 25鈥痑 m

also, your Dockerfile should be using AWS' linux/amd64 image, see here

The Docker approach should make moot the point that you're working on a Mac, as the Dockerfile pulls everything based on the platform designated at the top of the file.

@lpm0073
Copy link
Member

lpm0073 commented Jan 14, 2024

Closing this, as there hasn't been any further discussion in the last month.

@lpm0073 lpm0073 closed this as completed Jan 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dependencies Pull requests that update a dependency file good first issue Good for newcomers python Pull requests that update Python code terraform Pull requests that update Terraform code
Projects
None yet
Development

No branches or pull requests

2 participants