Serverless Offline: Error while running handler, cannot find module

6.6k Views Asked by At

I've been trying to use serverless-offline / serverless-offline-python and a family of other node packages (serverless-s3-local for example) to build an offline testing environment for some lambdas that I've written.

Using serverless-offline-python, I am able to start my python lambdas and invoke each function manually with:

sls offline start --stage local --noTimeout --watch --port 3000
sls invoke local --stage local -f import-processor-stage-1 -d '{"event": "", "context": ""}'

This works, but doesn't quite simulate what the lambda is actually doing. Ideally, I would simulate an S3 bucket having a new object imported and that action would be the catalyst for the lambda (import-processor-stage-1).

Enter serverless-s3-local.

I've rigged up a pretty fantastic configuration with the plugin, but for some reason when serverless-s3-local goes to trigger an event to my lambda handler, I receive this error:

Error while running handler { Error: Cannot find module '/Users/user/Documents/project/manager/infra/main/import_processor_stage_1'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.createHandler (/usr/local/lib/node_modules/serverless-offline/src/functionHelper.js:167:17)
    at Object.func (/Users/user/node_modules/serverless-s3-local/index.js:308:50)
    at /Users/user/node_modules/serverless-s3-local/index.js:170:11
    at SafeSubscriber.s3eventSubscription.client.s3Event.pipe.subscribe [as _next] (/Users/user/node_modules/serverless-s3-local/index.js:176:7)
    at SafeSubscriber.__tryOrUnsub (/Users/user/node_modules/rxjs/src/internal/Subscriber.ts:270:10)
    at SafeSubscriber.next (/Users/user/node_modules/rxjs/src/internal/Subscriber.ts:212:14)
    at Subscriber._next (/Users/user/node_modules/rxjs/src/internal/Subscriber.ts:141:22)
    at Subscriber.next (/Users/user/node_modules/rxjs/src/internal/Subscriber.ts:101:12)
    at MergeMapSubscriber.notifyNext (/Users/user/node_modules/rxjs/src/internal/operators/mergeMap.ts:159:22)
    at InnerSubscriber._next (/Users/user/node_modules/rxjs/src/internal/InnerSubscriber.ts:17:17)
    at InnerSubscriber.Subscriber.next (/Users/user/node_modules/rxjs/src/internal/Subscriber.ts:101:12)
    at /Users/user/node_modules/rxjs/src/internal/util/subscribeToArray.ts:9:16
    at Object.subscribeToResult (/Users/user/node_modules/rxjs/src/internal/util/subscribeToResult.ts:25:29)
    at MergeMapSubscriber._innerSub (/Users/user/node_modules/rxjs/src/internal/operators/mergeMap.ts:145:5)
    at MergeMapSubscriber._tryNext (/Users/user/node_modules/rxjs/src/internal/operators/mergeMap.ts:138:10)
    at MergeMapSubscriber._next (/Users/user/node_modules/rxjs/src/internal/operators/mergeMap.ts:122:12)
    at MergeMapSubscriber.Subscriber.next (/Users/user/node_modules/rxjs/src/internal/Subscriber.ts:101:12)
    at MapSubscriber._next (/Users/user/node_modules/rxjs/src/internal/operators/map.ts:86:22)
    at MapSubscriber.Subscriber.next (/Users/user/node_modules/rxjs/src/internal/Subscriber.ts:101:12)
    at Subject.next (/Users/user/node_modules/rxjs/src/internal/Subject.ts:68:17)
    at triggerS3Event (/Users/user/node_modules/s3rver/lib/controllers.js:56:21)
    at store.putObject (/Users/user/node_modules/s3rver/lib/controllers.js:498:11)
    at storeMetadata.err (/Users/user/node_modules/s3rver/lib/stores/filesystem.js:291:13)
    at /Users/user/node_modules/graceful-fs/graceful-fs.js:111:16
    at /usr/local/lib/node_modules/serverless/node_modules/graceful-fs/graceful-fs.js:43:10
    at FSReqWrap.oncomplete (fs.js:135:15) code: 'MODULE_NOT_FOUND' }

Sadly, I know that these plugins are in their enfancy (particularly serverless-offline-python), but I am quite sure that this should be a very easy fix. The module that is not found actually exists within the package! Im thinking that it is because it is a Python file and not a JS file. Is there a way within the js script to accept python files? So far, I have tried replacing all references of serverless-offline with serverless-offline-python within the serverless-s3-local code base, but I get the same error.

For reference, here is my serverless.yml file (for the record, the handler file is available, correctly located and works fine while manually invoking it):

service: catalog-import-manager

plugins:
 - serverless-s3-local
 - serverless-dynamodb-local
 - serverless-offline-python

provider:
  name: aws
  runtime: python3.6
  region: us-west-2


custom:
  s3:
    port: 8090
    directory: s3-simulated-buckets
    cors: false


resources:
  Resources:
    stage-one:
      Type: 'AWS::DynamoDB::Table'
      Properties:
        AttributeDefinitions:
          -
            AttributeName: import_id
            AttributeType: S
        KeySchema:
          -
            AttributeName: import_id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: stage-one

    ImportBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: import


functions:
  import-processor-stage-1:
    handler: import_processor_stage_1.process_file_import
    events:
      - existingS3:
          bucket: import
          events:
            - s3:ObjectCreated:*

Any and all help is appreciated! Thank you for taking a look!


Can confirm that this works for a js environment.

1

There are 1 best solutions below

0
On BEST ANSWER

I ended up customizing the serverless-s3-local library to be able to call .py files. I may end up creating a new github repo and node package for this customization, but for now I will refer users to said customizations that I described here.