{"id":16879643,"url":"https://github.com/easycz/aws-lambda-middleware","last_synced_at":"2026-05-17T22:37:01.537Z","repository":{"id":57577454,"uuid":"359717299","full_name":"easyCZ/aws-lambda-middleware","owner":"easyCZ","description":"Helpers to enable middewares on AWS Lambda.","archived":false,"fork":false,"pushed_at":"2021-04-20T08:47:39.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-25T06:41:21.108Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/easyCZ.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-04-20T07:04:37.000Z","updated_at":"2021-04-20T10:49:03.000Z","dependencies_parsed_at":"2022-09-11T22:50:46.277Z","dependency_job_id":null,"html_url":"https://github.com/easyCZ/aws-lambda-middleware","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easyCZ%2Faws-lambda-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easyCZ%2Faws-lambda-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easyCZ%2Faws-lambda-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easyCZ%2Faws-lambda-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/easyCZ","download_url":"https://codeload.github.com/easyCZ/aws-lambda-middleware/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244554124,"owners_count":20471173,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T15:55:11.282Z","updated_at":"2026-05-17T22:37:01.508Z","avatar_url":"https://github.com/easyCZ.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# aws-lambda-middleware\nThis Golang package provides helpers to construct AWS Lambda middlewares. It works very similarly to HTTP Middlewares you would find on your favourite HTTP server library.\n\n## Features\n* Only use the AWS Lambda SDK interfaces, no reflection, no type casting\n* Composable, you can compose Middlewares together into a Middleware Chain\n\n## Usage\n```bash\ngo get -u github.com/easyCZ/aws-lambda-middleware\n```\n\nFull example available in [./middleware_example_test.go](./middleware_example_test.go).\n```golang\nimport (\n    \"context\"\n    \"log\"\n    \n    \"github.com/easyCZ/aws-lambda-middleware\"\n    \n    \"github.com/aws/aws-lambda-go/lambda\"\n    \"github.com/aws/aws-lambda-go/lambdacontext\"\n)\n\ntype HandlerPayload struct {\n    Value string `json:\"value\"`\n}\n\n\nfunc ExampleMiddleware() {\n\tlogger := log.Default()\n\tmyLambdaHandler := func(ctx context.Context, payload HandlerPayload) error {\n\t\tlogger.Printf(\"Handling lambda invocation for payload: %v\\n\", payload)\n\n\t\treturn nil\n\t}\n\n\thandler := lambda.NewHandler(myLambdaHandler)\n\thandler = lambdamiddleware.Chain(handler, []lambdamiddleware.Middleware{\n\t\tLoggingMiddleware(logger),\n\t\tContextExtendMiddleware,\n\t}...)\n\n\t// start your lambda\n\tlambda.StartHandler(handler)\n}\n\n// LoggingMiddleware is a constructor, it allows us to inject dependencies into this middleware.\nfunc LoggingMiddleware(logger *log.Logger) lambdamiddleware.Middleware {\n\t// We return a Middleware which has access to our logger in a closure\n\treturn func(next lambda.Handler) lambda.Handler {\n\n\t\t// Middleware must return a handler. We use the HandlerFunc helper to write a handler as a single function.\n\t\treturn lambdamiddleware.HandlerFunc(func(ctx context.Context, bytes []byte) ([]byte, error) {\n\t\t\t// Extract invocation information from the context. This is populated from the lambda runtime and the internal lambda.Handler implementation.\n\t\t\tlctx, _ := lambdacontext.FromContext(ctx)\n\n\t\t\t// Print the request body, and the request ID.\n\t\t\t// You may want to attach this as structured fields to your *smart* logger.\n\t\t\tlogger.Printf(\"Handing lambda invocation. Request ID: %s. Body: %s\\n\", lctx.AwsRequestID, string(bytes))\n\n\t\t\t// Trigger the next middleware in the chain. This may be the actual handler, or another Middleware.\n\t\t\tresponse, err := next.Invoke(ctx, bytes)\n\n\t\t\t// We have access to the response, we can choose to log it.\n\t\t\tif err != nil {\n\t\t\t\tlogger.Printf(\"Finished lambda invocation with error: %s. Request ID: %s. Response: %s.\", err.Error(), lctx.AwsRequestID, string(response))\n\t\t\t} else {\n\t\t\t\tlogger.Printf(\"Finished lambda invocation with OK. Request ID: %s. Response: %s.\", lctx.AwsRequestID, string(response))\n\t\t\t}\n\n\t\t\t// We return the response and the error back to the Middleware before us, or to the AWS Runtime.\n\t\t\treturn response, err\n\t\t})\n\t}\n}\n\n// ContextExtendMiddleware is a middleware which extends the context with a value\n// ContextExtendMiddleware is an example where we do not use a constructor to contruct the middleware, instead we implement the Middleware type directly.\nfunc ContextExtendMiddleware(next lambda.Handler) lambda.Handler {\n\t// We return a HandlerFunc to get access to the request context.\n\treturn lambdamiddleware.HandlerFunc(func(ctx context.Context, bytes []byte) ([]byte, error) {\n\t\t// Attach a custom value to the context.\n\t\tctx = context.WithValue(ctx, \"my_key\", \"my_value\")\n\n\t\t// Directly invoke the next handler, without capturing the response and error - we do not need them in this case.\n\t\treturn next.Invoke(ctx, bytes)\n\t})\n}\n\n```\n\n## Contributions\nContributions welcome. Please raise a Pull Request.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feasycz%2Faws-lambda-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feasycz%2Faws-lambda-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feasycz%2Faws-lambda-middleware/lists"}