pythonserverless-frameworkserverless-offline

How to reference handlers in sub-directories in serverless-offline?


Windows 10 64bits

Node v12.16.3

Serverless 1.71.3

serverless-offline 6.1.7

DIRECTORY:

root:
- package.json
- /sub
-- __init__.py
-- sub_handler.py
- ...
- handler.py
- serverless.yml

Hi, I'm currently trying to test locally two lambda functions using serverless-offline. It's working for the handler in the root directory but it seems that serverless-offline can't reference other handlers in sub-directories..

How can I reference handlers in a sub-directory?

PS: This configuration works when deployed to AWS LAMBDA. I can post both paths.

Error when I try to post hello2:

[offline] Loading handler... (C:\Users\my_user\Desktop\serverless-offline\sub\sub_handler) Traceback (most recent call last):
File C:\Users\my_user\Desktop\serverless-offline\node_modules\serverless-offline\dist\lambda\handler-runner\python-runner\invoke.py", line 75, in

    module = import_module(args.handler_path.replace('/', '.'))
File "C:\Users\my_user\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py",

line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import

File "<frozen importlib._bootstrap>", line 971, in _find_and_load   
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked

ModuleNotFoundError: No module named 'sub\sub_handler'

serverless.yml:

service: sls-offline-test    
provider:
  name: aws
  runtime: python3.6
  memorySize: 256
  stage: ${opt:stage, 'dev'}
  region: us-east-1
  environment:
    STAGE: ${self:provider.stage}    
functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: hello
          method: POST
  hello2:
    handler: sub/sub_handler.hello2
    events:
      - http:
          path: 'hello2'
          method: POST    
package:
  individually: true    
  include:
    - sub/**
plugins:
  - serverless-offline

Solution

  • Recently, I was getting the same problem, I updated the version of serverless-offline plugin and worked out for me