如何在Golang的AWS Lambda中支持多个触发器? [英] How to support more than one trigger in AWS Lambda in Golang?

查看:141
本文介绍了如何在Golang的AWS Lambda中支持多个触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Golang中构建一个AWS Lambda函数,该函数将内容从n个复制到m个S3存储桶.需要支持S3触发以及从存储所有源S3存储桶更改的SQS提取数据.可以在这里找到代码: https://github.com/maknahar/s3copy

I am building an AWS Lambda function in Golang that copy the content from n to m S3 buckets. There is a requirement to support for S3 trigger as well as fetching the data from an SQS where all source S3 bucket change is stored. The code can be found here: https://github.com/maknahar/s3copy

我尝试了以下操作:

func main() {
    lambda.Start(ProcessIncomingS3Events)
    lambda.Start(ProcessIncomingEvents)
}

func ProcessIncomingS3Events(event events.S3Event) error {
    ...
    log.Println("Got S3 Event")
    return processS3Trigger(config, event)
}

func ProcessIncomingEvents() error {
    ...
    log.Println("Defaulting to SQS")
    return processSQSMessage(config)
}

在这种情况下,每次都会触发第一个事件ProcessIncomingS3Events.

In this case, the first event ProcessIncomingS3Events is triggered every time.

我也尝试关注

func main() {
    lambda.Start(ProcessIncomingEvents)
}

func ProcessIncomingEvents(event interface{}) error {
    ...
    switch request := event.(type) {
    case events.S3Event:
        log.Println("Got S3 Event")
        return processS3Trigger(config, request)

    case types.Nil:
        log.Println("Defaulting to SQS")
        return processSQSMessage(config)

    default:
        log.Println("Could not find the event type")

    }

    return nil
}

在这种情况下,Lambda无法检测到类型,并且在每个触发器中都记录了Could not find the event type.

In this case, Lambda could not detect the type and Could not find the event type is logged in every trigger.

该功能是否完全可以通过AWS开发工具包支持多个触发器?

Is there a way to support multiple triggers via AWS SDK at all for the function?

推荐答案

我通过实现aws Handler接口实现了监听多个事件的方法,它定义了一个方法

I achieved to listen to multiple events by implementing the aws Handler interface, it defines one method

Invoke(ctx context.Context, payload []byte) ([]byte, error)

我实现了如下的多事件处理程序

I implemented a multievent Handler as follows

type Handler struct {
//add global variables or context information that your handler may need
}

func (h Handler) Invoke(ctx context.Context, data []byte) ([]byte, error) {
  //for demonstration purposes, not the best way to handle
  apiGatewayEvent := new(events.APIGatewayProxyRequest)
  if err := json.Unmarshal(data, apiGatewayEvent); err != nil {
    log.Println("Not a api gateway event")
  }
  snsEvent := new(events.SNSEvent)
  if err := json.Unmarshal(data, snsEvent); err != nil {
    log.Println("Not a sns event")
  }
  return nil, nil
}

func main() {
  lambda.StartHandler(Handler{})
}

如您所见,您可以获取任何事件的原始字节并根据需要进行处理,从而使您可以监听具有相同lambda的任何aws事件.但是,请谨慎使用此方法,因为如上所述,最好使用lambda来处理仅一种类型的事件

As you can see, you could get the raw bytes of any event and handle them as you need giving you the possibility to listen to any aws event with the same lambda. However, think carefully before using this aproach, because, as noted above, lambdas are best used handling just one type of event

希望这会有所帮助.

这篇关于如何在Golang的AWS Lambda中支持多个触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