-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSnsAlarm.test.ts
More file actions
111 lines (100 loc) · 3.6 KB
/
SnsAlarm.test.ts
File metadata and controls
111 lines (100 loc) · 3.6 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
import {App, Duration, Stack} from "aws-cdk-lib"
import {Template} from "aws-cdk-lib/assertions"
import {ComparisonOperator, Unit} from "aws-cdk-lib/aws-cloudwatch"
import {Topic} from "aws-cdk-lib/aws-sns"
import {describe, expect, it} from "vitest"
import {SnsAlarm} from "../../src/constructs/SnsAlarm"
const importedSlackTopicArn = "arn:aws:sns:eu-west-2:111111111111:SlackAlertsTopic"
describe("SnsAlarm construct", () => {
it("applies sane defaults for simple alarm definitions", () => {
const app = new App()
const stack = new Stack(app, "TestStack")
const slackAlertTopic = Topic.fromTopicArn(stack, "SlackAlertsTopic", importedSlackTopicArn)
const metricAlarm = new SnsAlarm(stack, "SimpleMetricAlarm", {
stackName: "pfp-test-stack",
enableAlerts: true,
alarmDefinition: {
alarmName: "MySimpleAlarm",
alarmDescription: "An alarm for any breach (threshold 1) in a single period"
},
metricStatConfig: {
namespace: "LambdaLogFilterMetrics",
metricName: "ErrorCount"
},
snsTopic: slackAlertTopic
})
expect(metricAlarm.alarm).toBeDefined()
const template = Template.fromStack(stack)
template.resourceCountIs("AWS::SNS::Topic", 0)
template.hasResourceProperties("AWS::CloudWatch::Alarm", {
AlarmName: "pfp-test-stack-MySimpleAlarm",
Namespace: "LambdaLogFilterMetrics",
MetricName: "ErrorCount",
Threshold: 1,
ComparisonOperator: "GreaterThanOrEqualToThreshold",
Unit: "Count",
Statistic: "Sum",
Period: 60,
EvaluationPeriods: 1,
TreatMissingData: "notBreaching",
AlarmDescription: "An alarm for any breach (threshold 1) in a single period",
AlarmActions: [importedSlackTopicArn],
OKActions: [importedSlackTopicArn],
InsufficientDataActions: [importedSlackTopicArn],
ActionsEnabled: true
})
})
it("allows overriding threshold, comparison operator, unit and dimensions", () => {
const app = new App()
const stack = new Stack(app, "OverrideStack")
const slackAlertTopic = Topic.fromTopicArn(stack, "SlackAlertsTopic", importedSlackTopicArn)
const metricAlarm = new SnsAlarm(stack, "OverrideMetricAlarm", {
stackName: "pfp-test-stack",
enableAlerts: false,
alarmDefinition: {
alarmName: "MyOverrideAlarm",
alarmDescription: "Override alarm",
threshold: 250,
comparisonOperator: ComparisonOperator.GREATER_THAN_THRESHOLD,
evaluationPeriods: 3,
datapointsToAlarm: 2
},
metricStatConfig: {
namespace: "CustomNamespace",
metricName: "Latency",
unitFilter: Unit.MILLISECONDS,
dimensions: [
{
name: "FunctionName",
value: "my-function"
}
],
period: Duration.minutes(1),
statistic: "Sum"
},
snsTopic: slackAlertTopic
})
expect(metricAlarm.alarm).toBeDefined()
const template = Template.fromStack(stack)
template.hasResourceProperties("AWS::CloudWatch::Alarm", {
AlarmName: "pfp-test-stack-MyOverrideAlarm",
Namespace: "CustomNamespace",
MetricName: "Latency",
Threshold: 250,
ComparisonOperator: "GreaterThanThreshold",
DatapointsToAlarm: 2,
Unit: "Milliseconds",
Dimensions: [
{
Name: "FunctionName",
Value: "my-function"
}
],
AlarmDescription: "Override alarm",
AlarmActions: [importedSlackTopicArn],
OKActions: [importedSlackTopicArn],
InsufficientDataActions: [importedSlackTopicArn],
ActionsEnabled: false
})
})
})