Git Product home page Git Product logo

aws-cloudwatch-log-minder's Introduction

AWS Cloudwatch Log minder

AWS CloudWatch logs is an useful logging system, but it has two quircks. It does not allow you too set a default retention period for newly created log groups, and it does not delete empty log streams that are older than the retention period. This utility:

  1. sets a default retention period on log groups without a period set.
  2. removes empty log streams older than the retention period of the log group

You can use it as a command line utility. You can also install it as an AWS Lambda function and have your logs kept in order, NoOps style!

install the log minder

to install the log minder, type:

pip install aws-cloudwatch-log-minder

set default retention period

to set the default retention period on log groups without one, type:

cwlog-minder --dry-run set-log-retention --days 30

This will show you which log groups will have its retention period set. Remove the --dry-run and it the retention period will be set. If you wish to set the retention of all log groups to the same value, type:

cwlog-minder --dry-run set-log-retention --days 30 --overwrite

delete empty log streams

To delete empty log streams older than the retention period, type:

cwlog-minder --dry-run delete-empty-log-streams

This will show you which empty log streams will be deleted. Remove the --dry-run and these stream will be deleted.

deploy the log minder

To deploy the log minder as an AWS Lambda, type:

git clone https://github.com/binxio/aws-cloudwatch-log-minder.git
cd aws-cloudwatch-log-minder
aws cloudformation deploy \
	--capabilities CAPABILITY_IAM \
	--stack-name aws-cloudwatch-log-minder \
	--template-file ./cloudformation/aws-cloudwatch-log-minder.yaml \
	--parameter-overrides LogRetentionInDays=30

This will install the log minder in your AWS account and run every hour.

delete empty log groups

To delete empty log groups, type:

cwlog-minder --dry-run delete-empty-log-groups

This will show you which empty log groups will be deleted. Remove the --dry-run and these groups will be deleted. Do not use this command, if your log groups are managed by CloudFormation or Terraform.

verbose

export LOG_LEVEL=DEBUG
cwlog-minder ...

region and profile selection

AWS regions and credential profiles can be selected via command line arguments or environment variables.

region via parameter

cwlog-minder --region eu-west-1 ...

region via environment

export AWS_DEFAULT_REGION=eu-west-1
cwlog-minder ...

profile via parameter

cwlog-minder --profile dev ...

profile via environment

export AWS_PROFILE=dev
cwlog-minder ...

aws-cloudwatch-log-minder's People

Contributors

afittz avatar akvadrako avatar dependabot[bot] avatar friedcircuits avatar jcotton1123 avatar larsfronius avatar mvanholsteijn avatar otanner avatar ramsperger avatar regevbr avatar silvexis avatar trondhindenes avatar vschurink avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aws-cloudwatch-log-minder's Issues

Please add `--region` and `--profile` options

It would be nice if this utility could support the --region and --profile options like the aws-cli does.
This would enable us to use this tool in an environment with many accounts, regions and profiles.

E.g. I have several profiles for several accounts, and each account has one profile for readonly and admin access.

cwlog-minder --dry-run delete-empty-log-streams --profile my-admin would allow me to use this tool much more easily on the command line.

Unable to deploy via CloudFormation in AWS GovCloud

The current CloudFormation template does not work with AWS GovCloud.

It returns the following error message:

Partition "aws" is not valid for resource "arn:aws:lambda:us-gov-west-1:{acct-id}:function:aws-cloudwatch-delete-empty-log-streams". (Service: AmazonIdentityManagement; Status Code: 400; Error Code: MalformedPolicyDocument; Request ID: xxxx)

If you're open to a pull request, I'd be happy to make the small alteration necessary to the CloudFormation template to support this deployment scenario.

Feature request: Delete empty log groups

It would be nice to have a command to delete empty log groups. This seems useful after running delete_empty_log_streams.

The below seems to work for me (need to clean up)

