Created
October 18, 2018 15:29
-
-
Save ritesh/4989d641c14fb07c8afed751d3e742d1 to your computer and use it in GitHub Desktop.
Expire CloudWatch logs after X days
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 ( | |
| "fmt" | |
| "log" | |
| "github.com/aws/aws-sdk-go/aws" | |
| "github.com/aws/aws-sdk-go/aws/session" | |
| "github.com/aws/aws-sdk-go/service/cloudwatchlogs" | |
| ) | |
| const ( | |
| //Expire logs after these many days | |
| LogGroupExpiration = 30 | |
| ) | |
| func main() { | |
| sess, err := session.NewSession() | |
| if err != nil { | |
| log.Fatal("errr") | |
| } | |
| svc := cloudwatchlogs.New(sess) | |
| err = svc.DescribeLogGroupsPages( | |
| &cloudwatchlogs.DescribeLogGroupsInput{}, | |
| func(logGroup *cloudwatchlogs.DescribeLogGroupsOutput, lastPage bool) bool { | |
| fmt.Println("Got", len(logGroup.LogGroups), "log groups") | |
| for _, obj := range logGroup.LogGroups { | |
| fmt.Println("Key:", aws.StringValue(obj.Arn)) | |
| _, err = svc.PutRetentionPolicy(&cloudwatchlogs.PutRetentionPolicyInput{ | |
| LogGroupName: obj.LogGroupName, | |
| RetentionInDays: aws.Int64(LogGroupExpiration), | |
| }) | |
| if err != nil { | |
| log.Fatal("couldnt set policy fail") | |
| } | |
| } | |
| return true | |
| }) | |
| if err != nil { | |
| log.Fatal("fail", err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment