How can I change the default Amazon SNS email subject line, “AWS Notification Message”, for a CloudWatch Events rule?

Last updated: 2021-01-06

I configured my Amazon Simple Notification Service (Amazon SNS) to receive email notifications from an Amazon CloudWatch Events rule that has multiple event sources. How do I customize the default SNS email subject line, "AWS Notification Message," and body copy based on the event that triggers the notification?

Short description

There is currently no way to customize the message of an Amazon SNS email based on specific CloudWatch Events rules using the Amazon SNS console.

Instead, use a Lambda function—rather than the Amazon SNS topic—as a target for the CloudWatch Events rule. Then, configure the Lambda function to publish a custom message to the Amazon SNS topic when triggered by the CloudWatch Events rule.

Here's how the logic works:

  1. The CloudWatch Events rule is triggered.
  2. The Lambda function invokes with the payload of the CloudWatch Events rule.
  3. The function calls the Amazon SNS publish API.
  4. Amazon SNS delivers an email notification with a custom subject and/or message.

Resolution

Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you’re using the most recent AWS CLI version.

Create an AWS Lambda function that is set as a target for the CloudWatch Events rule

For more information, see Schedule AWS Lambda functions using CloudWatch Events.

Important: Make sure that the Lambda function’s execution role has permission to publish to the Amazon SNS topic. For example, if your function's execution role has the AWS managed policy, AWSLambdaBasicExecutionRole, you must attach the AmazonSNSFullAccess policy to the execution role.

After the Lambda function is set as a target for the CloudWatch Events rule, the following resource-based policy is automatically added to the function:

{
  "Version": "2012-10-17",
  "Id": "default",
  "Statement": [
    {
      "Sid": "AWSEvents_CWRule_CustomEmailSubject_Id196649187337",
      "Effect": "Allow",
      "Principal": {
        "Service": "events.amazonaws.com"
      },
      "Action": "lambda:InvokeFunction",
      "Resource": "<Lambda-function-ARN>",
      "Condition": {
        "ArnLike": {
          "AWS:SourceArn": "<Cloudwatch-event-rule-ARN>"
        }
      }
    }
  ]
}

Configure the Lambda function to publish a custom email subject or a custom message to the Amazon SNS topic

For more information, see Publish (API reference).

Important: The following code snippets are for reference only. Don't use the code snippets in your production environment before testing them.

Example Python publish API call

import boto3
import json
sns_arn = "<SNS_TOPC_ARN>"

def lambda_handler(event, context):
    client = boto3.client("sns")
    resp = client.publish(TargetArn=sns_arn, Message=json.dumps(event), Subject="<CUSTOM_SUBJECT>")
Example JavaScript/NodeJS publish API call
const AWS = require('aws-sdk');

exports.handler = (event, context, callback) => {
    let sns = new AWS.SNS();
    sns.publish({
    TopicArn: '<SNS_TOPIC_ARN>',
    Message: JSON.stringify(event), 
    Subject: '<CUSTOM_SUBJECT>'
    }, function(error, data){
       if(error) console.log(error, error.stack);
       callback(error, data);
     });
};

Important: Make sure that you replace the values for <SNS_TOPIC_ARN> and <CUSTOM_SUBJECT> with your own inputs.

In this way, you can use a Lambda function to customize and forward a CloudWatch Events rule’s email subject or message to an Amazon SNS topic.