From 0d6ef359f5adbccdd865e5fed05ce2f4769d813d Mon Sep 17 00:00:00 2001 From: Stefan Reimer Date: Tue, 16 May 2023 14:23:43 +0000 Subject: [PATCH] Add support for ASG Events, fix deep link to AWS UI --- app.py | 26 ++++++++++++++++++++++---- tests/test_aws-lambda-rie.py | 6 ++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 62cfea5..e4b66ae 100644 --- a/app.py +++ b/app.py @@ -39,7 +39,8 @@ else: # Ensure slack URLs use ?blocks=yes if "slack.com" in WEBHOOK_URL: - scheme, netloc, path, query_string, fragment = urllib.parse.urlsplit(WEBHOOK_URL) + scheme, netloc, path, query_string, fragment = urllib.parse.urlsplit( + WEBHOOK_URL) query_params = urllib.parse.parse_qs(query_string) query_params["blocks"] = ["yes"] new_query_string = urllib.parse.urlencode(query_params, doseq=True) @@ -56,7 +57,8 @@ asset.app_url = "https://zero-downtime.net" asset.image_url_mask = ( "https://cdn.zero-downtime.net/assets/zdt/apprise/{TYPE}-{XY}{EXTENSION}" ) -asset.app_id = "{} / {} {}".format("cloudbender", __version__, "zero-downtime.net") +asset.app_id = "{} / {} {}".format("cloudbender", + __version__, "zero-downtime.net") apobj = apprise.Apprise(asset=asset) apobj.add(WEBHOOK_URL) @@ -142,7 +144,7 @@ def handler(event, context): title = "AWS EC2 - CloudBender" try: - msg_context = "{account} - {region} - {host} ({instance}) ".format( + msg_context = "{account} - {region} - {host} ({instance}) ".format( account=get_alias(msg["AWSAccountId"]), region=msg["Region"], asg=msg["Asg"], @@ -239,7 +241,23 @@ def handler(event, context): # New simple ElasticCache notifications elif "ElastiCache:SnapshotComplete" in msg: title = "ElastiCache Snapshot complete." - body = "Snapshot taken on {}".format(msg["ElastiCache:SnapshotComplete"]) + body = "Snapshot taken on {}".format( + msg["ElastiCache:SnapshotComplete"]) + + # Basic ASG events + elif "Origin" in msg and msg["Origin"] == "AutoScalingGroup": + title = msg["Description"] + body = msg["Cause"] + + try: + msg_context = "{account} - {region} - ".format( + region=region, + account=get_alias(msg["AccountId"]), + asg=msg["AutoScalingGroupName"], + ) + body = body + "\n\n_{}_".format(msg_context) + except KeyError: + pass else: title = "Unknown message type" diff --git a/tests/test_aws-lambda-rie.py b/tests/test_aws-lambda-rie.py index e66900f..9956a76 100755 --- a/tests/test_aws-lambda-rie.py +++ b/tests/test_aws-lambda-rie.py @@ -67,3 +67,9 @@ class Test: r' {"Records": [{"EventSource": "aws:sns", "EventVersion": "1.0", "EventSubscriptionArn": "arn:aws:sns:eu-central-1:123456789012:AlertHub:0e7ce1ba-c3e4-4264-bae1-4eb71c91235a", "Sns": {"Type": "Notification", "MessageId": "10ae86eb-9ddc-5c2f-806c-df6ecb6bde42", "TopicArn": "arn:aws:sns:eu-central-1:123456789012:AlertHub", "Subject": null, "Message": "{\"ElastiCache:SnapshotComplete\":\"redis-prod-0002-001\"}" }}]}' ) self.send_event(event) + + def test_asg(self): + event = json.loads( + r' {"Records": [{"EventSource": "aws:sns", "EventVersion": "1.0", "EventSubscriptionArn": "arn:aws:sns:eu-central-1:123456789012:AlertHub:0e7ce1ba-c3e4-4264-bae1-4eb71c91235a", "Sns": {"Type": "Notification", "MessageId": "10ae86eb-9ddc-5c2f-806c-df6ecb6bde42", "TopicArn": "arn:aws:sns:eu-central-1:123456789012:AlertHub", "Subject": null, "Message": "{\"Origin\":\"AutoScalingGroup\",\"Destination\":\"EC2\",\"Progress\":50,\"AccountId\":\"123456789012\",\"Description\":\"Terminating EC2 instance: i-023ca42b188ffd91d\",\"RequestId\":\"1764cac3-224b-46bf-8bed-407a5b868e63\",\"EndTime\":\"2023-05-15T08:51:16.195Z\",\"AutoScalingGroupARN\":\"arn:aws:autoscaling:us-west-2:123456789012:autoScalingGroup:4a4fb6e3-22b4-487b-8335-3904f02ff9fd:autoScalingGroupName/powerbi\",\"ActivityId\":\"1764cac3-224b-46bf-8bed-407a5b868e63\",\"StartTime\":\"2023-05-15T08:50:14.145Z\",\"Service\":\"AWS Auto Scaling\",\"Time\":\"2023-05-15T08:51:16.195Z\",\"EC2InstanceId\":\"i-023ca42b188ffd91d\",\"StatusCode\":\"InProgress\",\"StatusMessage\":\"\",\"Details\":{\"Subnet ID\":\"subnet-fe2d6189\",\"Availability Zone\":\"us-west-2a\"},\"AutoScalingGroupName\":\"powerbi\",\"Cause\":\"At 2023-05-15T08:50:03Z the scheduled action end executed. Setting min size from 1 to 0. Setting desired capacity from 1 to 0. At 2023-05-15T08:50:03Z a scheduled action update of AutoScalingGroup constraints to min: 0, max: 1, desired: 0 changing the desired capacity from 1 to 0. At 2023-05-15T08:50:13Z an instance was taken out of service in response to a difference between desired and actual capacity, shrinking the capacity from 1 to 0. At 2023-05-15T08:50:14Z instance i-023ca42b188ffd91d was selected for termination.\",\"Event\":\"autoscaling:EC2_INSTANCE_TERMINATE\"}" }}]}' + ) + self.send_event(event)