Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

AWS SNS Deploy notification to slack return as json

I have create a CICD pipeline to my EC2 machine and also configure cloudfront cache clear at the end. After cloudfront cache clear with lambda function it trigger SNS notification to send deploy success notification to my slack channel.

Right now everything works fine but the deploy success notification i received in my slack channel is in json format. I think its returning entire json source. I did try to setup publish message but it work only when I test lambda function manually..

My Lambda python 3.7 code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#!/usr/bin/python3.6
import urllib3
import json
http = urllib3.PoolManager()
def lambda_handler(event, context):
    print(event)
    url = "https://hooks.slack.com/services/web___hook__url"
    msg = {
        "channel": "#deploy-notifications", //my channel name
        "username": "WEBHOOK_USERNAME",  // do I need to change this WEBHOOK_USERNAME ? if yes with what ?
        "text": event['Records'][0]['Sns']['Message'],
        "icon_emoji": ""
    }
    
    encoded_msg = json.dumps(msg).encode('utf-8')
    resp = http.request('POST',url, body=encoded_msg)
    print({
        "message": event['Records'][0]['Sns']['Message'], 
        "status_code": resp.status, 
        "response": resp.data
    })

Do I need to change this WEBHOOK_USERNAME ? if yes with what ?

"username": "WEBHOOK_USERNAME",

After this lambda function is triggered by clearing cloudfront Invalidating cache I received this json in my slack channel.

{
    "version": "1.0",
    "timestamp": "2022-08-31T14:23:20.379Z",
    "requestContext": {
        "requestId": "******-88d6-4fe8-876b-*****",
        "functionArn": "arn:aws:lambda:us-east-1:****:function:Clear_cloudfront_******_ssr:$LATEST",
        "condition": "Success",
        "approximateInvokeCount": 1
    },
    "requestPayload": {
        "Records": [{
            "EventSource": "aws:sns",
            "EventVersion": "1.0",
            "EventSubscriptionArn": "***************-Deploy-Success:***************",
            "Sns": {
                "Type": "Notification",
                "MessageId": "******-af7d-5ca3-b95b-*********",
                "TopicArn": "arn:aws:sns:us-east-1:***********:*****-Deploy-Success",
                "Subject": "SUCCEEDED: AWS CodeDeploy d-**** in us-east-1 to *******-code-deploy",
                "Message": "{\"region\":\"us-east-1\",\"accountId\":\"******\",\"eventTriggerName\":\"******DeploySuccess\",\"applicationName\":\"*****-code-deploy\",\"deploymentId\":\"d-*****\",\"deploymentGroupName\":\"ec2-code-deploy-dg\",\"createTime\":\"Wed Aug 31 14:23:05 UTC 2022\",\"completeTime\":\"Wed Aug 31 14:23:17 UTC 2022\",\"deploymentOverview\":\"{\\\"Succeeded\\\":1,\\\"Failed\\\":0,\\\"Skipped\\\":0,\\\"InProgress\\\":0,\\\"Pending\\\":0}\",\"status\":\"SUCCEEDED\"}",
                "Timestamp": "2022-08-31T14:23:18.215Z",
                "SignatureVersion": "1",
                "Signature": "*********/***+**/************/******==",
                "SigningCertUrl": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-******.pem",
                "UnsubscribeUrl": "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:*****:****-Deploy-Success:****-*****-****",
                "MessageAttributes": {}
            }
        }]
    },
    "responseContext": {
        "statusCode": 200,
        "executedVersion": "$LATEST"
    },
    "responsePayload": null
}

enter image description here

How can I return plain message. Where it says only "Code deployment success."

>Solution :

You can set the "text" field to be whatever message you’d like.

[...]
def lambda_handler(event, context):
    print(event)
    url = "https://hooks.slack.com/services/web___hook__url"
    msg = {
        "channel": "#deploy-notifications", //my channel name
        "username": "WEBHOOK_USERNAME",  // do I need to change this WEBHOOK_USERNAME ? if yes with what ?
        "text": ":white_check_mark: Code deployment success.",
        "icon_emoji": ""
    }
[...]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading