LoginSignup
1
2

More than 3 years have passed since last update.

NestJSで特定のサービスのメソッドを直接実行する

Last updated at Posted at 2020-03-19

NestJSをLambdaでホストしているとき、特定のサービスのメソッドをダイレクトに実行したい場合があります。

例:cronジョブをCloudWatch Event -> Lambdaで実行する

そういう時はこんな感じのhandlerを作ればOKです。

import {
    NestFactory
} from '@nestjs/core'
import { Handler } from 'aws-lambda'

// 実行したいサービスのあるモジュール
import {
    SiteModule
} from './site.module'

// 実行したいサービス
import { 
    SiteService
} from './site.service'

export const handler: Handler = async (event) => {
    // Create NestJS application
    const app = await NestFactory.create(SiteModule)

    // Resolve Dependencies
    const service = await app.resolve(SiteService)

    // Execute method
    const result = await service.listSite(event.username)
    return result
  };

NestJSをLambdaでってどうなの?

パフォーマンス期待するならやめた方がいいと思います。
Fargateとかその辺覚える労力より先にNestJS使いこなせるようになりたい目的でLambdaに載せてるだけなので・・・

ただ、この記事のような形で、すでに動かしているNestJSアプリケーションの実装を使ったバッチ処理をLambdaにやらせるのはありかもしれません。
ジョブのリトライとか通知とかその辺りの便利機能がLambdaにはありますし、バッチ系なら多少立ち上がりが遅くても問題ないですし。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2