Send SMS with AWS
This Python code snippet demonstrates how to send SMS messages using AWS Simple Notification Service (SNS) and the boto3 library. You can use this code to send transactional SMS messages to phone numbers.
import boto3
SMS_NUMBER='+27000000000'
sns = boto3.Session(profile_name='prod', region_name='eu-west-1').client('sns')
response = sns.publish(
PhoneNumber=SMS_NUMBER,
Message='testing',
MessageAttributes={
'AWS.SNS.SMS.SenderID': {
'DataType': 'String',
'StringValue': '123'
},
'AWS.SNS.SMS.SMSType': {
'DataType': 'String',
'StringValue': 'Transactional'
}
}
)
Explanation
The code uses the boto3 library to interact with AWS SNS. It configures a session with a specified profile and region, then creates an SNS client. The publish method sends the SMS message to the specified phone number. Message attributes are used to set the sender ID and message type.
Prerequisites
Before running this code, ensure you have the following:
- An AWS account
- Configured AWS credentials with the necessary permissions to access SNS
- The
boto3library installed (pip install boto3)
Additional Resources
For more information, refer to the following resources: