Reusable Lambda for Multiple Event Types (Java)

0

I want to write an AWS Lambda function that is able to respond to various types of event types: API Gateway, Kinesis, S3, etc. The API/SDK I need to work with is Java. I'd like to create a general-purpose handler, but it appears that each service has its own event type and does not derive from some common parent event type. Is there a pattern or a best practice for creating an AWS Lambda function that can be used with a variety of event types? Or do I need to create a custom handler for each event type?

Brian
質問済み 1年前1820ビュー
1回答
0

Hi Brian,

One approach is to try to find a common “parent” to all those event types. As everything is an object in Java, you could argue that your handler could accept Object type as the event.

The drawback is that then you ll have to cast and use instanceof operator, not ideal but it will work.

Another approach is to use Map and/or Inputstream as shown here: https://docs.aws.amazon.com/lambda/latest/dg/java-handler.html.

Hope it helps

profile picture
エキスパート
回答済み 1年前
  • Using Object and instanceof does work, but isn’t really a lot different than having a handler per event type. The issue with the Map or InputStream is that the event is serialized and I have essentially the same problem as before in that each event has a different format. :(

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