Created
January 15, 2023 16:20
-
-
Save clarkritchie/8fe7eaacf8d3e32c336bf8df922fd8a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// Simple example of a lambda SQS processor | |
import ( | |
"context" | |
"github.com/aws/aws-lambda-go/events" | |
"github.com/aws/aws-lambda-go/lambda" | |
"github.com/aws/aws-sdk-go-v2/service/cloudwatchevents" | |
"github.com/rs/zerolog" | |
// import awsutils here | |
) | |
var logger *zerolog.Logger | |
// example of this BatchItemFailures pattern: https://issuehint.com/issue/aws/aws-lambda-go/430 | |
func handler(ctx context.Context, sqsEvent events.SQSEvent) (events.SQSEventResponse, error) { | |
// awsutils being a ustom package that reutrns a pointer to the logger | |
logger = awsutils.SetupJsonLogging(ctx, awsutils.DebugLevel) | |
cfg := awsutils.SetupEnvironment(ctx) | |
failedMessageIds := events.SQSEventResponse{} | |
for _, message := range sqsEvent.Records { | |
// processSqsEvent is elsewhere, such as in process.go | |
err := processSqsEvent(ctx, message.Body) | |
if err != nil { | |
logger.Error().Msg(err.Error()) | |
failedMessageIds.BatchItemFailures = append(failedMessageIds.BatchItemFailures, events.SQSBatchItemFailure{ | |
ItemIdentifier: message.MessageId, | |
}) | |
} else { | |
logger.Debug().Str("MessageId", message.MessageId).Msg("the event processed successfully") | |
} | |
} | |
return failedMessageIds, nil | |
} | |
func main() { | |
lambda.Start(handler) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment