Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!