serverless framework+TypeScriptでhandlerごとにファイルを分割する
serverless-plugin-typescriptを使っているのが前提です。
以下のようなディレクトリ構造を考える。
code:bash
src
├── handlers
│ ├── hoge.ts
│ ├── fuga.ts
│ └── index.ts
...
handlerそれぞれは以下のように実装されている。
handlers/hoge.ts
code:typescript
export async function hoge(): Promise<void> {
// something logic
}
handlers/index.ts
code:typescript
export * from './hoge';
export * from './fuga';
serverless.yaml
code:yaml
plugins:
- serverless-plugin-typescript
package:
individually: true
include:
- src/handlers/**
functions:
hoge:
handler: src/handlers/index.hoge
events:
- schedule:
rate: rate(1 minute)
enabled: true
fuga:
handler: src/handlers/index.fuga
events:
- schedule:
rate: rate(1 minute)
enabled: true
plugins
package
任意のディレクトリにあるhandlerを指定するには以下のオプションが必要
individually
true
include
handlerを配置するパス
functions
{name}.handler の指定がちょっと特殊