Skip to content

Utility lambdas that automatically set retention policy on log groups.

License

Notifications You must be signed in to change notification settings

rcoundon/serverless-lambda-log-management

 
 

Repository files navigation

Manage Lambda Logs Generated By SST

This is a fork of Allan Helton's project here which was developed for use with AWS SAM. SST is my personal favourite means of deploying serverless applications so I've taken what Allan did and converted it to use SST and TypeScript.

In This Stack

Two lambdas are provided in this stack:

  • delete-unused-log-groups - Removes and lambda log groups that remained when a lambda resource name changed
  • set-log-retention - Sets all lambda function log groups to have a retention period defined in an environment variable

Architecture diagram for the two provided lambdas

Schedule

The lambdas are triggered on a schedule so you don't have to maintain anything as you continue to build. The scheduled jobs/crons are added only if the environment variables LOG_GROUP_RETENTION_CRON and LOG_GROUP_DELETE_CRON are specified

set-log-retention

It will set the log retention of all lambdas that do not match the configured retention period in the lambda environment variable.

delete-unused-log-groups

Typically you'd want to set this to run every 7 days. As this is likely a much less frequently needed lambda function, you might be able to run it on demand. Resource names do not change frequently (really only when developers change it manually), so this lambda is set less frequently to run.

Regex Matching

For both lambdas, it's possible to specify a regular expression to match the log group name. So you could use /-dev-/ to only apply this to log groups where their name matches -dev- This is specified using the LOG_GROUP_MATCH_REGEX env var

Deployment

See the docs over at SST for information on setting stage names and regions. The scripts in package json currently deploy to eu-west-2 (specified in sst.confiig.ts) with stage dev so change this as appropriate

Things of Note

This project uses the AWS v3 SDK to perform all interactions with the infrastructure. Version 3 of the SDK is not included by default in the lambda environment, so we bundle it with our lambdas

The lambda functions are running nodejs18.x and are executed on the arm64 architecture aka AWS Graviton2 processor.

Contact

You can show your support for the original project by following Allan on Twitter or connecting with him on LinkedIn. I'm on Twitter too if you have any questions on this implementation, or log an issue on Github.

About

Utility lambdas that automatically set retention policy on log groups.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 92.9%
  • JavaScript 7.1%