Miserlou / Zappa

Serverless Python
https://blog.zappa.io/
MIT License
11.89k stars 1.21k forks source link

slim_handler + lambda_handler doesn't work #941

Open xuru opened 7 years ago

xuru commented 7 years ago

Context

When setting slim_handler in the zappa_settings.py, the handler can not find my python function.

Expected Behavior

The handler finds my python module and executes the function.

Actual Behavior

The handler fails to find my python module.

Log output:

START RequestId: c14f364d-55fd-11e7-a944-cd633b8a58cc Version: $LATEST
Unable to import module 'mymodule': No module named 'mymodule'

END RequestId: c14f364d-55fd-11e7-a944-cd633b8a58cc
REPORT RequestId: c14f364d-55fd-11e7-a944-cd633b8a58cc  Duration: 0.39 ms   Billed Duration: 100 ms     Memory Size: 512 MB Max Memory Used: 20 MB  

Result returned from handler:

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

Possible Fix

Steps to Reproduce

  1. Add zappa_settings.json and mymodule.py below to a directory.
  2. pip install -U zappa
  3. zappa deploy dev

Your Environment

def process_events(event, context): for arg in sys.argv: print(arg) print(os.getcwd()) print(os.path.basename(file)) for key in os.environ.keys(): print('{0}={1}'.format(key,os.environ[key])) print(os.getuid()) print(os.getgid()) print(os.geteuid()) print(os.getegid()) print(os.getgroups()) print(os.umask(0o222))

print(event)
print(context.get_remaining_time_in_millis())
print(context.aws_request_id)
if context.client_context:
    print(context.client_context)
print(context.function_name)
print(context.function_version)
print(context.identity.cognito_identity_id)
print(context.identity.cognito_identity_pool_id)
print(context.invoked_function_arn)
print(context.log('Log this for me please'))
print(context.log_group_name)
print(context.log_stream_name)
print(context.memory_limit_in_mb)

return 'It works!'
xuru commented 7 years ago

After a zappa package dev, I have the following files:

-rw-r--r--   1 ericplaster  staff   9.2M Jun 21 10:00 handler_zappaprojecttest-dev-1498057185.zip
-rw-r--r--   1 ericplaster  staff   1.0K Jun 20 16:06 mymodule.py
-rw-r--r--   1 ericplaster  staff   360B Jun 20 16:14 zappa_settings.json
-rw-r--r--   1 ericplaster  staff   9.2M Jun 21 09:59 zappaprojecttest-dev-1498057130.zip

Contents of handler_zappaprojecttest-dev-1498057185.zip:

$ du -sh *
 28K    PyYAML-3.12.dist-info
 60K    Unidecode-0.04.20.dist-info
 36K    Werkzeug-0.12.dist-info
  0B    __init__.py
 48K    __pycache__
128K    argcomplete
 52K    argcomplete-1.8.2.dist-info
 36K    base58-0.2.4.dist-info
4.0K    base58.py
1.3M    boto3
 36K    boto3-1.4.4.dist-info
 24M    botocore
 84K    botocore-1.5.40.dist-info
708K    certifi
 28K    certifi-2017.4.17.dist-info
1.0M    chardet
 36K    chardet-3.0.4.dist-info
496K    click
 28K    click-6.7.dist-info
428K    dateutil
 16K    durationpy
 28K    durationpy-0.4.dist-info
4.0K    easy_install.py
3.0M    future
 60K    future-0.16.0.dist-info
 28K    futures-3.1.1.dist-info
 20K    handler.py
440K    hjson
 32K    hjson-2.0.7.dist-info
524K    idna
 40K    idna-2.5.dist-info
148K    jmespath
 40K    jmespath-0.9.3.dist-info
216K    kappa
 56K    kappa-0.6.0.dist-info
 20K    lambda_packages
 32K    lambda_packages-0.15.1.dist-info
284K    libfuturize
236K    libpasteurize
120K    past
888K    pkg_resources
 44K    placebo
 36K    placebo-0.8.1.dist-info
 32K    python_dateutil-2.6.0.dist-info
 32K    python_slugify-1.2.4.dist-info
 24K    requestlogger
376K    requests
120K    requests-2.18.1.dist-info
384K    s3transfer
 28K    s3transfer-0.1.10.dist-info
1012K   setuptools
 52K    setuptools-36.0.1.dist-info
 28K    six-1.10.0.dist-info
 32K    six.py
 20K    slugify
 28K    toml-0.9.2.dist-info
 24K    toml.py
