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

Last updated: 2021-12-14

I configured my Amazon Simple Notification Service (Amazon SNS) to receive email notifications from an Amazon EventsBridge rule that has multiple event sources. How do I customize the default SNS email subject line, "AWS Notification Message," and email body 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 EventBridge rules using the Amazon SNS console.

Use a Lambda function, instead of the Amazon SNS topic, as a target for the EventBridge rule. Then, configure the Lambda function to publish a custom message to the Amazon SNS topic when triggered by the EventBridge rule.

Here's how the logic works:

  1. The EventBridge rule is triggered.
  2. The Lambda function invokes with the payload of the EventBridge rule.
  3. The function calls the Amazon SNS publish API.
  4. Amazon SNS delivers an email notification with a custom subject 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 EventBridge rule

For more information, see Tutorial: Schedule AWS Lambda functions using EventBridge.

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 EventBridge 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": "Eventbridge-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/Node.js 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 EventBridge rule’s email subject or message to an Amazon SNS topic.


Did this article help?


Do you need billing or technical support?