{"id":13830295,"url":"https://github.com/jbleduigou/slog-aws-lambda","last_synced_at":"2026-01-12T08:02:35.012Z","repository":{"id":228442928,"uuid":"774018559","full_name":"jbleduigou/slog-aws-lambda","owner":"jbleduigou","description":"An AWS Lambda Function Handler for slog Go library.","archived":false,"fork":false,"pushed_at":"2025-01-26T07:23:34.000Z","size":25,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-26T08:22:51.886Z","etag":null,"topics":["aws","go","logging","serverless"],"latest_commit_sha":null,"homepage":"http://www.jbleduigou.com","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/jbleduigou.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-18T19:56:24.000Z","updated_at":"2025-01-26T07:21:50.000Z","dependencies_parsed_at":"2024-03-18T21:23:16.466Z","dependency_job_id":"33adf64a-5f0a-4225-865f-109457b65b54","html_url":"https://github.com/jbleduigou/slog-aws-lambda","commit_stats":null,"previous_names":["jbleduigou/slog-aws-lambda"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/jbleduigou/slog-aws-lambda","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbleduigou%2Fslog-aws-lambda","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbleduigou%2Fslog-aws-lambda/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbleduigou%2Fslog-aws-lambda/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbleduigou%2Fslog-aws-lambda/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jbleduigou","download_url":"https://codeload.github.com/jbleduigou/slog-aws-lambda/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbleduigou%2Fslog-aws-lambda/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28336980,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T06:09:07.588Z","status":"ssl_error","status_checked_at":"2026-01-12T06:05:18.301Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["aws","go","logging","serverless"],"created_at":"2024-08-04T10:00:58.351Z","updated_at":"2026-01-12T08:02:34.998Z","avatar_url":"https://github.com/jbleduigou.png","language":"Go","funding_links":["https://github.com/sponsors/jbleduigou"],"categories":["Enrichment"],"sub_categories":[],"readme":"# slog: AWS Lambda handler\n\n![Go Version](https://img.shields.io/badge/Go-%3E%3D%201.21-%23007d9c)\n[![GoDoc](https://godoc.org/github.com/jbleduigou/slog-aws-lambda?status.svg)](https://pkg.go.dev/github.com/jbleduigou/slog-aws-lambda)\n![Build Status](https://github.com/jbleduigou/slog-aws-lambda/actions/workflows/go.yml/badge.svg)\n[![Go report](https://goreportcard.com/badge/github.com/jbleduigou/slog-aws-lambda)](https://goreportcard.com/report/github.com/jbleduigou/slog-aws-lambda)\n[![Contributors](https://img.shields.io/github/contributors/jbleduigou/slog-aws-lambda)](https://github.com/jbleduigou/slog-aws-lambda/graphs/contributors)\n[![License](https://img.shields.io/github/license/jbleduigou/slog-aws-lambda)](./LICENSE)\n\nAn [AWS Lambda Function](https://aws.amazon.com/lambda/) Handler for [slog](https://pkg.go.dev/log/slog) Go library.  \nThe idea is to provide a log handler that will use the attributes present in the lambda context.  \nAlso the handler is using Json format so that it can leverage the features offered by [AWS CloudWatch](https://aws.amazon.com/cloudwatch/).\n\n## 🚀 Install\n\n```sh\ngo get github.com/jbleduigou/slog-aws-lambda\n```\n\n**Compatibility**: go \u003e= 1.21\n\n## 💡 Usage\n\nGoDoc: [https://pkg.go.dev/github.com/jbleduigou/slog-aws-lambda](https://pkg.go.dev/github.com/jbleduigou/slog-aws-lambda)\n\nThe handler will log the following attributes by default:\n* `request_id`: the unique request ID for the lambda function invocation.\n* `function_arn`: the Amazon Resource Name (ARN) that's used to invoke the function, indicates if the invoker specified a version number or alias.\n\nAlso, it will set the log level based on the `LOG_LEVEL` environment variable.\n\n### Using the handler as default handler\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"log/slog\"\n\n\t\"github.com/aws/aws-lambda-go/events\"\n\tslogawslambda \"github.com/jbleduigou/slog-aws-lambda\"\n)\n\nfunc handleS3Event(ctx context.Context, s3Event events.S3Event) {\n\tslog.SetDefault(slog.New(slogawslambda.NewAWSLambdaHandler(ctx, nil)))\n\t//rest of the business logic\n}\n```\n\n### Logging a message with a few attributes\n\nOnce the handler is defined as default, there is no additional configuration needed.  \nWe can start using the logger by directly using the `slog` package.\n\n```go\nimport \"log/slog\"\n\nslog.Info(\"Successfully downloaded configuration file from S3\", \"bucket\", bucket, \"object-key\", objectKey)\n```\n\nThis will produce the following log statement in CloudWatch Logs:\n```json\n{\n    \"time\": \"2024-06-18T21:36:21.128059573Z\",\n    \"level\": \"INFO\",\n    \"msg\": \"Successfully downloaded configuration file from S3\",\n    \"function_arn\": \"arn:aws:lambda:eu-west-1:01234567890:function:myawesomefunction-chN9TDColFt1\",\n    \"request_id\": \"bdb96c48-c3bc-462d-abbd-6b56e6cb3050\",\n    \"bucket\": \"configurationbucket\",\n    \"object-key\": \"config/configuration.yml\"\n}\n```\n\n### Adding environment variables as attributes to the log handler\n\nYou might want to add some environment variables as attributes to the log handler.\nTypically you would want to add some environment variables that will help you identify the specific deployment of your lambda function.\nYou can achieve this by passing the environment variable names as arguments to the `NewAWSLambdaHandler` function.\n\n```golang\nslog.SetDefault(slog.New(slogawslambda.NewAWSLambdaHandler(ctx, nil, \"ENVIRONMENT\", \"REGION\")))\n```\n\nThis will produce the following log statement in CloudWatch Logs:\n```json\n{\n    \"time\": \"2024-06-18T21:36:21.128059573Z\",\n    \"level\": \"INFO\",\n    \"msg\": \"Successfully downloaded configuration file from S3\",\n    \"environment\": \"production\",\n    \"region\": \"eu-west-1\",\n    \"function_arn\": \"arn:aws:lambda:eu-west-1:01234567890:function:myawesomefunction-chN9TDColFt1\",\n    \"request_id\": \"bdb96c48-c3bc-462d-abbd-6b56e6cb3050\"\n}\n```\n\n### Changing the key for request ID or function ARN\n\nThe default case for the attributes is snake-case, i.e. we are using `request_id` and `function_arn`.  \nIf you want to switch to another case you can use the `ReplaceAttr` mechanism provided by the slog package.  \nThis way allows not only to switch to camel case but also renaming altogether the attribute.\n\n```golang\nslog.SetDefault(slog.New(slogawslambda.NewAWSLambdaHandler(ctx, \u0026slog.HandlerOptions{\n    ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {\n\t// change request ID to camel case\n        if a.Key == \"request_id\" {\n            a.Key = \"requestID\"\n            return a\n        }\n        // change the name of the attribute for the invoked function ARN\n        if a.Key == \"function_arn\" {\n            a.Key = \"invokedFunctionARN\"\n            return a\n        }\n        return a\n    },\n})))\n```\n\nThis will produce the following log statement in CloudWatch Logs:\n```json\n{\n    \"time\": \"2024-06-18T21:36:21.128059573Z\",\n    \"level\": \"INFO\",\n    \"msg\": \"Successfully downloaded configuration file from S3\",\n    \"invokedFunctionARN\": \"arn:aws:lambda:eu-west-1:01234567890:function:myawesomefunction-chN9TDColFt1\",\n    \"requestID\": \"bdb96c48-c3bc-462d-abbd-6b56e6cb3050\",\n    \"bucket\": \"configurationbucket\",\n    \"object-key\": \"config/configuration.yml\"\n}\n```\n\n### Stop logging the function ARN\n\nYou might also want to stop logging the function ARN.  \nFor instance if you are not using it and want to save cost on your CloudWatch logs.\n\n```golang\nslog.SetDefault(slog.New(slogawslambda.NewAWSLambdaHandler(ctx, \u0026slog.HandlerOptions{\n    ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {\n        if a.Key == \"request_id\" {\n            // don't log function ARN \n            return slog.Attr{}\n        }\n        return a\n    },\n})))\n```\nThis will produce the following log statement in CloudWatch Logs:\n```json\n{\n    \"time\": \"2024-06-18T21:36:21.128059573Z\",\n    \"level\": \"INFO\",\n    \"msg\": \"Successfully downloaded configuration file from S3\",\n    \"request_id\": \"bdb96c48-c3bc-462d-abbd-6b56e6cb3050\",\n    \"bucket\": \"configurationbucket\",\n    \"object-key\": \"config/configuration.yml\"\n}\n```\n\n### Setting the log level using CloudFormation/SAM\n\nAnother important aspect is to customize the log level.  \nTypically you might want to have a higher level in production (e.g. only log error level messages) to lower the cost of your CloudWatch logs.  \nThis can be achieved by providing a value for the environment variable `LOG_LEVEL`.\n\nIf you are using CloudFormation or SAM as your infrastructure as code tool you could do the following:\n\n```yml\nBudgetCategorizerFunction:\nType: AWS::Serverless::Function \nProperties:\n  CodeUri: ./\n  Handler: bootstrap\n  Environment: \n    Variables:\n      LOG_LEVEL: 'debug'\n```\n\n### Setting the log level using Terraform\n\nIf you are using Terraform as your infrastructure as code tool you could do the following:\n\n```terraform\nresource \"aws_lambda_function\" \"hello_world_arm64\" {\n  filename         = \"bootstrap.zip\"\n  function_name    = \"helloWorldarm64\"\n  architectures    = [\"arm64\"]\n  role             = aws_iam_role.lambda_role.arn\n  handler          = \"bootstrap\"\n  runtime          = \"provided.al2\"\n  source_code_hash = filebase64sha256(\"bootstrap.zip\")\n  \n  environment {\n    variables = {\n      LOG_LEVEL = \"debug\"\n    }\n  }\n}\n```\n\n\n## 📓 Why should we want to log request ID and function ARN\n\nEmbracing serverless comes with scalability and resilience benefits, but logging across a distributed system can be challenging.  \nAWS generates a unique request ID for each Lambda execution, serving as a critical identifier for tracing requests and facilitating troubleshooting.  \n\nImplementing this logging strategy involves retrieving the request ID from lambdacontext, which is exactly what this handler does.  \n\nOnce this is configured it becomes easier to retrieve all the logs for a specific execution, whether you use the AWS Console or a third party tool like DataDog or SumoLogic.\n\n## 🤝 Contributing\n\nFeel free to contribute to this project, either my opening issues or submitting pull requests.  \nDon't hesitate to contact me, by sending me a PM on [LinkedIn](www.linkedin.com/in/jbleduigou).\n\n## 👤 Contributors\n\nThe only contributor so far is me, Jean-Baptiste Le Duigou.\nFeel free to check my blog to about my other projects: http://www.jbleduigou.com\n\n## 💫 Show your support\n\nGive a ⭐️ if this project helped you!\n\n[![GitHub Sponsors](https://img.shields.io/github/sponsors/jbleduigou?style=for-the-badge)](https://github.com/sponsors/jbleduigou)\n\n## 📝 License\n\nThis project is [MIT](./LICENSE) licensed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjbleduigou%2Fslog-aws-lambda","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjbleduigou%2Fslog-aws-lambda","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjbleduigou%2Fslog-aws-lambda/lists"}