How to support more than one trigger in AWS Lambda in Golang?

两盒软妹~` 提交于 2019-11-30 21:27:54

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

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

Hope this helps.

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

You can configure multiple event sources to trigger one or more Lambda functions.

However, in Go the lambda.Start call is blocking so it's not very easy to write a single function that handles multiple event types. You are strongly encouraged to make a separate Lambda function for every event source.

The idiomatic Go solution is to have your function logic defined once in the main package, and write multiple programs that take the source event and call your function. So the project layout would be:

  • s3copy/main.go
  • s3copy/handlers/s3/main.go
  • s3copy/handlers/sqs/main.go

See my boilerplate Lambda and Go app for an example project layout and Makefile.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!