How do I customize default Amazon SNS email messages?

3 minute read
0

I want to modify the standard Amazon Simple Notification Service (Amazon SNS) email subject line "AWS Notification Message" for an Amazon EventBridge notification.

Short description

Amazon SNS receives notifications from an EventBridge rule that's created with AWS services as the event source for delivery by email. Note that the subject line "AWS Notification Message" and email body are fixed. You can't change these directly if any AWS service triggers the Amazon SNS topic that has the email subscription.

Workflow

  1. An event triggers an EventBridge rule.
  2. The EventBridge rule's payload invokes the Lambda function.
  3. The Lambda function calls the Amazon SNS Publish API.
  4. Amazon SNS delivers a message or email notification with the custom "Subject" and "body."

Resolution

Include AWS Lambda in the architecture to customize the default email subject line and message body as needed. You can use AWS Lambda instead of the Amazon SNS topic as a target for the EventBridge rule. Then, use the Lambda function to publish messages with custom email subject and body to the Amazon SNS topic with the subscribers.

Follow these steps to customize the notification.

Create an SNS topic and email subscription

  1. Create an SNS topic.
  2. Create an email subscription.

Create the Lambda function

  1. Open the Functions page of the Lambda console.

  2. Choose Create function.

  3. Select Author from scratch.

  4. Provide the Function name, and then select the runtime as Python 3.10.

  5. Create an Execution role. This role must have the necessary permissions to publish the specified SNS topic. Here, your Lambda function was created with the AWS managed policy for Lambda.

  6. Attach AWSLambdaBasicExecutionRole and AmazonSNSFullAccess policy to the function's execution role. To attach the execution policy for Lambda, see Lambda execution role.

  7. Choose Create function. In the function Code editor, paste this code:
    Important: This code snippet is for reference only. Don't use example code snippets in your production environment before testing it.

    Example: Python sample code

    import boto3
    import json
    sns_arn = "sns_topic_arn"
    
    def lambda_handler(event, context):
        client = boto3.client("sns")
        resp = client.publish(TargetArn=sns_arn, Message=json.dumps(event), Subject="This is the customized subject line")

    Note: Replace sns_topic_arn with the topic ARN from Create an SNS topic and email notification. Replace "This is the customized subject line" with your own subject line, within quotes.

  8. Choose Deploy.

Create an EventBridge rule

Create an EventBridge rule using the Amazon EventBridge console.

Note:

  • For step 11 in the AWS documentation, Select a target, choose the Lambda function that you created under Create a lambda function.
  • Provide the payload message that you want for your Lambda function.

When the EventBridge rule is invoked, the Lambda function makes a Publish API call to Amazon SNS. It forwards the event rule's message while changing the subject that it uses to deliver the message. The subscriber then receives the email with the customized subject line in their mailbox.

Related information

Why can't I receive email notifications from my Amazon SNS topic?

Why did I get an email from AWS stating that my Amazon SNS subscription was manually disabled?

How do I keep mailing list recipients from unsubscribing everyone on the list from my Amazon SNS topic emails?

Why didn't I receive an SNS notification for my Amazon CloudWatch alarm trigger?

How do I send email using Lambda and Amazon Simple Email Service (Amazon SES)?

AWS OFFICIAL
AWS OFFICIALUpdated 8 months ago