-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.go
More file actions
135 lines (103 loc) · 3.68 KB
/
lambda.go
File metadata and controls
135 lines (103 loc) · 3.68 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package window
import (
"time"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/aws/aws-sdk-go/aws"
)
type (
LambdaFunction struct {
// It is the SHA256 hash of your function deployment package.
CodeSha256 string
// The size, in bytes, of the function .zip file you uploaded.
CodeSize int64
// The user-provided description.
Description string
// The Amazon Resource Name (ARN) assigned to the function.
FunctionArn string
// The name of the function.
FunctionName string
// The function Lambda calls to begin executing your function.
Handler string
// The timestamp of the last time you updated the function.
LastModified string
// The memory size, in MB, you configured for the function. Must be a multiple
// of 64 MB.
MemorySize int64
// The Amazon Resource Name (ARN) of the IAM role that Lambda assumes when it
// executes your function to access any other Amazon Web Services (AWS) resources.
Role string
// The runtime environment for the Lambda function.
Runtime string
// The function execution time at which Lambda should terminate the function.
// Because the execution time has cost implications, we recommend you set this
// value based on your expected execution time. The default is 3 seconds.
Timeout time.Duration
// The version of the Lambda function.
Version string
// VPC configuration associated with your Lambda function.
VpcConfig *lambda.VpcConfigResponse
Name string
Id string
State string
LastModifiedTime time.Time
Region *Region
VPC *VPC
SecurityGroups []*SecurityGroup
Subnets []*Subnet
CloudWatchAlarms []*CloudWatchAlarm
Stats *LambdaFunctionStats
}
LambdaFunctionsByNameAsc []*LambdaFunction
)
func (a LambdaFunctionsByNameAsc) Len() int { return len(a) }
func (a LambdaFunctionsByNameAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a LambdaFunctionsByNameAsc) Less(i, j int) bool {
return string_less_than(a[i].Name, a[j].Name)
}
func LoadLambdaFunctions(input *lambda.ListFunctionsInput) (map[string]*LambdaFunction, error) {
funcs := map[string]*LambdaFunction{}
if err := LambdaClient.ListFunctionsPages(input, func(p *lambda.ListFunctionsOutput, _ bool) bool {
for _, f := range p.Functions {
lf := &LambdaFunction{
CodeSha256: aws.StringValue(f.CodeSha256),
CodeSize: aws.Int64Value(f.CodeSize),
Description: aws.StringValue(f.Description),
FunctionArn: aws.StringValue(f.FunctionArn),
FunctionName: aws.StringValue(f.FunctionName),
Handler: aws.StringValue(f.Handler),
LastModified: aws.StringValue(f.LastModified),
MemorySize: aws.Int64Value(f.MemorySize),
Role: aws.StringValue(f.Role),
Runtime: aws.StringValue(f.Runtime),
Timeout: time.Duration(aws.Int64Value(f.Timeout)) * time.Second,
Version: aws.StringValue(f.Version),
VpcConfig: f.VpcConfig,
}
lf.Name = lf.FunctionName
lf.Id = "lambda:" + lf.FunctionArn
lf.LastModifiedTime, _ = time.Parse(`2006-01-02T15:04:05.999999999-0700`, lf.LastModified) // fuck
funcs[lf.FunctionName] = lf
}
return true
}); err != nil {
return nil, err
}
return funcs, nil
}
func (lf *LambdaFunction) Poll() []chan error {
var errs []chan error
lf.Stats = &LambdaFunctionStats{}
for _, m := range LambdaFunctionMetrics {
m := m
errs = append(errs, lf.Region.Throttle.do(lf.Name+":"+*m.name+" Lambda METRICS POLL", func() error {
return m.RunFor(lf)
}))
}
return errs
}
func (lf *LambdaFunction) Inactive() bool {
if lf.Stats == nil {
return false // default to active if we can't tell
}
return lf.Stats.InvocationsPerSecond == 0
}