Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda not importing Asyncio

So I'm working on making an application for making Reddit API calls and it seems like Lambda isn't liking how I import asyncio. I installed asyncio in a package folder using "pip install asyncio -t ." and then zipped that folder up with my project file. And I'm importing asyncio in the project file (import asyncio). However, every time I try to test my Alexa application in the Alexa Developer Console, the application won't run until I've gotten rid of the import statement.

Here's the message I get when I try to test:

{ "errorMessage": "Syntax error in module 'reddit_alexa_py': invalid syntax (base_events.py, line 296)", "errorType": "Runtime.UserCodeSyntaxError", "stackTrace": [ " File \"/var/task/asyncio/base_events.py\" Line 296\n future = tasks.async(future, loop=self)\n" ] }

and here's the log output:

START RequestId: ee952162-1d06-4c04-9a0d-cfd4f0fce80f Version: $LATEST [ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'reddit_alexa_py': invalid syntax (base_events.py, line 296) Traceback (most recent call last):   File "/var/task/asyncio/base_events.py" Line 296             future = tasks.async(future, loop=self)END RequestId: ee952162-1d06-4c04-9a0d-cfd4f0fce80f REPORT RequestId: ee952162-1d06-4c04-9a0d-cfd4f0fce80f Duration: 19.71 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 57 MB

This is happening with other things I try to import, too. I think I'll need to import these packages in order to finish this project, so any help would be greatly appreciated. Thanks!

EDIT: here's a link to the Python file https://drive.google.com/file/d/1_rbNLlwRBmt_6J0YMMa4idPSfmOKZb_j/view?usp=sharing

like image 820
ssjcurry Avatar asked Dec 31 '19 16:12

ssjcurry


2 Answers

I'm on python 3.8 and had my program running great locally, but when packaged it up and added it to a lambda function I was getting the exact same error. Turns out, asyncio is part of python, but if you don't know that and do a pip to install it like I did, you'll get a second copy. I used this second copy in a layer and kept getting the error. After trying a LOT of different variants/versions/etc, I realized that the version included with python is completely different from the one pip installed. The solution is simple, make sure you do NOT try to pip asyncio, just let lambda pick it up from the python version you are using and make sure that you don't have a second (different) version somewhere in your search path...

like image 139
Tromster Avatar answered Oct 14 '22 20:10

Tromster


I used a similar process. I had to copy my imported module into a subdirectory of my working directory before zipping everything up. After loading the zip file into the Lambda function, I was able to verify that the code I wanted to import was loaded by looking at the function in the Lambda console.

enter image description here

The key for me was that, on my local computer, I had to find the psycopg2 directory and copy that whole directory into my working directory where I was writing postgresql_test.py before zipping up my working directory.

like image 32
user212514 Avatar answered Oct 14 '22 19:10

user212514