-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda_function.py
87 lines (79 loc) · 2.96 KB
/
lambda_function.py
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
# -*- coding: utf-8 -*-
# Version: 0.1a4
import json
import logging
import os
import uuid
from base64 import b64decode
from urllib.error import URLError, HTTPError
from urllib.request import Request, urlopen
import boto3
ENCRYPTED_HOOK_URL = os.environ['kmsEncryptedHookUrl']
SLACK_CHANNEL = os.environ['slackChannel']
HOOK_URL = 'https://' + boto3.client('kms') \
.decrypt(CiphertextBlob=b64decode(ENCRYPTED_HOOK_URL))['Plaintext'] \
.decode('utf-8')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
confluence_url = 'http://www.korniichuk.com'
cloudwatch_url = 'http://www.korniichuk.com'
icon_url = 'https://raw.githubusercontent.com/korniichuk/' \
'cloudwatch-alarm/master/img/amazon_cloudwatch_icon.png'
metric = """{
"metrics": [[
"LogMetrics",
"MetricName",
{"period": 3600, "stat": "Sum"}]],
"title": "Title",
"start": "-P1D",
"end": "P0D",
"timezone": "+0100"}"""
bucket_name = 'korniichuk' # for temporary public file
filename = 'tmp/cloudwatch_metric_chart_%s.png' % uuid.uuid4()
# Get MetricWidgetImage from CloudWatch Metrics
response = boto3.client('cloudwatch') \
.get_metric_widget_image(MetricWidget=metric)
data = response['MetricWidgetImage']
# Create temporary public file on S3
boto3.client('s3').put_object(ACL='public-read', Body=data,
Bucket=bucket_name, Key=filename)
# Get URL of piblic file on S3
response = boto3.client('s3').get_bucket_location(Bucket=bucket_name)
bucket_location = response['LocationConstraint']
image_url = 'https://s3-{0}.amazonaws.com/{1}/{2}'.format(
bucket_location, bucket_name, filename)
logger.info('Event: ' + str(event))
message = json.loads(event['Records'][0]['Sns']['Message'])
logger.info('Message: ' + str(message))
alarm_name = message['AlarmName']
reason = message['NewStateReason']
attachments = [{
'title': 'Reason :fire:',
'text': reason,
'color': 'danger',
'fields': [
{
'title': 'Confluence',
'value': confluence_url,
'short': True},
{
'title': 'CloudWatch',
'value': cloudwatch_url,
'short': True}],
'image_url': image_url}]
slack_message = {
'channel': SLACK_CHANNEL,
'username': 'Amazon CloudWatch',
'icon_url': icon_url,
'text': '`%s`' % alarm_name,
'attachments': attachments}
req = Request(HOOK_URL, json.dumps(slack_message).encode('utf-8'))
try:
response = urlopen(req)
response.read()
logger.info('Message posted to %s', slack_message['channel'])
except HTTPError as e:
logger.error('Request failed: %d %s', e.code, e.reason)
except URLError as e:
logger.error('Server connection failed: %s', e.reason)