Skip to content

invoke local issue when "module" is defined #520

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
lepirlouit opened this issue Jun 3, 2020 · 9 comments
Open

invoke local issue when "module" is defined #520

lepirlouit opened this issue Jun 3, 2020 · 9 comments

Comments

@lepirlouit
Copy link

I'm unable to invoke my function locally when I use the module functionality:

severless.yml

package:
  individually: true

functions:
  my_function:
     handler: my_function.lambda_handler
     module: src/my_function_folder

invoke local :
serverless invoke local -f my_function

got error :
ModuleNotFoundError: No module named 'my_function'

@lepirlouit
Copy link
Author

lepirlouit commented Jun 3, 2020

warkaround : this can be fixed in the serverless/lib/plugins/aws/invokeLocal/invoke.py file
by adding

line 72: import os
line 73: sys.path.append(os.path.join('.', "src", "my_function_folder"))```

@jorgeandresvasquez
Copy link

Having the same issue, have you been able to find any other solutions?

@lepirlouit
Copy link
Author

temporary try my workaround #520 (comment)

@sndrsnk
Copy link

sndrsnk commented Jun 22, 2020

I'm also affected by this issue. Removing the module: parameter works but then the plugin can't see my requirements.txt file as it's nested, so there's always going to be a discrepancy between the handler path determined locally and remotely. Thanks for the temporary workaround @lepirlouit!

@sndrsnk
Copy link

sndrsnk commented Jun 23, 2020

I've found another temporary workaround using environment variables in serverless.yml:

functions:
  LambdaTestFunction:
    handler: ${env:HANDLER_PATH_PREFIX, ""}handler.handler
    module: src/test_lambda
  ...
  ...
package:
  individually: true

When I invoke locally, I use the following command:

HANDLER_PATH_PREFIX=src/test_lambda/ LOCAL=true ./node_modules/serverless/bin/serverless.js invoke local -f LambdaTest -p ./tests/resources/base_event.yml

I've tested local and remote invocations and I can confirm it's working as expected.

@jamesoflol
Copy link

jamesoflol commented Jun 25, 2020

Thanks @sndrsnk. This works. But I then have a problem with importing modules from alongside handler.py. I find that they either work in local or lambda, depending on my syntax. As a workaround, I have to do this:

if "IS_LOCAL" in os.environ:
    from test_lambda import my_other_module
else:
    import my_other_module

Note that IS_LOCAL is automatically set by serverless framework - you don't need to make it yourself.

Python isn't my fave language, so I'm probably doing something dumb. Or is this required? (I tried doing from . import my_other_module but it only worked locally and not in lambda.)

@sndrsnk
Copy link

sndrsnk commented Jun 25, 2020

@jamesoflol You're welcome, and thanks for raising the issue regarding relative imports. I tested it on my end and can confirm that I'm experiencing the same issue. I did some research and found a really nice workaround here.

Simply add the following block of code to your package's __init__.py file:

import os
import sys

sys.path.append(os.path.dirname(os.path.realpath(__file__)))
└── test_lambda
    ├── __init__.py <=== add the code to this one
    ├── handler.py
    ├── requirements.txt
    └── some_module
        ├── __init__.py
        └── example.py

Hope it helps!

@thomaschen
Copy link

@sndrsnk Thanks for sharing!

Here' another workaround by setting the PYTHONPATH environment variable to the module field in serverless.yml when you invoke local. In this case, you don't need to add local-only logic to the serverless.yml, and the good thing is the relative imports would still work as-is.

PYTHONPATH=YOUR_MODULE serverless invoke local --function YOUR_FUNCTION

@tw1t611
Copy link

tw1t611 commented Jun 30, 2022

The provided workaround works great for functions without an import of an external package.
However it does not include the packages installed in venv. Is there any solution to this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants