Last active
April 22, 2020 05:22
-
-
Save matsubo/43cd5d1604bf49a71127402d2c93ba9e to your computer and use it in GitHub Desktop.
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
import boto3 | |
import json | |
import logging | |
import os | |
from base64 import b64decode | |
from urllib.request import Request, urlopen | |
from urllib.error import URLError, HTTPError | |
# The base-64 encoded, encrypted key (CiphertextBlob) stored in the kmsEncryptedHookUrl environment variable | |
ENCRYPTED_HOOK_URL = os.environ['kmsEncryptedHookUrl'] | |
# The Slack channel to send a message to stored in the slackChannel environment variable | |
SLACK_CHANNEL = os.environ['slackChannel'] | |
HOOK_URL = 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): | |
logger.info("Event: " + str(event)) | |
message = (event['detail']) | |
logger.info("Message: " + str(message)) | |
alarm_name = message['alarmName'] | |
old_state = message['previousState']['value'] | |
new_state = message['state']['value'] | |
reason = message['state']['reason'] | |
slack_message = { | |
'channel': SLACK_CHANNEL, | |
'text': "%s state is now %s: %s" % (alarm_name, new_state, reason) | |
} | |
logger.info(HOOK_URL); | |
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) |
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
{ | |
"version": "0", | |
"id": "c4c1c1c9-6542-e61b-6ef0-8c4d36933a92", | |
"detail-type": "CloudWatch Alarm State Change", | |
"source": "aws.cloudwatch", | |
"account": "123456789012", | |
"time": "2019-10-02T17:04:40Z", | |
"region": "us-east-1", | |
"resources": [ | |
"arn:aws:cloudwatch:us-east-1:123456789012:alarm:ServerCpuTooHigh" | |
], | |
"detail": { | |
"alarmName": "ServerCpuTooHigh", | |
"configuration": { | |
"description": "Goes into alarm when server CPU utilization is too high!", | |
"metrics": [ | |
{ | |
"id": "30b6c6b2-a864-43a2-4877-c09a1afc3b87", | |
"metricStat": { | |
"metric": { | |
"dimensions": { | |
"InstanceId": "i-12345678901234567" | |
}, | |
"name": "CPUUtilization", | |
"namespace": "AWS/EC2" | |
}, | |
"period": 300, | |
"stat": "Average" | |
}, | |
"returnData": true | |
} | |
] | |
}, | |
"previousState": { | |
"reason": "Threshold Crossed: 1 out of the last 1 datapoints [0.0666851903306472 (01/10/19 13:46:00)] was not greater than the threshold (50.0) (minimum 1 datapoint for ALARM -> OK transition).", | |
"reasonData": "{\"version\":\"1.0\",\"queryDate\":\"2019-10-01T13:56:40.985+0000\",\"startDate\":\"2019-10-01T13:46:00.000+0000\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[0.0666851903306472],\"threshold\":50.0}", | |
"timestamp": "2019-10-01T13:56:40.987+0000", | |
"value": "OK" | |
}, | |
"state": { | |
"reason": "Threshold Crossed: 1 out of the last 1 datapoints [99.50160229693434 (02/10/19 16:59:00)] was greater than the threshold (50.0) (minimum 1 datapoint for OK -> ALARM transition).", | |
"reasonData": "{\"version\":\"1.0\",\"queryDate\":\"2019-10-02T17:04:40.985+0000\",\"startDate\":\"2019-10-02T16:59:00.000+0000\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[99.50160229693434],\"threshold\":50.0}", | |
"timestamp": "2019-10-02T17:04:40.989+0000", | |
"value": "ALARM" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment