Skip to content

How do I customize the default Amazon SNS email subject line?

3 minute read
1

I want to customize the default 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. You can't change the email subject line AWS Notification Message directly if any AWS service triggers the Amazon SNS topic that has the email subscription.

Resolution

Create an AWS Lambda function to customize the default email subject line. You can use 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 line to the Amazon SNS topic with the subscribers.

Create an SNS topic and email subscription

Create an SNS topic and then create an email subscription.

Create an IAM role for the Lambda function

To create an AWS Identity and Access Management (IAM) role for the Lambda function, complete the following steps:

  1. Open the IAM console.
  2. In the navigation pane, choose Roles, and then choose Create role.
  3. Choose the Service or use case dropdown list, choose Lambda, and then choose Next.
  4. In the Add permissions page, attach the AWSLambdaBasicExecutionRole and AmazonSNSFullAccess policies to the role, and then choose Next.
    Important: The AmazonSNSFullAccess managed policy provides full access to Amazon SNS. It's a best practice to follow the principle of least privilege.
  5. In Role name, enter a name for the role, and then choose Create role.

Create the Lambda function

Complete the following steps:

  1. Open the Functions page of the Lambda console.

  2. Choose Create function.

  3. Select Author from scratch.

  4. In Function name, enter a name for the function.

  5. Choose the Runtime dropdown list, and then select Python 3.13.

  6. Expand Change default execution role, and then choose Use an existing role.

  7. Choose the Existing role dropdown list, and then choose the role that you created previously.

  8. Choose Create function.

  9. In the function code editor, enter the following Python code snippet:

    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 Amazon Resource Name (ARN) from Create an SNS topic and email notification. Replace "This is the customized subject line" with your own subject line, within quotes.

  10. Choose Deploy.

Create an EventBridge rule

Follow these instructions to create an EventBridge rule.

Note:

  • For step 11 in the AWS documentation, Select a target, choose the Lambda function that you created previously.
  • 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 and changes the subject line that it uses to deliver the message. The subscriber then receives the email with the customized subject line in their mailbox.

Related information

How do I use Lambda and Amazon SES to send email?