Ihor Pysmennyi
Ihor Pysmennyi

Reputation: 39

Send SNS notifications to Slack

I'd like to send SNS notifications into Slack. I receive notifications on my e-mail. It looks like:

Instance: i-0f9606e41cd6f1e8e has changed state
State: running
Type: c5.4xlarge
Public IP Address: 52.32.193.26
Private IP Address: 10.10.75.168
Region: us-west-2a
Name: VOSaaS-Cluster-SaaS-Longevity-055ba27d-f7c4-b70a-0954-a08ae21ccb2d-vos-node-i-0f9606e41cd6f1e8e

But also I want to receive the same output into my Slack channel. I've already set up the incoming webhooks and I can receive simple messages but have a problem with sending output.

MY_SNS_TOPIC_ARN = 'arn:aws:sns:us-west-2:421572644019:CloudWatchAlarmsForSpotInstances'
sns_client = boto3.client('sns')
ec2_spot_info = sns_client.publish(
    TopicArn = MY_SNS_TOPIC_ARN,
    Subject = 'EC2 Spot Instances Termination Notifications',
    Message =   'Instance: ' + instance_id + ' has changed state\n' +
                'State: ' + instance['State']['Name'] + '\n' +
                'Type: ' + instance['InstanceType'] + '\n' +
                'Public IP Address: ' + instance['PublicIpAddress'] + '\n' +
                'Private IP Address: ' + instance['PrivateIpAddress'] + '\n' +
                'Region: ' + instance['Placement']['AvailabilityZone'] + '\n' +
                'Name: ' + name
)

slack_url='https://hooks.slack.com/services/+token'
slack_msg = {
                "attachments": [
                    {
                        "title": "EC2 Spot Instance Info",
                        "pretext": "EC2 Spot Instances Termination Notifications",
                        "color": "#ed1717",
                        "text": ec2_spot_info
                    }
                ]
            }
output = json.dumps(slack_msg)
r = requests.post(slack_url, data = output)

Upvotes: 2

Views: 3588

Answers (2)

Sam
Sam

Reputation: 776

There is an issue when When you Subscribe Slack wehbook with SNS. The slack is unable to convert/read the payload comes from SNS. You have to do a bit of hack to read the SubscribeURL/Message. Try with pure SNS topic with a slack channel first.

You can use SLACK workflow with SNS.

Follow the video which show all the steps clearly. https://www.youtube.com/watch?v=CszzQcPAqN

Steps to follow:

  • Create slack channel or use existing channel
  • Create a work flow with selecting Webhook
  • Create a variable name as "SubscribeURL". The name is very important
  • Add the above variable in the message body of the workflow
  • Publish the workflow and get the url
  • Add the above Url as subscription of the SNS
  • You will see the subscription URL in the slack channel
  • Follow the URl and complete the subscription
  • Come back to the work flow and change the variable to "Message"
  • The publish the message in SNS. you will see the message in the slack channel.

Upvotes: 1

John Rotenstein
John Rotenstein

Reputation: 269470

The sns_client.publish() call returns a response of:

{
    'MessageId': 'string'
}

Yet your slack command is sending this as a message:

"text": ec2_spot_info

This means that, instead of sending a message to slack, you are sending a dictionary containing the MessageId.

Instead, you should:

  • Construct message as a variable
  • Call sns_client.publish() with Message = message
  • Call slack using "text": message

Upvotes: 1

Related Questions