184K    tqdm
 84K    tqdm-4.14.0.dist-info
724K    troposphere
 40K    troposphere-1.9.4.dist-info
2.1M    unidecode
752K    urllib3
 88K    urllib3-1.21.1.dist-info
1.8M    werkzeug
432K    wheel
 56K    wheel-0.29.0.dist-info
 32K    wsgi_request_logger-0.4.6.dist-info
412K    yaml
460K    zappa
148K    zappa-0.42.1.dist-info
4.0K    zappa_settings.py

Contents of zappaprojecttest-dev-1498057130.zip:

$ du -sh *
 28K    PyYAML-3.12.dist-info
 60K    Unidecode-0.04.20.dist-info
 36K    Werkzeug-0.12.dist-info
  0B    __init__.py
 48K    __pycache__
128K    argcomplete
 52K    argcomplete-1.8.2.dist-info
 36K    base58-0.2.4.dist-info
4.0K    base58.py
1.3M    boto3
 36K    boto3-1.4.4.dist-info
 24M    botocore
 84K    botocore-1.5.40.dist-info
708K    certifi
 28K    certifi-2017.4.17.dist-info
1.0M    chardet
 36K    chardet-3.0.4.dist-info
496K    click
 28K    click-6.7.dist-info
428K    dateutil
 16K    durationpy
 28K    durationpy-0.4.dist-info
4.0K    easy_install.py
3.0M    future
 60K    future-0.16.0.dist-info
 28K    futures-3.1.1.dist-info
440K    hjson
 32K    hjson-2.0.7.dist-info
524K    idna
 40K    idna-2.5.dist-info
148K    jmespath
 40K    jmespath-0.9.3.dist-info
216K    kappa
 56K    kappa-0.6.0.dist-info
 20K    lambda_packages
 32K    lambda_packages-0.15.1.dist-info
284K    libfuturize
236K    libpasteurize
4.0K    mymodule.py
120K    past
 68K    pip-9.0.1.dist-info
888K    pkg_resources
 44K    placebo
 36K    placebo-0.8.1.dist-info
 32K    python_dateutil-2.6.0.dist-info
 32K    python_slugify-1.2.4.dist-info
 24K    requestlogger
376K    requests
120K    requests-2.18.1.dist-info
384K    s3transfer
 28K    s3transfer-0.1.10.dist-info
1012K   setuptools
 52K    setuptools-36.0.1.dist-info
 28K    six-1.10.0.dist-info
 32K    six.py
 20K    slugify
 28K    toml-0.9.2.dist-info
 24K    toml.py
184K    tqdm
 84K    tqdm-4.14.0.dist-info
724K    troposphere
 40K    troposphere-1.9.4.dist-info
2.1M    unidecode
752K    urllib3
 88K    urllib3-1.21.1.dist-info
1.8M    werkzeug
432K    wheel
 56K    wheel-0.29.0.dist-info
 32K    wsgi_request_logger-0.4.6.dist-info
412K    yaml
460K    zappa
148K    zappa-0.42.1.dist-info
4.0K    zappa_settings.json
xuru commented 7 years ago

If I use app_handler instead of lambda_handler, it will run, but I have to make sure and pass {"command": "mymodule.process_events"} to the function.

xuru commented 7 years ago

So, to sum up, going off the example zappa_settings.json, if I use lambda_handler + slim_handler, it won't work. If I use app_handler + slim_handler, even though it's not a WSGI app, it will work, as long as you pass {"command": "mymodule.process_events"} to the handler.

Also, handler.lambda_handler will be displayed as the handler in the AWS console for the lambda.

abhisuri97 commented 7 years ago

Hey xuru, do you mind posting exactly how/where you passed the {"command": "mymodule.process_events"} to the handler? I'm having the exact same issue as you.

xuru commented 7 years ago

In the AWS console, you can do this:

lambda_management_console lambda_management_console 2
xuru commented 7 years ago

If you use app_handler + slim_handler, triggers (timed events is all I've tried so far) and zappa invoke dev seem to work.

SongYunSeop commented 7 years ago

I got a same problem.

I use slim_handler option because my project's size bigger than 50MB.

So I got 504 GATEWAY TIMEOUT.

I removed some libraries, removed slim_handler option and It is working normally.

lu911 commented 6 years ago

+1