0

I have a system writed in using NestJs and serverless framework were each endpoint is a lambda function on aws. One of the functions is not an endpoint, but a trigger from AWS eventbridge. As this function is not an endpoint it cannot be included on a NestJs module since it have to be exported separatelly. 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. Is that a way for me to call a NestJs service from outside the module?

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. As you can see on the lambda function file:

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

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? Thank you so much.

2 Answers2

0

You can use a "Standalone" Nest application and pass the event data directly to MyService

er8h
  • 136
  • 1
  • 3
0

You can use NEstJs standalone app, and make your handler like this 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

    functions:
  followup-emails:
    environment:
      STAGE: ${opt:stage}
    name: followup-emails-${opt:stage}
    handler: src/lambda.checkDeletion
    events:
      - schedule: rate(1 day)