如何在 Go 中创建 AWS Lambda 来处理多个事件

use*_*325 2 go aws-lambda

我需要实施 AWS Lambda 处理程序来处理 AWS S3Events 和 SNSEvent,有什么解决方案吗?

我已经检查过这个答案,How to support more one trigger in AWS Lambda in Golang?

但这对我不起作用。

Pey*_*man 5

根据此文档,您可以处理您的自定义事件。因此您可以创建包含 S3Entity 和 SNSEntity 的自定义事件

type Record struct {
   EventVersion         string           `json:"EventVersion"`
   EventSubscriptionArn string           `json:"EventSubscriptionArn"`
   EventSource          string           `json:"EventSource"`
   SNS                  events.SNSEntity `json:"Sns"`
   S3                   events.S3Entity  `json:"s3"`
}

type Event struct {
    Records []Record `json:"Records"`
}
Run Code Online (Sandbox Code Playgroud)

然后检查事件源

func handler(event Event) error {
   if len(event.Records) > 0 {
    if event.Records[0].EventSource == "aws:sns" {
       //Do Something
    } else {
       //Do Something
    }
  }

  return nil
}
Run Code Online (Sandbox Code Playgroud)