Created
October 23, 2018 14:42
-
-
Save ritesh/db24e1280183b699e23c9b30870a3f91 to your computer and use it in GitHub Desktop.
Go-sdk-v2 set log group retention to 1 day
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" | |
| "os" | |
| "github.com/aws/aws-sdk-go-v2/aws" | |
| "github.com/aws/aws-sdk-go-v2/aws/external" | |
| "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" | |
| ) | |
| const ( | |
| logGroupRetentionDays = 1 | |
| ) | |
| func main() { | |
| awscfg, err := external.LoadDefaultAWSConfig() | |
| if err != nil { | |
| exitErrorf("failed to load config, %v", err) | |
| } | |
| svc := cloudwatchlogs.New(awscfg) | |
| req := svc.DescribeLogGroupsRequest( | |
| &cloudwatchlogs.DescribeLogGroupsInput{}) | |
| p := req.Paginate() | |
| for p.Next() { | |
| page := p.CurrentPage() | |
| for _, obj := range page.LogGroups { | |
| req := svc.PutRetentionPolicyRequest( | |
| &cloudwatchlogs.PutRetentionPolicyInput{ | |
| LogGroupName: obj.LogGroupName, | |
| RetentionInDays: aws.Int64(logGroupRetentionDays)}) | |
| //output is always none, err may not be | |
| _, err := req.Send() | |
| if err != nil { | |
| fmt.Println("errrr:", err) | |
| } | |
| } | |
| } | |
| if err := p.Err(); err != nil { | |
| exitErrorf("paging error %v", err) | |
| } | |
| } | |
| func exitErrorf(msg string, args ...interface{}) { | |
| fmt.Fprintf(os.Stderr, msg+"\n", args...) | |
| os.Exit(1) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment