customsmssender lambda trigger python code example

Example: sending a text message with lambda using a database of phone numbers

import logging
import boto3

# Initialize logger and set log level
logger = logging.getLogger()
logger.setLevel(logging.INFO)

# Initialize SNS client for Ireland region
session = boto3.Session(
    region_name="eu-west-1"
)
sns_client = session.client('sns')


def lambda_handler(event, context):

    # Send message
    response = sns_client.publish(
        PhoneNumber=event["phone_number"],
        Message='Hi there! This is a test message sent with Amazon SNS',
        MessageAttributes={
            'AWS.SNS.SMS.SenderID': {
                'DataType': 'String',
                'StringValue': 'SENDERID'
            },
            'AWS.SNS.SMS.SMSType': {
                'DataType': 'String',
                'StringValue': 'Promotional'
            }
        }
    )

    logger.info(response)
    return 'OK'

Tags:

Misc Example