我一直在尝试使用serverless-offline
/serverless-offline-python
和一系列其他节点包(serverless-s3-local
例如)为我编写的一些 lambdas 构建离线测试环境。
使用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
) 的催化剂。
我已经用插件配置了一个非常棒的配置,但是由于某种原因,当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 环境。