Your first Go Lambda¶
Start with structured logs. This handler accepts a name, returns a greeting and writes one searchable JSON record.
Install¶
Use Go 1.27 or newer and initialize your application:
go mod init example.com/hello
CGO_ENABLED=0 go get github.com/rambow-cloud/powertools-lambda-go/logger@v1.2.0 github.com/aws/aws-lambda-go@v1.55.0
Write the handler¶
Save this as main.go:
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"github.com/rambow-cloud/powertools-lambda-go/logger"
)
type Event struct {
Name string `json:"name"`
}
type Response struct {
Message string `json:"message"`
}
func main() {
appLog := logger.New(logger.WithServiceName("hello"))
handler := func(ctx context.Context, event Event) (Response, error) {
if event.Name == "" {
return Response{}, fmt.Errorf("name is required")
}
requestLog := appLog.WithContext(ctx)
if err := requestLog.Info("Handling request", logger.Fields{"name": event.Name}); err != nil {
return Response{}, fmt.Errorf("write request log: %w", err)
}
return Response{Message: "Hello, " + event.Name}, nil
}
lambda.Start(logger.WrapHandler(appLog, handler))
}
Create the logger once; obtain a request-bound logger inside the wrapped handler.
Invoke it¶
Input
Response
Log fields (excerpt; timestamp and Lambda request fields vary)
An empty name returns name is required. The logger does not turn a business error into a successful handler response.
Build for Lambda¶
Use amd64 for Lambda x86_64. Package the executable as bootstrap with executable permissions and configure the function for provided.al2023 and the matching architecture. See packaging notes for repository helpers.
Add the utilities you need¶
| Need | Next guide |
|---|---|
| Log fields, levels and incoming events | Logger |
| Handler and outbound-call spans | Tracer |
| Custom CloudWatch metrics | Metrics |
| JSON HTTP routes | HTTP |
| More modules | Installation |
Tracing is optional. Configure a reachable OpenTelemetry collector before enabling export; use its awsxray exporter for X-Ray. The legacy X-Ray SDK adapter is deprecated and frozen.
Things to know¶
- Keep
CGO_ENABLED=0for builds and tests. - Use explicit
jsontags on event/response structs. The project uses JSON v2 behavior; see JSON notes. - Follow usage patterns when composing wrappers and sharing clients across invocations.