AWS Lambda: Sending SMS using Python

{
"Statement": [
{
"Effect": "Allow",
"Action": [
"sns:Publish",
"sns:SetSMSAttribute"
],
"Resource": "*"
}
]
}
import logging
import boto3
def lambda_handler(event, context):
try:
phone_number = event["phone_number"]
sender = event["sender"]
message = event["message"]

client = boto3.client("sns")

# Send the sms message.
response = client.publish(
PhoneNumber=event["phone_number"],
Message=message,
MessageAttributes={
'AWS.SNS.SMS.SenderID': {
'DataType': 'String',
'StringValue': sender
},
'AWS.SNS.SMS.SMSType': {
'DataType': 'String',
'StringValue': 'Promotional'
}
}
)
logging.info("Message ist sent to %s with MessageId %s",
event["phone_number"], response['MessageId'])

print("Message ist sent to %s with MessageId %s",
event["phone_number"], response['MessageId'])
except Exception as e:
logging.error(e)
{
"phone_number":"0000",
"message":"This is the test message for lambda sms!",
"sender":"cosmos",
}

Summary

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using AWS Lambda with S3 and DynamoDB

Creating a Loot system (part 1)

How to livestream your Meetup for free as events become virtual

Queue management system structure of stages of a customer service process

Wrapping impure code with ZIO

PlugShare is selling your data

The user is the product

Getting Started With Ansible

Learn Python With Me!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
cem akpolat

cem akpolat

More from Medium

Creating a simple database using Python and SQLite

Create a custom analytics server in 30 minutes with Python & AWS

Use AWS S3 and Python to share your pictures and videos as a webpage

SQL for Data Engineering with 5 best Queries