Import error for pymongo #117
-
I worked through the tutorial to set up AWS Lambda with an API gateway and associated MongoDB database. I ran the updated main.py code after adding the associated credentials to my_secrets.py (and environment variable for the Lambda function). I received the following error:
The tutorial notes to add the pymongo layer to the lambda function, but I don't see an option for it: Is this a custom layer that should be added? Or is it included as part of one of the powertools options? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 3 replies
-
We are thinking that we might remove the AWS part from the hello world course. What this would mean is the orchestrator would handle uploading and downloading of data, and there would be no direct upload of data via the microcontroller. Instead, that concept, which is still important for some applications, would be covered in a later course. Thoughts? For the specific error mentioned, @SissiFeng could you advise? |
Beta Was this translation helpful? Give feedback.
-
Select Custom Layer. |
Beta Was this translation helpful? Give feedback.
-
@SissiFeng thanks for your reply. I had checked the custom layers before but there are none compatible for my runtime (Python 3.12). Do you know for which runtime the custom pymongo layer is compatible? I'm wondering if a different version of Python might be required. Additionally, I tried to create a layer and upload the tar.gz file from the pymongo website (link) but it was flagged as incompatible. Is there a separate pymongo_layer.zip file that I'm not finding on my end? |
Beta Was this translation helpful? Give feedback.
-
The current AWS Lambda stable supports Python versions 3.7-3.10. If aws suggests an incompatible error, you can also install pymongo in local environment and package it as a Lambda layer. |
Beta Was this translation helpful? Give feedback.
-
Related issue: #108. Hopefully will be better resolved in #132. |
Beta Was this translation helpful? Give feedback.
-
Hi all, thank you to both @sgbaird and @SissiFeng for their help. Also, thanks to the poster from #108 who provided insight as well. Glad to hear #132 is being put together. I do think learning the Lambda/API Gateway/MongoDB pipeline was worthwhile and, for what it's worth, I do support keeping this as part of the course. I just wanted to update here for completion's sake since the solution was more involved than expected. Firstly, since pymongo was added manually, the .zip file had to be reorganized to reflect the proper path in order for the python runtime to access it: This technically solved the import error I had originally flagged when I opened this discussion. There was also a mismatch between the main.py file and lambda function, but this was also flagged by #108 so I won't elaborate on it. Finally, connecting with MongoDB using the connection string generated from MongoDB directly raised timeout errors which were difficult to debug (MongoDB doesn't raise exceptions, there is just no connection, so the timeout error is from the Lambda function). Some things I noted:
Ultimately, I was not able to work with the mongodb:// URL directly and instead downloaded dnspython as a separate layer (with the same path correction I mentioned above) that I load before pymongo in order to process the mongodb+srv URL. With the updated database user password credentials, I was able to connect and write to the database successfully. I'm sure there is a more elegant way to do it but this workaround did solve the issue so I thought it would be worth documenting here. |
Beta Was this translation helpful? Give feedback.
Hi all, thank you to both @sgbaird and @SissiFeng for their help. Also, thanks to the poster from #108 who provided insight as well. Glad to hear #132 is being put together. I do think learning the Lambda/API Gateway/MongoDB pipeline was worthwhile and, for what it's worth, I do support keeping this as part of the course. I just wanted to update here for completion's sake since the solution was more involved than expected.
Firstly, since pymongo was added manually, the .zip file had to be reorganized to reflect the proper path in order for the python runtime to access it:
This technically solved the import error I had originally flagged when I opened this discussion.
There was also a mis…