-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
76 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,83 @@ | ||
import { SNS, PublishCommand } from '@aws-sdk/client-sns'; | ||
|
||
const sns = new SNS({ region: process.env.AWS_REGION }); | ||
const sns = new SNS({ | ||
region: process.env.SLACK_MESSAGE_RELAY_SNS_TOPIC_ARN.split(':')[3], | ||
}); | ||
|
||
const GREEN = '#2eb886'; | ||
const RED = '#a30200'; | ||
|
||
function timeSince(date) { | ||
let seconds = Math.floor((+new Date() - date) / 1000); | ||
|
||
let interval = seconds / 86400; | ||
if (interval > 1) { | ||
return Math.floor(interval) + ' days'; | ||
} | ||
interval = seconds / 3600; | ||
if (interval > 1) { | ||
return Math.floor(interval) + ' hours'; | ||
} | ||
interval = seconds / 60; | ||
if (interval > 1) { | ||
return Math.floor(interval) + ' minutes'; | ||
} | ||
return Math.floor(seconds) + ' seconds'; | ||
} | ||
|
||
export const handler = async (event) => { | ||
sns.send( | ||
new PublishCommand({ | ||
TopicArn: process.env.SLACK_MESSAGE_RELAY_SNS_TOPIC_ARN, | ||
Message: JSON.stringify({ | ||
username: 'AWS Health Events', | ||
icon_emoji: ':ops-aws-health:', | ||
channel: 'G2QHC2N7K', // #ops-warn | ||
attachments: [ | ||
{ | ||
color: '#a30200', | ||
fallback: event.eventArn.eventTypeCode, | ||
blocks: [ | ||
{ | ||
type: 'section', | ||
text: { | ||
type: 'mrkdwn', | ||
text: event.eventArn.eventDescription[0].latestDescription, | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}), | ||
console.log( | ||
JSON.stringify({ | ||
msg: 'Input event', | ||
event, | ||
}), | ||
); | ||
|
||
const region = event.detail.eventRegion; | ||
const service = event.detail.service; | ||
const code = event.detail.eventTypeCode; | ||
const category = event.detail.eventTypeCategory; | ||
const scope = event.detail.eventScopeCode; | ||
const startTs = Date.parse(event.detail.startTime); | ||
const status = event.detail.statusCode; | ||
const description = event.detail.eventDescription[0].latestDescription; | ||
|
||
if (scope === 'PUBLIC' && category === 'issue') { | ||
const color = status === 'closed' ? GREEN : RED; | ||
|
||
await sns.send( | ||
new PublishCommand({ | ||
TopicArn: process.env.SLACK_MESSAGE_RELAY_SNS_TOPIC_ARN, | ||
Message: JSON.stringify({ | ||
username: 'AWS Health Events', | ||
icon_emoji: ':ops-aws-health:', | ||
// channel: 'G2QHC2N7K', // #ops-warn | ||
channel: 'CHZTAGBM2', // #ops-warn | ||
attachments: [ | ||
{ | ||
color, | ||
fallback: event.detail.eventTypeCode, | ||
blocks: [ | ||
{ | ||
type: 'section', | ||
text: { | ||
type: 'mrkdwn', | ||
text: [ | ||
`*Service:* ${service}`, | ||
`*Region:* ${region}`, | ||
`*Event Code:* ${code}`, | ||
'\n', | ||
`*Started:* ${timeSince(new Date(startTs))} ago`, | ||
'\n', | ||
description, | ||
].join('\n'), | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}), | ||
}), | ||
); | ||
} | ||
}; |