Skip to content

Commit 518cdd0

Browse files
authored
better filtering of log groups owned by active lambdas (#214)
1 parent f72cf55 commit 518cdd0

File tree

9 files changed

+19728
-7
lines changed

9 files changed

+19728
-7
lines changed

cmd/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ go_library(
2020
"//vendor/github.com/aws/aws-sdk-go/service/iam:go_default_library",
2121
"//vendor/github.com/aws/aws-sdk-go/service/kinesis:go_default_library",
2222
"//vendor/github.com/aws/aws-sdk-go/service/kms:go_default_library",
23+
"//vendor/github.com/aws/aws-sdk-go/service/lambda:go_default_library",
2324
"//vendor/github.com/aws/aws-sdk-go/service/s3:go_default_library",
2425
"//vendor/github.com/dustin/go-humanize:go_default_library",
2526
"//vendor/github.com/mitchellh/go-homedir:go_default_library",

cmd/orphaned.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717
import (
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

135137
func 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("\tretention %d\n", retentionDays)
198-
fmt.Printf("\tbytes %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)

vendor/github.com/aws/aws-sdk-go/service/lambda/BUILD.bazel

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)