amazon-web-servicesamazon-cloudwatchamazon-snscloudwatch-alarms

How can I add a custom message attribute to the sns notification sent by a an aws cloudwatch alarm?


I have created an AWS cloudwatch alarm that is linked to an SNS topic as an action, and then this SNS topic triggers a lambda that handles the message and decides what to do with it (drop it or send it to another destination).

The image below is a similar representation of what I have:

enter image description here

The issue that I facing is that I couldn't find a way to add a custom SNS message attribute to the SNS message that is sent to the lambda.

The reason why I want to do this is that I have a set of alarms that I want to be handled in the same way, so my idea is if I can add a custom attribute to the alarm I would be able then to use that attribute in my lambda as a tag or an indicator to decide how to handle the alarm message.

Note that the alarms could be from different namespaces and different services but they should be handled in the same way.

An alternative way that I thought of is to create an SNS topic for each set of alarms that I want to handle in the same way, then in the lambda, I would decide how to handle the message based on the source topic of the SNS message. But I wonder if there is a better way to do this or if my first idea of adding a custom message attribute to the SNS event is feasible?


Solution

  • There isn't a way of adding a message attribute to your first topic from the alarm directly. I've had a similar requirement so can see the value of this. Your proposal for multiple topics works and I've seen it done in practice, for example using a topic per alarm priority.

    An alternative option is for the Lambda function to derive metadata for itself rather than being fed it from message attributes. Any of the alarm's parameters could be used, but it sounds like you need something user defined, in which case you could do this using the alarm description or tags: