@@ -17,6 +17,7 @@ package cmd
1717import (
1818 "fmt"
1919 "os"
20+ "strings"
2021 "time"
2122
2223 "encoding/json"
@@ -29,6 +30,7 @@ import (
2930 "github.com/aws/aws-sdk-go/service/dynamodb"
3031 "github.com/aws/aws-sdk-go/service/kinesis"
3132 "github.com/aws/aws-sdk-go/service/kms"
33+ "github.com/aws/aws-sdk-go/service/lambda"
3234 "github.com/aws/aws-sdk-go/service/s3"
3335 "github.com/dustin/go-humanize"
3436 "github.com/spf13/cobra"
@@ -134,6 +136,7 @@ func getSession() *session.Session {
134136
135137func processLogs (rootedResources map [string ]bool ) {
136138 svc := cloudwatchlogs .New (getSession ())
139+ l := lambda .New (getSession ())
137140
138141 fmt .Printf ("GroupName, RetentionDays, StoredBytesRaw, StoredBytesHuman, LastLogEntry, DaysAgo\n " )
139142 var nextGroup * string
@@ -158,9 +161,29 @@ func processLogs(rootedResources map[string]bool) {
158161 if Debug {
159162 fmt .Printf ("Processing %v\n " , * group )
160163 }
161- if _ , ok := rootedResources [* group .LogGroupName ]; ok {
162- // this stream is owned by a cloudformation stack, skip it
164+ _ , ownedByCfn := rootedResources [* group .LogGroupName ]
165+ ownedByLambda := false
166+ if strings .HasPrefix (* group .LogGroupName , "/aws/lambda" ) {
167+ // check whether there is a lambda function with a matching name
168+ // we do this because log groups are freqently created by the
169+ // lambda, not by the cloudformation stack that created the lambda
170+ lambdaName := strings .Split (* group .LogGroupName , "/aws/lambda/" )[1 ]
163171 if Debug {
172+ fmt .Printf ("Checking lambda %v\n " , lambdaName )
173+ }
174+ _ , err := l .GetFunction (& lambda.GetFunctionInput {
175+ FunctionName : aws .String (lambdaName ),
176+ })
177+ if err == nil {
178+ ownedByLambda = true
179+ if Debug {
180+ fmt .Printf ("Group %v is owned by a lambda function, skipping\n " , * group .LogGroupName )
181+ }
182+ }
183+ }
184+ if ownedByCfn || ownedByLambda {
185+ // this stream is owned by a cloudformation stack, skip it
186+ if Debug && ownedByCfn {
164187 fmt .Printf ("Group %v is owned by a cloudformation stack, skipping\n " , * group .LogGroupName )
165188 }
166189 } else {
@@ -192,11 +215,6 @@ func processLogs(rootedResources map[string]bool) {
192215 if group .RetentionInDays != nil {
193216 retentionDays = * group .RetentionInDays
194217 }
195- if Debug {
196- fmt .Printf ("processing group %v\n " , * group .LogGroupName )
197- fmt .Printf ("\t retention %d\n " , retentionDays )
198- fmt .Printf ("\t bytes %v\n " , * group .StoredBytes )
199- }
200218 sizeHuman := humanize .Bytes (uint64 (* group .StoredBytes ))
201219 sizeRaw := uint64 (* group .StoredBytes )
202220 fmt .Printf ("\" %v\" , %d, %v, %v, %v, %d\n " , * group .LogGroupName , retentionDays , sizeRaw , sizeHuman , lastEvent , daysAgo )
0 commit comments