drqn5418 2018-03-27 15:09
浏览 98

如何在Golang的AWS Lambda中支持多个触发器?

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

I tried following:

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)
}

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

I tried following as well

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
}

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

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

  • 写回答

3条回答 默认 最新

  • douao3636 2018-03-27 16:08
    关注

    I'm not a GoLang guy. Just guessing on normal programming thinking.

    In approach one, you are directly calling ProcessIncomingS3Events in first statement, so every time this is called.

    Read this - Lambda Function Handler (Go)

    In above link, the author is parsing event's name field. Similarly, you can check for any field which is always present in S3 event e.g. "eventSource":"aws:s3" (S3 event structure see here)

    If present then S3 event else other. Or you can also check for SQS event's field.

    HIH

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题