我有一个使用NestJs和无服务器框架编写的系统,每个端点都是aws上的一个lambda函数。其中一个功能不是端点,而是来自AWS eventbridge的触发器。由于此函数不是端点,因此不能将其包含在NestJs模块中,因为它必须单独导出。我的问题是,当事件到达Eventbridge并触发lambda时,我必须调用NestJs服务,但我无法这样做,因为lambda函数在NestJs环境之外。这是我从模块外部调用NestJs服务的一种方式吗?
下面是无服务器框架的配置
functions:
function 1(NestJs controller):
handler: src/lambda.handler
events:
- http:
cors: true
method: post
path: entrypoint for function 1
Function 2 (External from NestJs modules):
handler: path to lambda function
events:
- eventBridge:
eventBus: eventbus name
pattern:
source:
- source目前,我正在使用axios调用另一个NestJs端点来传递接收到的有效负载。正如您在lambda函数文件中看到的:
import { Context, Handler } from 'aws-lambda'
import axios from 'axios'
export const handler: Handler = async (event: any, context: Context) => {
return await axios
.post(
'lambda function production url',
event.detail
)
.then((data) => {
console.log('data', data)
return data
})
.catch((error) => {
console.log('error', error)
return error
})
}下面是lambda函数1的控制器
import { Body, Controller, Post } from '@nestjs/common'
import { MyService } from './enrichment.service'
@Controller('function1')
export class EnrichmentController {
constructor(private readonly myService: MyService) {}
@Post('entrypoint')
sendForm(@Body() body) {
return this.myService.start(body)
}
}这就是服务
import { forwardRef, Inject, Injectable } from '@nestjs/common'
import { EventbridgeService } from '../eventbridge/eventbridge.service'
import { CampaignsService } from '../campaigns/campaigns.service'
import { UploadedDataService } from '../uploaded-data/uploaded-data.service'
@Injectable()
export class MyService {
constructor(
private readonly anotherService: AnotherService,
) {}
async start(body) {
return this.anotherService.updateData(body)
}
}问题是:这是一种从函数文件调用所有NestJs结构的方法吗,因为它在NestJs模块之外,并且该函数的触发器不是http请求,而是来自Eventbridge的触发器?非常感谢。
发布于 2021-06-17 20:19:39
您可以使用"Standalone" Nest application并将事件数据直接传递给MyService
https://stackoverflow.com/questions/68018892
复制相似问题