无服务器离线:运行 处理程序出错,找不到模块

Serverless Offline: Error while running handler, cannot find module

我一直在尝试使用 serverless-offline / serverless-offline-python 和一系列其他节点包(例如 serverless-s3-local )为我的一些 lambda 构建离线测试环境写了。

使用 serverless-offline-python,我能够启动我的 python lambda 并使用以下命令手动调用每个函数:

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

这可行,但不能完全模拟 lambda 实际执行的操作。理想情况下,我会模拟一个导入了新对象的 S3 存储桶,该操作将成为 lambda (import-processor-stage-1) 的催化剂。

Enter serverless-s3-local.

我已经使用插件进行了非常棒的配置,但是由于某些原因,当 serverless-s3-local 去触发一个事件到我的 lambda 处理程序时,我收到这个错误:

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' }

遗憾的是,我知道这些插件还处于初期阶段(尤其是 serverless-offline-python),但我很确定这应该是一个非常容易解决的问题。没有找到的模块实际上存在于包中!我认为这是因为它是一个 Python 文件而不是 JS 文件。 js 脚本中有没有办法接受 python 文件?到目前为止,我已经尝试在 serverless-s3-local 代码库中用 serverless-offline-python 替换 serverless-offline 的所有引用,但我得到了同样的错误。

作为参考,这是我的 serverless.yml 文件(作为记录,处理程序文件可用,位置正确并且在手动调用时工作正常):

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:*

感谢任何帮助!感谢您的观看!


可以确认这适用于 js 环境。

我最终自定义了 serverless-s3-local 库,以便能够调用 .py 文件。我最终可能会为此自定义创建一个新的 github 存储库和节点包,但现在我将向用户推荐我描述的自定义 here.