0

I created a Cloudwatch alert to send a notification to SNS whenever the state of the container changes. The SNS then triggers the Lambda function in order to post the alert to slack. The problem I have is that there is too much information in the Message. I want to choose a few fields such as availabilityZone, name, lastStatus, desiredStatus and connectivity.

Here is my lambda function.

#!/usr/bin/python3.6
import urllib3
import json
http = urllib3.PoolManager()
def lambda_handler(event, context):
    url = "https://hooks.slack.com/services/XXXXXXXXXXXX"
    msg = {
        "channel": "#XXXXX",
        "username": "XXXXX",
        "text": event['Records'][0]['Sns']['Message'],
        "icon_emoji": ""
    }
    
    encoded_msg = json.dumps(msg).encode('utf-8')
    resp = http.request('POST',url, body=encoded_msg)

Here is what I get in slack.

{"version":"0","id":"641aaafc-d51d-cd4f-0ab8-92a53455345”,”detail-type":"ECS Task State Change","source":"aws.ecs","account”:”123456789”,”time":"2020-09-16T22:18:47Z","region":"us-west-2","resources":["arn:aws:ecs:us-west-2:123456789:task/2b9f7454b-db7c-4af3-8aa8-4234234234f"],"detail":{"attachments":[],"availabilityZone":"us-west-2a","clusterArn":"arn:aws:ecs:us-west-2:123456789:cluster/clustername”,”containerInstanceArn":"arn:aws:ecs:us-west-2:123456789:container-instance/454545-6ebe-4cca-8d39-068468cf6fe4","containers":[{"containerArn":"arn:aws:ecs:us-west-2:123456789:container/dfgdf4-b5de-4171-b395-fdfdfgdfg545”,”lastStatus":"RUNNING","name":"TaskName","image":"123456789.dkr.ecr.us-west-2.amazonaws.com/imagename,”imageDigest":"sha256:35c86c84ff4e2965dfgdfgdfgdfgbb978d406e668411b849df8d1a6122d247c4f7a832","runtimeId":"818d6f8b811bdfgdfgdfgdfg317bf24bdfeb3a915e3a7cd9ec2416c48b","networkBindings":[{"bindIP":"0.0.0.0","containerPort":80,"hostPort”:2082,"protocol":"tcp"}],"taskArn":"arn:aws:ecs:us-west-2:123456789:task/2b9f7a6b-db7c-4aferera8-e548eaa8403f","networkInterfaces":[],"cpu":"512","memory":"1024","memoryReservation":"102"}],"createdAt":"2020-09-15T16:49:28.597Z","launchType":"EC2","cpu":"512","memory":"102","desiredStatus":"STOPPED","group":"service:dServiceName”,”lastStatus":"RUNNING","overrides":{"containerOverrides":[{"name":"TaskName"}]},"connectivity":"CONNECTED","connectivityAt":"2020-09-15T16:49:28.597Z","pullStartedAt":"2020-09-15T16:49:29.712Z","startedAt":"2020-09-15T16:49:30.712Z","startedBy":"ecs-svc/5345345345345”,”stoppingAt":"2020-09-16T22:18:47.075Z","pullStoppedAt":"2020-09-15T16:49:29.712Z","stoppedReason":"Scaling activity initiated by (deployment ecs-svc/345345345345345)”,”updatedAt":"2020-09-16T22:18:47.075Z","taskArn":"arn:aws:ecs:us-west-2:123456789:task/345345dgfg-db7c-4af3-8aa8-e548eaa8403f","taskDefinitionArn":"arn:aws:ecs:us-west-2:123456789:task-definition/TaskDefinication:9”,”version":3}}
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Bash
  • 1
  • 2
    Do you know how to parse JSON using Python? See [this post](https://stackoverflow.com/questions/7771011/how-to-parse-json-in-python) for some ideas. – stdunbar Sep 16 '20 at 22:44
  • The message from SNS is a string containing JSON. You can convert it to a Python object by using `json.loads()`. Then, construct a new message from whichever fields you wish. – John Rotenstein Sep 16 '20 at 23:24

0 Answers0