2

I need to implement the AWS Lambda handler to handle AWS S3Events & SNSEvent, any solution?

Already I checked this answer, How to support more than one trigger in AWS Lambda in Golang?

But that's doesn't work for me.

user885325
  • 35
  • 6

2 Answers2

4

According to this document you could hanlde your custom event. So you can create custom event which includes S3Entity and 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"`
}

Then check the EventSource

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

  return nil
}
Peyman
  • 3,068
  • 1
  • 18
  • 32
0

You can use embedding in Go to solve this

I answer a similar question here

import (
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
    "reflect"
)

type Event struct {
    events.SQSEvent
    events.APIGatewayProxyRequest
    //other event type
}

type Response struct {
    events.SQSEventResponse `json:",omitempty"`
    events.APIGatewayProxyResponse `json:",omitempty"`
   //other response type
}

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

func eventRouter(event Event) (Response, error) {
    var response Response
    switch {
    case reflect.DeepEqual(event.APIGatewayProxyRequest, events.APIGatewayProxyRequest{}):
        response.SQSEventResponse = sqsEventHandler(event.SQSEvent)
    case reflect.DeepEqual(event.SQSEvent, events.SQSEvent{}):
        response.APIGatewayProxyResponse = apiGatewayEventHandler(event.APIGatewayProxyRequest)
  //another case for a event handler
    }
    return response, nil
}


func sqsEventHandler(sqsEvent events.SQSEvent) events.SQSEventResponse {
    //do something with the SQS event 
}

func apiGatewayEventHandler(apiEvent events.APIGatewayProxyRequest) events.APIGatewayProxyResponse {
    //do something with the API Gateway event
}

Note: if the basic events have some same field names, you will need to look for another the compare method instance of DeepEqual.

Note2: sorry for my english, greetings from Argentina

Agu-GC
  • 11
  • 3