如何解决NestJs Eventbridge Lambda 函数
我使用 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 的触发器?非常感谢。
解决方法
您可以使用 "Standalone" Nest application 并将事件数据直接传递给 MyService
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。