Skip to content

Instantly share code, notes, and snippets.

@chrisking
Created March 9, 2021 16:03
Show Gist options
  • Save chrisking/1011539b1983b57eaa5f89c2f3b1f879 to your computer and use it in GitHub Desktop.
Save chrisking/1011539b1983b57eaa5f89c2f3b1f879 to your computer and use it in GitHub Desktop.
sample lambda
import json
import datetime
from dateutil.parser import parse
import boto3
topic_arn = "arn:aws:sns:us-west-2:059124553121:alertsforlookout"
def send_to_sns(message):
sns = boto3.client("sns")
sns.publish(TopicArn=topic_arn,
Message=message,
Subject="Lookout for Metrics Alert Update")
def create_anomaly_string(input_json):
timestamp = parse(input_json['timestamp'], fuzzy_with_tokens=True)[0]
timestamp1 = timestamp.strftime("%B %d %Y")
timestamp2 = timestamp.strftime("%H:%M")
# Setup the time
response = "An anomaly in " + str(input_json['impactedMetric']['metricName']) + " was detected on " + timestamp1 + ' at ' + timestamp2 + ".\n"
# Now define your information
num_of_time_series = len(input_json['impactedMetric']['relevantTimeSeries'])
response += str(num_of_time_series) + " time series was impacted.\n\n"
for ts in range(num_of_time_series):
response += "TimeSeries: " + str(num_of_time_series) + " was impacted on the following dimensions and values:\n"
for item in input_json['impactedMetric']['relevantTimeSeries'][ts]['dimensions']:
response += "\t" + item['dimensionName'] + " - " + item['dimensionValue'] + "\n"
response += "\nThe Anomaly score was: " + str(input_json['anomalyScore']) + "\n"
url = "https://us-west-2.console.aws.amazon.com/lookoutmetrics/home?region=us-west-2#"
url += str(input_json['anomalyDetectorArn'])
response += "To learn more visit the Lookout for Metrics console at: " + url + " \n"
return response
def lambda_handler(event, context):
# TODO implement
response = create_anomaly_string(input_json=event)
send_to_sns(message=response)
return {
'statusCode': 200,
'body': json.dumps('Message Delivered!')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment