-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathalarm.go
76 lines (70 loc) · 2.52 KB
/
alarm.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
package decorator
import (
"context"
"fmt"
gof "github.com/awslabs/goformation/v5/cloudformation"
gofcloudwatch "github.com/awslabs/goformation/v5/cloudformation/cloudwatch"
goflambda "github.com/awslabs/goformation/v5/cloudformation/lambda"
sparta "github.com/mweagle/Sparta/v3"
"github.com/rs/zerolog"
)
// CloudWatchErrorAlarmDecorator returns a TemplateDecoratorHookFunc
// that associates a CloudWatch Lambda Error count alarm with the given
// lambda function. The four parameters are periodWindow, minutes per period
// the strict lower bound value, and the SNS topic to which alerts should be
// sent. See the CloudWatch alarm resource type in the official
// AWS documentation at https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html
// for more information
func CloudWatchErrorAlarmDecorator(periodWindow int,
minutesPerPeriod int,
thresholdGreaterThanOrEqualToValue float64,
snsTopic string) sparta.TemplateDecoratorHookFunc {
alarmDecorator := func(ctx context.Context,
serviceName string,
lambdaResourceName string,
lambdaResource *goflambda.Function,
resourceMetadata map[string]interface{},
lambdaFunctionCode *goflambda.Function_Code,
buildID string,
template *gof.Template,
logger *zerolog.Logger) (context.Context, error) {
periodInSeconds := minutesPerPeriod * 60
alarm := &gofcloudwatch.Alarm{
AlarmName: gof.Join("", []string{
"ERROR Alarm for ",
gof.Ref(lambdaResourceName)}),
AlarmDescription: gof.Join(" ", []string{
"ERROR count for AWS Lambda function",
gof.Ref(lambdaResourceName),
"( Stack:",
gof.Ref("AWS::StackName"),
") is greater than",
fmt.Sprintf("%.2f", thresholdGreaterThanOrEqualToValue),
"over the last",
fmt.Sprintf("%d", periodInSeconds),
"seconds",
}),
MetricName: "Errors",
Namespace: "AWS/Lambda",
Statistic: "Sum",
Period: periodInSeconds,
EvaluationPeriods: periodWindow,
Threshold: thresholdGreaterThanOrEqualToValue,
ComparisonOperator: "GreaterThanOrEqualToThreshold",
Dimensions: []gofcloudwatch.Alarm_Dimension{
{
Name: "FunctionName",
Value: lambdaResourceName,
},
},
TreatMissingData: "notBreaching",
AlarmActions: []string{snsTopic},
}
// Create the resource, add it...
alarmResourceName := sparta.CloudFormationResourceName("Alarm",
lambdaResourceName)
template.Resources[alarmResourceName] = alarm
return ctx, nil
}
return sparta.TemplateDecoratorHookFunc(alarmDecorator)
}