3
2

More than 3 years have passed since last update.

Serverless Framework lambda 1プロジェクトで複数関数を定義したい

Posted at

Serverless Frameworkを使って1プロジェクト内にlambdaの複数関数を用意するには、どうしたらいいの?(TypeScriptで)ということで、簡単にserverless.ymlとプロジェクトの構成を解説していこうかなと思います。

プロジェクト構成

今回はhandler.tsとhandler2.tsにそれぞれ一つづつ関数を定義しています。
スクリーンショット 2020-03-01 21.59.01.png

serverless.yml

lambdaの関数を複数定義したい場合は、serverless.ymlのfunctionを複数書けばいいだけ。
すごく簡単。

functions:
  hello:
    handler: handler.helloWorld
    events:
      - http:
          method: get
          path: hello
  hello2:
    handler: handler2.helloWorld2
    events:
      - http:
          method: get
          path: hello2

今回は、helloとhello2がそれに当たります。
そこで、それぞれのhandlerの関数を指定すればいいだけです。

デプロイ後

こんな感じで複数関数ができてます。
スクリーンショット 2020-03-01 22.02.51.png

まとめ

ServerlessFrameworkすごく簡単。
関数ごとにプロジェクトを分ける必要がない。

3
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
3
2