def _delete_empty_log_groups(
    group: dict, purge_non_empty: bool = False, dry_run: bool = False
):
    now = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
    log_group_name = group["logGroupName"]
    retention_in_days = group.get("retentionInDays", 0)
    if not retention_in_days:
        log.info(
            "skipping log group %s as it has no retention period set",
            log_group_name,
        )
        return

    kwargs = {
        "logGroupName": log_group_name,
        "orderBy": "LastEventTime",
        "descending": False,
        "PaginationConfig": {"PageSize": 50},
    }

    for response in cw_logs.get_paginator("describe_log_streams").paginate(
        **kwargs
    ):
        if len(response["logStreams"]) == 0:
            log.info(
                "Deleting group %s", log_group_name,
            )
            cw_logs.delete_log_group(logGroupName=log_group_name)

Thanks for such a great python script!

delete_empty_log_groups not in cloudformation

The lambda delete_empty_log_groups is not included in the Cloudformation and is not run as part of the emptying of the log streams (or on a separate schedule). After a while there is a buildup of empty log groups that causes the delete_empty_log_streams lambda to run for a long time and thousands of needless invocations.
Should the delete_empty_log_groups be included in the Cloudformation (with a schedule)?

delete-empty-log-groups doesn't seem to do anything

This looks like a great utility, but I was noticing that delete-empty-log-groups didn't seem to actually delete any log groups. When looking at the code, there doesn't seem to be anything that actually performs the delete (it just logs that it would happen)

There are no log messages indicating success when a lot of log streams are deleted

I was running log minder on a big collection of log steams. Since there was no log output I thought the tool was stuck or not working. When I turned on Debug logging I discovered it was doing a lot of log deletion. No log messages were shown because they were set to debug level.

Maybe a summary log message every 10 seconds can help show progress so that users don't think the script is not working.

public bucket is not accessible

Hi,

Thanks for this great tool!

I have a few suggestions and issues:

  1. It will be easier to use a single line to deploy with cloud formation:
aws cloudformation deploy --stack-name=aws-cloudwatch-log-minder --template-file=./cloudformation/aws-cloudwatch-log-minder.yaml --capabilities CAPABILITY_IAM --parameter-overrides LambdaS3Bucket=<my bucket> CFNCustomProviderZipFileName=<zipfile location>
  1. Add cloudformation parameter to change the default retention period
  2. List all the required permissions for the cloudformation stack creation
  3. The public s3 bucket that contains the zipped version is not publicly accessible - I had to manually build it (using make) and deploy it to my own bucket
  4. When trying to build manually, the make command failed because you pass the encoding param in setup.py to the open method (line 10), and apparently it is not supported. Removing this param solved the issue for me.

side note - this is the first time I dealt with python code :-)

@mvanholsteijn any thoughts?

The storedBytes for the stream used in delete_empty_log_streams.py is deprecated

From https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/logs.html#CloudWatchLogs.Client.describe_log_streams

storedBytes (integer) --
The number of bytes stored.
IMPORTANT: On June 17, 2019, this parameter was deprecated for log streams, and is always reported as zero. This change applies only to log streams. The storedBytes parameter for log groups is not affected.

The code uses this parameter in delete_empty_log_streams.py.
I am observing some storedBytes to have a value other than zero when the stream is empty.

CloudFormation: Public S3 zip not accessible

Hi, I just want to inform that the link https://binxio-public-eu-central-1.s3.eu-central-1.amazonaws.com/lambdas/aws-cloudwatch-log-minder-0.4.1.zip is currently not accessible so CloudFormation stack creation fails.

Thank you.

Initial log stream deletion takes too long for older applications

I have been running an application for more than 3 years without the log minder. There are >100k log streams in 1 log group. With the rate limits of AWS Cloudwatch and the default maximum log minder duration of 5 minutes it would take weeks to clean up all the log streams.

For now I've been running log-minder from my laptop and leaving it on throughout the night. This has cleaned my log group in 30 hours or so.

Maybe log-minder can run continuously when it notices there is more work to do. That would be easier than running the CLI manually.

Feature Request: Filter Log Groups by tag before setting retention

It would be nice if we can filter out some Log Groups that do not have any retention and have a tag like - NoExpire= true as part of set-log-retention command.

We have some scenarios were we don't want to set retention for certain log groups, so filtering them out and setting default retention for other would be great.

cwlog-minder --dry-run set-log-retention --days 30 --filter tagKey:tagValue

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.