[英]NestJs Eventbridge Lambda function
I have a system writed in using NestJs and serverless framework were each endpoint is a lambda function on aws.我有一个使用 NestJs 和无服务器框架编写的系统,每个端点都是 aws 上的 lambda function。 One of the functions is not an endpoint, but a trigger from AWS eventbridge.其中一个功能不是端点,而是来自 AWS eventbridge 的触发器。 As this function is not an endpoint it cannot be included on a NestJs module since it have to be exported separatelly.由于此 function 不是端点,因此不能包含在 NestJs 模块中,因为它必须单独导出。 My problem is that when the event comes to Eventbridge and triggers the lambda I have to call a NestJs service but I'm not able to do this, since the lambda function is outside NestJs environment.我的问题是,当事件到达 Eventbridge 并触发 lambda 时,我必须调用 NestJs 服务,但我无法执行此操作,因为 lambda function 在 NestJs 环境之外。 Is that a way for me to call a NestJs service from outside the module?这是我从模块外部调用 NestJs 服务的一种方式吗?
Here is the serverless framework configs这是无服务器框架配置
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
Currently I'm using axios to call another NestJs endpoint to just pass the received payload.目前我正在使用 axios 调用另一个 NestJs 端点来传递接收到的有效负载。 As you can see on the lambda function file:正如您在 lambda function 文件中所见:
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
})
}
Here is the controller of lambda function 1这是lambda的controller function 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)
}
}
and here is the service这是服务
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)
}
}
The question is: Is that a way to call all this NestJs structure from the function file, since it is outside NestJs modules and since the trigger for this function is not an http request but a trigger from Eventbridge?问题是:这是从 function 文件调用所有 NestJs 结构的方法吗,因为它在 NestJs 模块之外,因为这个 function 的触发器不是 http 请求而是来自 Eventbridge 的触发器? Thank you so much.太感谢了。
You can use a "Standalone" Nest application and pass the event data directly to MyService
您可以使用“独立”Nest 应用程序并将事件数据直接传递给MyService
You can use NEstJs standalone app, and make your handler like this export const checkDeletion: Handler = async (event: any, context: Context) => {您可以使用 NEstJs 独立应用程序,并使您的处理程序像这样 export const checkDeletion: Handler = async (event: any, context: Context) => {
async function bootstrap() {
const app = await NestFactory.createApplicationContext(AppModule);
await app
.select(SchedulerModule)
.get(SchedulerService, { strict: true })
.runScheduler();
}
await bootstrap();
};
After that call your handler from serverless.yaml like之后从 serverless.yaml 调用你的处理程序
functions:
followup-emails:
environment:
STAGE: ${opt:stage}
name: followup-emails-${opt:stage}
handler: src/lambda.checkDeletion
events:
- schedule: rate(1 day)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.