-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
lambda.go
90 lines (73 loc) · 2.29 KB
/
lambda.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package resources
import (
"context"
"time"
"github.com/aws/aws-sdk-go/aws"
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
)
func (lf *LambdaFunctions) getAll(c context.Context, configObj config.Config) ([]*string, error) {
var names []*string
err := lf.Client.ListFunctionsPages(
&lambda.ListFunctionsInput{}, func(page *lambda.ListFunctionsOutput, lastPage bool) bool {
for _, lambda := range page.Functions {
if lf.shouldInclude(lambda, configObj) {
names = append(names, lambda.FunctionName)
}
}
return !lastPage
})
if err != nil {
return nil, err
}
return names, nil
}
func (lf *LambdaFunctions) shouldInclude(lambdaFn *lambda.FunctionConfiguration, configObj config.Config) bool {
if lambdaFn == nil {
return false
}
fnLastModified := aws.StringValue(lambdaFn.LastModified)
fnName := lambdaFn.FunctionName
layout := "2006-01-02T15:04:05.000+0000"
lastModifiedDateTime, err := time.Parse(layout, fnLastModified)
if err != nil {
logging.Debugf("Could not parse last modified timestamp (%s) of Lambda function %s. Excluding from delete.", fnLastModified, *fnName)
return false
}
return configObj.LambdaFunction.ShouldInclude(config.ResourceValue{
Time: &lastModifiedDateTime,
Name: fnName,
})
}
func (lf *LambdaFunctions) nukeAll(names []*string) error {
if len(names) == 0 {
logging.Debugf("No Lambda Functions to nuke in region %s", lf.Region)
return nil
}
logging.Debugf("Deleting all Lambda Functions in region %s", lf.Region)
deletedNames := []*string{}
for _, name := range names {
params := &lambda.DeleteFunctionInput{
FunctionName: name,
}
_, err := lf.Client.DeleteFunction(params)
// Record status of this resource
e := report.Entry{
Identifier: aws.StringValue(name),
ResourceType: "Lambda function",
Error: err,
}
report.Record(e)
if err != nil {
logging.Errorf("[Failed] %s: %s", *name, err)
} else {
deletedNames = append(deletedNames, name)
logging.Debugf("Deleted Lambda Function: %s", awsgo.StringValue(name))
}
}
logging.Debugf("[OK] %d Lambda Function(s) deleted in %s", len(deletedNames), lf.Region)
return nil
}