Created
September 27, 2025 05:26
-
-
Save kenjij/62d027ff2224dc8ef1fbefa1a6bae982 to your computer and use it in GitHub Desktop.
AWS Lambda function basic template in Go (golang)
This file contains hidden or 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 | |
| import ( | |
| "context" | |
| "encoding/json" | |
| "errors" | |
| "fmt" | |
| "github.com/aws/aws-lambda-go/lambda" | |
| "log/slog" | |
| "os" | |
| "strings" | |
| ) | |
| func initSimpleLoggerWithLevel(lvl string) { | |
| var slogLvl slog.Level | |
| switch strings.ToLower(lvl) { | |
| case "debug": | |
| slogLvl = slog.LevelDebug | |
| case "info": | |
| slogLvl = slog.LevelInfo | |
| case "warn": | |
| slogLvl = slog.LevelWarn | |
| default: | |
| slogLvl = slog.LevelError | |
| } | |
| opts := &slog.HandlerOptions{ | |
| Level: slogLvl, | |
| ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { | |
| if a.Key == slog.TimeKey { | |
| return slog.Attr{} | |
| } | |
| return a | |
| }, | |
| } | |
| slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, opts))) | |
| } | |
| func handleRequest(ctx context.Context, event json.RawMessage) error { | |
| slog.Debug("Dump...", "ctx", fmt.Sprintf("%v", ctx)) | |
| var data map[string]any | |
| err := json.Unmarshal(event, &data) | |
| if err != nil { | |
| slog.Error(fmt.Sprintf("Error unmarshaling JSON: %v", err)) | |
| return errors.New("Unable to process inbound event") | |
| } | |
| slog.Debug("Dump...", "event", fmt.Sprintf("%v", data)) | |
| return nil | |
| } | |
| func main() { | |
| initSimpleLoggerWithLevel(os.Getenv("APP_LOG_LEVEL")) | |
| lambda.Start(handleRequest) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment