https://github.com/wolfeidau/lambda-go-extras
This module provides a middleware layer for github.com/aws/aws-lambda-go.
https://github.com/wolfeidau/lambda-go-extras
aws aws-lambda golang middleware
Last synced: about 1 year ago
JSON representation
This module provides a middleware layer for github.com/aws/aws-lambda-go.
- Host: GitHub
- URL: https://github.com/wolfeidau/lambda-go-extras
- Owner: wolfeidau
- License: apache-2.0
- Created: 2020-04-19T09:58:00.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-01-03T22:59:01.000Z (over 2 years ago)
- Last Synced: 2025-04-01T01:21:26.756Z (over 1 year ago)
- Topics: aws, aws-lambda, golang, middleware
- Language: Go
- Homepage: https://github.com/wolfeidau/lambda-go-extras
- Size: 74.2 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lambda-go-extras
This module provides a middleware layer for [github.com/aws/aws-lambda-go](https://github.com/aws/aws-lambda-go). This project is heavily based on https://github.com/justinas/alice.
[](https://github.com/wolfeidau/lambda-go-extras/actions?query=workflow%3AGo)
[](https://goreportcard.com/report/github.com/wolfeidau/lambda-go-extras)
[](https://godoc.org/github.com/wolfeidau/lambda-go-extras)
# Why?
Having used the `github.com/aws/aws-lambda-go` package for a while now I have found it annoying switching between the Go standard libraries `http` package and this library. After some review I think the thing I miss the most is the ability to chain a list of handlers, with each link responsible for a part of the puzzle.
Being able to compose these chains offers a lot of flexibility and reuse across projects.
Given the default way of using the `github.com/aws/aws-lambda-go` is via the [Start(handler interface{})](https://godoc.org/github.com/aws/aws-lambda-go/lambda#Start) function, which is very flexible, but not easily extended due it's dynamic nature. So I have moved to using the less used [func StartHandler(handler Handler)](https://godoc.org/github.com/aws/aws-lambda-go/lambda#StartHandler) which is more idiomatic with its `Handler` being just an interface with a single `Invoke` method. This module has extended this with a simple middleware chain inspired by alice as mentioned above.
# Usage
This illustrates how easy it is to dump the input and output payloads using a simple middleware chain.
```go
package main
import (
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/rs/zerolog"
"github.com/wolfeidau/lambda-go-extras/lambdaextras"
lmw "github.com/wolfeidau/lambda-go-extras/middleware"
"github.com/wolfeidau/lambda-go-extras/middleware/raw"
zlog "github.com/wolfeidau/lambda-go-extras/middleware/zerolog"
)
var (
commit = "unknown"
buildDate = "unknown"
)
func main() {
flds := lmw.FieldMap{"commit": commit, "buildDate": buildDate}
ch := lmw.New(
raw.New(raw.Fields(flds)), // raw event logger which prints input and output of handler
zlog.New(zlog.Fields(flds)), // inject zerolog into the context
).Then(lambdaextras.GenericHandler(processSQSEvent))
// use StartWithOptions as StartHandler is deprecated
lambda.StartWithOptions(ch)
}
func processSQSEvent(ctx context.Context, sqsEvent events.SQSEvent) (string, error) {
zerolog.Ctx(ctx).Info().Msg("sqsEvent")
return "ok", nil
}
```
There are two bundled middleware, these are
* `github.com/wolfeidau/lambda-go-extras/middleware/raw` - raw event logger which outputs input and output events which also includes `aws_request_id` and the fields you provide at initialisation.
* `github.com/wolfeidau/lambda-go-extras/middleware/zerolog` - configures the context with a [zerolog](https://github.com/rs/zerolog) logger which includes `aws_request_id` and the fields you provide at initialisation.
# License
This code was authored by [Mark Wolfe](https://www.wolfe.id.au) and is licensed under the [Apache 2.0 license](http://www.apache.org/licenses/LICENSE-2.0).