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?
Your function can have multiple triggers. Each trigger acts as a client invoking your function independently. Each event that Lambda passes to your function has data from only one client or trigger.
Yes a lambda functions can have event sources of different types but you must use the com. amazonaws. services.
If you invoke the function again while the first event is being processed, Lambda initializes another instance, and the function processes the two events concurrently. As more events come in, Lambda routes them to available instances and creates new instances as needed.
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:
See my boilerplate Lambda and Go app for an example project layout and Makefile.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With