Skip to content
Advertisement

Python boto3 SNS email formatting (each string in new line)

How to print each string in a new line in an email using AWS SNS service.

If I print a message in Python output all strings is in new lines:

Started copying.. snapshot_id: snap-000000aeaada0000 from: region_src to: region_dst with new snapshot_id: new_snapshot_id
Started copying.. snapshot_id: snap-000000aeaada0001 from: region_src to: region_dst with new snapshot_id: new_snapshot_id

but in an email it is all in one line:

Started copying.. snapshot_id: snap-05b3834aeaada5a02 from: region_src to: region_dst with new snapshot_id: new_snapshot_id Started copying.. snapshot_id: snap-012d3db747de08d1f from: region_src to: region_dst with new snapshot_id: new_snapshot_id
import boto3

region_src = 'us-east-1'
sns_arn = "arn:aws:sns:us-east-1:000000000099:AWS_LAMBDA_NOTIFICATIONS"

def copy_snapshot_src_to_dst(snapshot_id):
    message = ("Started copying.. snapshot_id: " + str(snapshot_id) + " from: " + "region_src" + " to: " + "region_dst" + " with new snapshot_id: " + "new_snapshot_id")
    #print(message)

    return message

def lambda_handler():
    snapshots_id = [('snap-000000aeaada0000', [{'Key': 'Backup_Type', 'Value': 'Demo'}, {'Key': 'Backup Initiator Rule', 'Value': 'Daily-6d-retention'}, {'Key': 'Disaster_Recovery', 'Value': 'Full'}, {'Key': 'aws:backup:source-resource', 'Value': 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'}, {'Key': 'Name', 'Value': 'HOSTNAME'}]), ('snap-000000aeaada0001', [{'Key': 'Name', 'Value': 'HOSTNAME'}, {'Key': 'Backup Initiator Rule', 'Value': 'Daily-6d-retention'}, {'Key': 'Backup_Type', 'Value': 'Demo'}, {'Key': 'Disaster_Recovery', 'Value': 'Full'}, {'Key': 'aws:backup:source-resource', 'Value': 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'}])]
    message = ""

    for i in snapshots_id:
        snapshot_id = i[0]
        message += copy_snapshot_src_to_dst(snapshot_id) + 'n'
    print(message)
    send_sns(message)

def send_sns(message):
    if sns_arn:
        print("Sending SNS alert")
        sns = boto3.client("sns", region_name=region_src)
        response = sns.publish(
            TargetArn=sns_arn,
            Subject=("AWS LAMBDA NOTIFICATION"),
            Message=(message)
        )


lambda_handler()

Advertisement

Answer

replace 'n' by ".n" and after that in an email – each string is in a new line.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement