无服务器脱机:运行处理程序时出错,无法找到模块

时间:2018-11-20 17:41:34

标签: python serverless-framework serverless-offline

我一直在尝试使用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-offline代码库中将serverless-offline-python的所有引用替换为serverless-s3-local,但是遇到相同的错误。

作为参考,这是我的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环境。

1 个答案:

答案 0 :(得分:1)

我最终定制了serverless-s3-local库以能够调用.py文件。我可能最终会为此自定义创建一个新的github repo和node包,但是现在我将引导用户参考我所描述的here的自定义。