logandk / serverless-wsgi

Serverless plugin to deploy WSGI applications (Flask/Django/Pyramid etc.) and bundle Python packages
MIT License
493 stars 88 forks source link

Unable to import module 'wsgi' #1

Closed jsnowacki closed 8 years ago

jsnowacki commented 8 years ago

After testing the plugin with the project very much similar to the one in example, in which local tests work correctly, after deployment to AWS I get the following message on call:

{
  "errorMessage": "Unable to import module 'wsgi'"
}

Detailed log:

START RequestId: 6b36272e-920d-11e6-9aed-0d5b9d25097b Version: $LATEST
Unable to import module 'wsgi': No module named datastructures

END RequestId: 6b36272e-920d-11e6-9aed-0d5b9d25097b
REPORT RequestId: 6b36272e-920d-11e6-9aed-0d5b9d25097b  Duration: 0.41 ms   Billed Duration: 100 ms     Memory Size: 1024 MB    Max Memory Used: 15 MB  

It seems that the requirements are not correctly visible to WSGI. Everything in the deployment package seems to be in the right place.

Thanks for the help.

logandk commented 8 years ago

If you look inside the zip file created by serverless, du you see a .requirements directory?

jsnowacki commented 8 years ago

Yes, everything is in place and the "missing" werkzeug is in the folder. I think the problem might be with the python path adding, but I cannot check it, at least easily, as wsgi.py, which is the main point of entry, does not load.

logandk commented 8 years ago

I just tried deploying the example from the README, using this serverless.yml file:

service: example

provider:
  name: aws
  runtime: python2.7

plugins:
  - serverless-wsgi

package:
  exclude:
    - node_modules/**

functions:
  api:
    handler: wsgi.handler
    events:
      - http:
          path: cats
          method: get
          integration: lambda-proxy
      - http:
          path: dogs/{id}
          method: get
          integration: lambda-proxy

custom:
  wsgi:
    app: api.app

Producing the following Lambda bundle:

https://dl.dropboxusercontent.com/u/313905/example.zip

Could you check how it compares to your bundle?

jsnowacki commented 8 years ago

OK I've found that the datastructures.py from werkzeug were missing. The reason was that in my serverless.yml I had exclude as follows:

exclude:
    - data

This meant to exclude my data folder; when I changed it to - data/ only the folder is excluded correctly. I was not aware that this form of exclude works as a global wildcard.

Nonetheless, I get a new error now:

u'requestContext': KeyError Traceback (most recent call last): File "/var/task/wsgi.py", line 34, in handler script_name = '/
{}
'.format(event[u'requestContext'].get(u'stage', '')) KeyError: u'requestContext'

It may be related with the fact I' using CORS in Serverless:

events:
      - http:
          path: task
          method: get
          integration: lambda-proxy
          cors: true
logandk commented 8 years ago

Strange, enabling CORS does not cause any issues for my example (it shouldn't, as the CORS setting will just add an OPTIONS method to APIGW).

According to AWS docs, requestContext should always be available for LAMBDA-PROXY integrations. Could you check in API Gateway, that your endpoint is indeed configured with a LAMBDA-PROXY integration? Also, I assume that you're seeing that error in the logs after invoking the Lambda through a HTTP call to API Gateway, right?

screen shot 2016-10-15 at 18 57 07
jsnowacki commented 8 years ago

The last one did the trick, though, for some reason, serverless.yml setting as above, i.e.:

events:
      - http:
          path: task
          method: get
          integration: lambda-proxy
          cors: true

doesn't change the Integration Request: I had to change it manually. Also I've tested it on a vanilla environment and it still doesn't trigger the integration setting. But after setting it manually, Serverless doesn't change the setting back, so it's fine for now at least.

I also tested it adding two new endpoints foo and bar, with and without CORS, respectively. In both cases the lambda-proxy setting hasn't been added.

Do you think this is related with your project or should I report it to Serverless directly?

Thank you very much for the help!

logandk commented 8 years ago

Ok, great that you found the issue. It definitely sounds like a Serverless bug, and you should be able to reproduce regardless of using this plugin. I'll close this issue and it would be cool if you could report directly to Serverless!

jsnowacki commented 8 years ago

Just leaving a note: the issue with setting integration was related to older version of Serverless I had, namely, 1.0.0-rc2; after updating to 1.0.2 the issue dissipated.