-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.ts
48 lines (41 loc) · 1.15 KB
/
handler.ts
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
import fetch from "node-fetch";
type EventHandler<Input, Output> = (event: Input) => Promise<Output>;
interface CloudWatchInput {
Records: Record[];
}
type Record = {
Sns: SNS;
};
type SNS = {
Subject: string;
Message: string;
Timestamp: string;
};
export const handler: EventHandler<CloudWatchInput, any> = async event => {
const subject = event.Records[0].Sns.Subject;
const message = JSON.parse(event.Records[0].Sns.Message);
const timestamp = event.Records[0].Sns.Timestamp;
const slackMessage = `
A cloudwatch alarm has been triggered.\n
Subject is: ${subject}\n
Message: ${message.AlarmDescription}\n
Timestamp: ${timestamp}`;
const body = JSON.stringify({
text: slackMessage,
});
try {
await fetch("SLACK_ENDPOINT_URL", {
method: "post",
body: body,
headers: {
accept: "application/json",
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(body).toString(),
},
});
} catch (error) {
// tslint:disable-next-line:no-console
console.error("ERROR", error.message, JSON.stringify(error));
throw error; // to return the error to the consumer
}
};